top | item 21816876

(no title)

tempguy9999 | 6 years ago

Utterly dumb question but why is tail recursion necessary in the JVM given that the compiler is better placed simply to turn recursion into a loop. TR removal should be done best at the highest level I'd think.

discuss

order

pron|6 years ago

The compiler can only make this transformation in special cases, in particular, when it doesn't break any of the JVM's semantics (also, not all recursion is self-recursion, i.e. a tail call to the subroutine you're in). You can't just discard a frame of a call in a tail position, because some security mechanisms require knowing the full call-stack (plus, developers might hate you when their stack traces start missing crucial frames). So we're talking about explicit tail calls, in places that can be checked for the safety of the optimization.

chrisseaton|6 years ago

> the compiler is better placed simply to turn recursion into a loop

How do you think it should do this? While keeping the semantics of Java.