Ravi Mohan's Tech Blog. To read my non technical blog, click here

Saturday, July 12, 2008

The JVM and Tail Recursion

In an attempt to investigate the performance of a trampolined interpreter on the JVM, I discovered something interesting.

First, some background. In essence, trampolining is a strategy used to avoid stack overflow on platforms (like the JVM) that save to the stack even when not necessary , e,g while doing a tail recursive function call. So after doing a CPS transform and then trampolining it by returning a lambda wrapped "thunk", my interpreter *should have* handled an infinite tail recursive call *without* a stack overflow exception. But it failed anyway!

After Dan Friedman pointed out (by email) that "If you use exceptions in Java, you will regain tail recursion. I think that is the only way to get around Java. ", I did some digging and found something even more interesting.

Tail recursive (java) functions are properly handled by some java JVMs (specifically the JITs) and not others! See this article for a good explanation.

Try running Listings 4 and 5 (from the referenced article) on your JVM :-D . I suspect that C# code equivalent to the two listings in the article (numbered 4 and 5) would both run perfectly. Can someone with a Windows machine and dotNet installed let me know if it works? (post a comment here or send me email! Thanks in advance!)

Footnote: the conversion of the tail recursive function (in the article) into the while loop form is one example of how a trampolining interpreter could "break stack build up".

2 comments:

tim said...

Listing 4 overflows the stack using .NET 3.5 and the current Microsoft C# compiler. Amusingly, turning on optimizations just means that you overflow at around 86,000 calls instead of around 14,000. It turns out that tail call optimization is only done by some JITs, just like on the JVM.

The CLR has an intermediate language instruction for hinting that a stack frame can be discarded, but according to the lead developer for Microsoft's C# compiler, 1) it's not very efficient, and 2) tails calls are rarely legal in C# anyway.

Ravi said...

@Tim,
I am offline for the next two weeks or so. Just popped in to say thanks for the informative comment.

Ravi