# Tail recursion with 1.9

**URL:** <https://www.ruby-forum.com/t/tail-recursion-with-1-9/193061>\
**Category:** Ruby\
**Created:** [August 10, 2010, 4:05pm UTC](https://www.ruby-forum.com/t/tail-recursion-with-1-9/193061 "2010-08-10T16:05:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tensaiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6de8d8/32.png) [@tensaiji](https://www.ruby-forum.com/u/tensaiji)\
**Post date:** [August 10, 2010, 4:05pm UTC](https://www.ruby-forum.com/t/tail-recursion-with-1-9/193061/1 "2010-08-10T16:05:33Z")

</div>

Hi,

I read in the first answer here

> <https://stackoverflow.com/questions/824562/does-ruby-perform-tail-call-optimization>

that tail recursion is not on by default and is enabled by uncommenting  
a line in the source before compilation. Is this still true, and is  
there anyway to tell if it’s on or not in a particular installation?

Regards,  
Iain

---

<div class="post-metadata">

**Author:** ![tensaiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6de8d8/32.png) [@tensaiji](https://www.ruby-forum.com/u/tensaiji)\
**Post date:** [August 10, 2010, 5:43pm UTC](https://www.ruby-forum.com/t/tail-recursion-with-1-9/193061/2 "2010-08-10T17:43:57Z")

</div>

Its commented out.

See vm\_opts.h.

As far as sensing it in Ruby I would think

ruby-1.9.2-rc2.tar.bz2\>

Snippet of vm\_opts.h below:

#ifndef RUBY\_VM\_OPTS\_H  
#define RUBY\_VM\_OPTS\_H

/\* Compile options.

- You can change these options at runtime by VM::CompileOption.
- Following definitions are default values.  
\*/

#define OPT\_TRACE\_INSTRUCTION 1  
#define OPT\_TAILCALL\_OPTIMIZATION 0  
#define OPT\_PEEPHOLE\_OPTIMIZATION 1  
#define OPT\_SPECIALISED\_INSTRUCTION 1  
#define OPT\_INLINE\_CONST\_CACHE 1

RUN TIME OPTIONS could be interesting to play with. You appear to loose  
the  
promise of portability.

See iseq.c (Interesting rb\_compile\_option\_t )

static rb\_compile\_option\_t COMPILE\_OPTION\_DEFAULT = {  
OPT\_INLINE\_CONST\_CACHE, /\* int inline\_const\_cache; _/  
OPT\_PEEPHOLE\_OPTIMIZATION, /_ int peephole\_optimization; _/  
OPT\_TAILCALL\_OPTIMIZATION, /_ int tailcall\_optimization _/  
OPT\_SPECIALISED\_INSTRUCTION, /_ int specialized\_instruction; _/  
OPT\_OPERANDS\_UNIFICATION, /_ int operands\_unification; _/  
OPT\_INSTRUCTIONS\_UNIFICATION, /_ int instructions\_unification; _/  
OPT\_STACK\_CACHING, /_ int stack\_caching; _/  
OPT\_TRACE\_INSTRUCTION, /_ int trace\_instruction \*/  
};

---

<div class="post-metadata">

**Author:** ![tensaiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6de8d8/32.png) [@tensaiji](https://www.ruby-forum.com/u/tensaiji)\
**Post date:** [August 13, 2010, 12:02am UTC](https://www.ruby-forum.com/t/tail-recursion-with-1-9/193061/3 "2010-08-13T00:02:58Z")

</div>

On 10 Aug 2010, at 16:43, Joseph E. Savard wrote:

> #ifndef RUBY\_VM\_OPTS\_H  
> #define OPT\_SPECIALISED\_INSTRUCTION 1  
> OPT\_TAILCALL\_OPTIMIZATION, /\* int tailcall\_optimization _/  
> OPT\_SPECIALISED\_INSTRUCTION, /_ int specialized\_instruction; _/  
> OPT\_OPERANDS\_UNIFICATION, /_ int operands\_unification; _/  
> OPT\_INSTRUCTIONS\_UNIFICATION, /_ int instructions\_unification; _/  
> OPT\_STACK\_CACHING, /_ int stack\_caching; _/  
> OPT\_TRACE\_INSTRUCTION, /_ int trace\_instruction \*/  
> };

Thanks for the reply. Finally got 1.9.1-p429 to compile, and I set  
#define OPT\_TAILCALL\_OPTIMIZATION 1 before compiling, which hopefully  
is right, I don’t know C.

Do you know any way I can check it’s definitely on, aside from  
attempting to blow the stack with something hugely recursive?

Iain

---

<div class="post-metadata">

**Author:** ![tensaiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6de8d8/32.png) [@tensaiji](https://www.ruby-forum.com/u/tensaiji)\
**Post date:** [August 13, 2010, 12:12am UTC](https://www.ruby-forum.com/t/tail-recursion-with-1-9/193061/4 "2010-08-13T00:12:21Z")

</div>

On 12 August 2010 23:52, Iain B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > \*/  
> > See iseq.c (Interesting rb\_compile\_option\_t )  
> > };
> 
> Thanks for the reply. Finally got 1.9.1-p429 to compile, and I set #define OPT\_TAILCALL\_OPTIMIZATION 1 before compiling, which hopefully is right, I don’t know C.
> 
> Do you know any way I can check it’s definitely on, aside from attempting to blow the stack with something hugely recursive?
> 
> Iain

You should probably dig deeper in your own link 🙂

There is a link on the stackoverflow’s question:  
[http://redmine.ruby-lang.org/issues/show/1256](http://redmine.ruby-lang.org/issues/show/1256)  
The third reply from Nobu might help you 😉

Regards,  
B.D.

---

<div class="post-metadata">

**Author:** ![tensaiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6de8d8/32.png) [@tensaiji](https://www.ruby-forum.com/u/tensaiji)\
**Post date:** [August 13, 2010, 12:25am UTC](https://www.ruby-forum.com/t/tail-recursion-with-1-9/193061/5 "2010-08-13T00:25:48Z")

</div>

On 12 Aug 2010, at 23:11, Benoit D. wrote:

> You should probably dig deeper in your own link 🙂
> 
> There is a link on the stackoverflow’s question:  
> [http://redmine.ruby-lang.org/issues/show/1256](http://redmine.ruby-lang.org/issues/show/1256)  
> The third reply from Nobu might help you 😉
> 
> Regards,  
> B.D.

Thanks. I had already read that, but my brain dumps any information that  
I can’t use at that particular moment 🙂

$ irb

> RubyVM::InstructionSequence.compile\_option[:tailcall\_optimization]  
> =\> true

Regards,  
Iain
