Does Ruby optimize tail-call recursion?

P

Patrick Li

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
 
J

Joost Diepenmaat

Patrick Li said:
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 `<main>'

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

Thomas Hurst

* Patrick Li ([email protected]) said:
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.
 
S

SASADA Koichi

Hi,

Thomas said:
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.
 
T

Thomas B.

Joost said:
./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 `<main>'

On my machine:

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top