Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java 8 have tail call optimization?

I tried digging on the web to get my question answered. I found some documents related to Project DaVinci. This is tagged to the JSR 292 which is related to including closures in the JVM. Did this project get realized and is it a part of Java 8?

like image 392
Abhijeet Kushe Avatar asked Apr 04 '14 15:04

Abhijeet Kushe


People also ask

Does Java allow tail recursion?

Tail recursion is a compile-level optimization that is aimed to avoid stack overflow when calling a recursive method. For example, the following implementation of Fibonacci numbers is recursive without being tail-recursive. The Scala compiler has a built-in tail recursion optimization feature, but Java's one doesn't.

Why does Java not support tail recursion?

Java as a language is unlikely to ever automatically optimize tail-calls. This is because most implementations of tail-call elimination have somewhat unintuitive effects on the callstack (eg. if you throw an exception in a recursive call, you won't see all of the recursive calls in the stack trace).

Is tail recursion faster in Java?

Tail-recursive functions are considered better than non-tail-recursive functions — the compiler can easily optimize the tail-recursive function as there is nothing left to do in the current function after the recursive call. Hence, the function's stack frame need not be saved.

Does V8 have tail call optimization?

Tail-call optimization is a part of the ES2015-ES6 specification. Supporting it isn't a NodeJS thing, it's something the V8 engine that NodeJS uses needs to support. Node 7.10 down to 6.5.


1 Answers

As far as I know Java 8 does not have tail call optimization. Afaik it isn't related to the actual compiler trick, because that one is simple, but to preserve a callstack for security purposes. But I guess it would be possible with a bytecode rewriter.

like image 104
pveentjer Avatar answered Oct 05 '22 10:10

pveentjer