[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Does Ruby optimize tail-call recursion?

Patrick Li

8/29/2008 7:14:00 PM

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
--
Posted via http://www.ruby-....

4 Answers

Joost Diepenmaat

8/29/2008 8:24:00 PM

0

Patrick Li <patrickli_2001@hotmail.com> 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 `<main>'

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

--
Joost Diepenmaat | blog: http://joost.... | work: http://...

Thomas Hurst

9/4/2008 3:11:00 AM

0

* Patrick Li (patrickli_2001@hotmail.com) wrote:

> 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.

--
Thomas 'Freaky' Hurst
http...

SASADA Koichi

9/4/2008 3:55:00 AM

0

Hi,

Thomas Hurst 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.

--
// SASADA Koichi at atdot dot net

TPReal

9/4/2008 8:51:00 AM

0

Joost Diepenmaat wrote:
> ./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.

--
Posted via http://www.ruby-....