Does Ruby optimize tail-call recursion?

Hi,
I’m wondering if Ruby 1.9 optimizes tail-call recursion?
I’ve read some reports that say yes, and some that say no. Does anyone
know?

Thanks
-Patrick

Patrick Li wrote:

Hi,
I’m wondering if Ruby 1.9 optimizes tail-call recursion?
I’ve read some reports that say yes, and some that say no. Does anyone
know?

Thanks
-Patrick

The 1.8.x versions do not.

Thank you for verifying. That’s all I wanted to know.
-Patrick

Patrick Li [email protected] writes:

Hi,
I’m wondering if Ruby 1.9 optimizes tail-call recursion?
I’ve read some reports that say yes, and some that say no. Does anyone
know?

I just compiled the ruby svn trunk with all defaults:

./ruby -v
ruby 1.9.0 (2008-08-30 revision 18932) [i686-linux]

./ruby -e ‘def bla(); bla(); end; bla()’
-e:1:in bla': stack level too deep (SystemStackError) from -e:1:in bla’
from -e:1:in bla' from -e:1:in bla’
from -e:1:in bla' from -e:1:in bla’
from -e:1:in bla' from -e:1:in bla’
from -e:1:in bla' ... 7692 levels... from -e:1:in bla’
from -e:1:in bla' from -e:1:in bla’
from -e:1:in `’

So I guess the answer is: no, it does not optimize tail calls.

I’m wondering if Ruby 1.9 optimizes tail-call recursion? I’ve read
some reports that say yes, and some that say no. Does anyone know?

vm_opts.h:

#define OPT_TAILCALL_OPTIMIZATION 0

Set to 1, rebuild. The comments suggest it can be changed at runtime
via VM::CompileOption, which doesn’t seem to exist. Hopefully it’ll be
enabled by default at some point in future.

Hi,

Thomas H. wrote:

vm_opts.h:

#define OPT_TAILCALL_OPTIMIZATION 0

Set to 1, rebuild. The comments suggest it can be changed at runtime
via VM::CompileOption, which doesn’t seem to exist. Hopefully it’ll be
enabled by default at some point in future.

In fact, I don’t check this option. So some bugs may hide on this
option. And backtrace (what you see on exception) is changed. It’s
convention issue.

Joost D. wrote:

./ruby -e ‘def bla(); bla(); end; bla()’
-e:1:in bla': stack level too deep (SystemStackError) from -e:1:inbla’
from -e:1:in bla' from -e:1:inbla’
from -e:1:in bla' from -e:1:inbla’
from -e:1:in bla' from -e:1:inbla’
from -e:1:in bla' ... 7692 levels... from -e:1:inbla’
from -e:1:in bla' from -e:1:inbla’
from -e:1:in `’

On my machine:

ruby -e ‘def bla(); bla(); end; bla()’
-e:1:in bla': stack level too deep (SystemStackError) from -e:1:inbla’
from -e:1

I use Ruby version from windows one-click installer, 1.8.6.