Tail calls again and again

4 views
Skip to first unread message

Rémi Forax

unread,
Dec 18, 2009, 2:18:22 PM12/18/09
to jvm-la...@googlegroups.com
I've written an entry on how to enable tail call on JVM:
http://weblogs.java.net/blog/forax/archive/2009/12/18/tailcall-anyone

I have do some tests and it works very well :)

R�mi


Charles Oliver Nutter

unread,
Dec 18, 2009, 2:34:36 PM12/18/09
to jvm-languages
Ship it!

> Rémi
>
>
> --
>
> You received this message because you are subscribed to the Google Groups "JVM Languages" group.
> To post to this group, send email to jvm-la...@googlegroups.com.
> To unsubscribe from this group, send email to jvm-language...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/jvm-languages?hl=en.
>
>
>

fo...@x9c.fr

unread,
Dec 18, 2009, 2:50:28 PM12/18/09
to jvm-la...@googlegroups.com, fo...@x9c.fr


Great piece of news !

Sorry to ask such a naive question, but why is it the responsibility
of the bytecode emitter to mark a call as "tail" ?
It seems fairly easy for HotSpot to decide whether a call is tail or not.

Does this mean that some language implementers would like to have
their calls unoptimized even when they could be ? The only reason I
can think of is to have full stack trace. However, in this case, I would
expect to just have a "-XX" flag to disable tailcall optimization when
one wishes to have full stack traces.


Xavier Clerc

Patrick Wright

unread,
Dec 18, 2009, 3:01:08 PM12/18/09
to jvm-la...@googlegroups.com
> Sorry to ask such a naive question, but why is it the responsibility
> of the bytecode emitter to mark a call as "tail" ?
> It seems fairly easy for HotSpot to decide whether a call is tail or not.
>
> Does this mean that some language implementers would like to have
> their calls unoptimized even when they could be ? The only reason I
> can think of is to have full stack trace. However, in this case, I would
> expect to just have a "-XX" flag to disable tailcall optimization when
> one wishes to have full stack traces.

see http://blogs.sun.com/jrose/entry/tail_calls_in_the_vm, "hard"
versus "soft" tail calls.

fo...@x9c.fr

unread,
Dec 18, 2009, 3:22:58 PM12/18/09
to jvm-la...@googlegroups.com

Thanks for the pointer.
If I understand correctly, tailcall information will be emitted by the compiler
for the sole purpose of being sure that HotSpot will actually perform the
optimization (and raise an exception if not).

While ensuring that the optimization is actually performed is very important,
it seems to be a novelty in the JVM: to the best of my knowledge, there is no
other way to guarantee that any optimization will actually occur.
Is this correct ?


Xavier Clerc

Per Bothner

unread,
Dec 18, 2009, 3:28:10 PM12/18/09
to jvm-la...@googlegroups.com, fo...@x9c.fr
On 12/18/2009 12:22 PM, fo...@x9c.fr wrote:
> While ensuring that the optimization is actually performed is very important,
> it seems to be a novelty in the JVM: to the best of my knowledge, there is no
> other way to guarantee that any optimization will actually occur.
> Is this correct ?

Tail-call optimization is not a pure optimization, as it changes the
semantics in a fundamental way: It removes stack traces, which are
part of both observable behaviour (stack traces) and the Java security
architecture. Conversely, the lack of tail-call optimization when it
is expected (or demanded by some [non-Java] language specification)
means that a program that should run in finite memory will run out of
stack.
--
--Per Bothner
p...@bothner.com http://per.bothner.com/

Patrick Wright

unread,
Dec 18, 2009, 3:30:10 PM12/18/09
to jvm-la...@googlegroups.com
> Thanks for the pointer.
> If I understand correctly, tailcall information will be emitted by the compiler
> for the sole purpose of being sure that HotSpot will actually perform the
> optimization (and raise an exception if not).
>
> While ensuring that the optimization is actually performed is very important,
> it seems to be a novelty in the JVM: to the best of my knowledge, there is no
> other way to guarantee that any optimization will actually occur.
> Is this correct ?

What I can say is just based from trying to follow the various
discussions here. This "optimization" allows you to have repeated tail
calls without blowing out your stack. Without this "optimization", you
have a serious (and unpredictable) hard limit on how deep you can
recurse. With the optimization, you don't (all else being equal). So
the issue here is that language designers/implementors and programmers
know they can use tail calls without running into a wall.

This is different from other "optimizations" we usually talk about in
the JVM. And it's not just HotSpot, though the current patch is a
HotSpot patch. The idea would be to make the bytecode, and its
interpretation, a standard feature of any compliant JVM.

John Cowan

unread,
Dec 18, 2009, 4:46:42 PM12/18/09
to jvm-la...@googlegroups.com, fo...@x9c.fr
On Fri, Dec 18, 2009 at 3:28 PM, Per Bothner <p...@bothner.com> wrote:

> Tail-call optimization is not a pure optimization, as it changes the
> semantics in a fundamental way:  It removes stack traces, which are
> part of both observable behaviour (stack traces) and the Java security
> architecture.  Conversely, the lack of tail-call optimization when it
> is expected (or demanded by some [non-Java] language specification)
> means that a program that should run in finite memory will run out of
> stack.

Indeed. We need to be careful to distinguish between tail-calling as
an optimization and proper tail calling as a language feature. We
also need, while I'm at it, to distinguish between mere tail recursion
and generalized tail calling. I've been hammering on these
terminological confusions lately; it's all too easy for people, even
people who know better, to write "TCO" when they mean "proper tail
calling".

--
GMail doesn't have rotating .sigs, but you can see mine at
http://www.ccil.org/~cowan/signatures

fo...@x9c.fr

unread,
Dec 18, 2009, 6:21:02 PM12/18/09
to jvm-la...@googlegroups.com, fo...@x9c.fr

I do wholeheartedly agree ; indeed for (direct) tail recursion, we need
nothing new: a goto with the first instruction of the method as the destination
does the trick.


Xavier Clerc

John Cowan

unread,
Dec 18, 2009, 7:25:04 PM12/18/09
to jvm-la...@googlegroups.com, fo...@x9c.fr
On Fri, Dec 18, 2009 at 6:21 PM, fo...@x9c.fr <fo...@x9c.fr> wrote:

> I do wholeheartedly agree ; indeed for (direct) tail recursion, we need
> nothing new: a goto with the first instruction of the method as the destination
> does the trick.

Unless you are generating Java source rather than bytecode! Hence my
plea for goto support in Janino.

Rich Hickey

unread,
Dec 20, 2009, 11:38:03 AM12/20/09
to JVM Languages

All points granted, but "proper" isn't that great either.

Rich

Reply all
Reply to author
Forward
0 new messages