Re:Show HN: Tail Recursion Optimization for the JVM
by anonymoushn on Thursday April 23, 2020 @01:03AM
(#22952974)
Lua's stack trace includes an indication that 1 or more tail calls happened, but not how many
$ cat tailrec.lua
function f(g,n)
if n == 0 then
error("oh no")
end
return g(g,n-1)
end
f(f,5)
$ lua tailrec.lua
lua: tailrec.lua:3: oh no
stack traceback:
[C]: in function 'error'
tailrec.lua:3: in function 'f'
(...tail calls...)
tailrec.lua:7: in main chunk
[C]: in ?
Re:Show HN: Tail Recursion Optimization for the JVM
by saagarjha on Wednesday April 22, 2020 @07:14PM
(#22951212)
JavaScriptCore keeps track of frames, even for JavaScript proper tail calls: https://webkit.org/blog/6240/ecmascript-6-proper-tail-calls-...
Re:Show HN: Tail Recursion Optimization for the JVM
by chrisseaton on Wednesday April 22, 2020 @08:42PM
(#22951713)
The essential words in that article are...
> There are some non-standard ECMAScript features in JavaScript that work differently in the presence of PTC.
Java has a strict spec, and the relevant methods which would break aren’t non-standard like they are in JavaScript.
If you’re willing to change the spec (I think Loom does) then yeah, but you can’t implement it as an ‘optimisation’ until then, because it’s not an optimisation if it changes behaviour.
Re:Show HN: Tail Recursion Optimization for the JVM
by saagarjha on Wednesday April 22, 2020 @11:37PM
(#22952589)
What would Java's be? Is there an interface besides Thread.getStackTrace()?
Re:Show HN: Tail Recursion Optimization for the JVM
by chrisseaton on Thursday April 23, 2020 @05:04AM
(#22954088)
It’s also the format of the backtrack - I think that’s covered by the TCK.
The whole idea with tail call elimination is that a stack trace shouldn't be able to see the frames for the tail calls, because the tail call reuses the frame. To add support for tracking the tail call stack frames, you would need to modify the compiler to output code that specifically keeps track of "elided tail call frames", and you would need to update the stack trace traversal code to be able to recognize the extra debug information.
Sure, it's something you could build into a new toolchain, but adding something like this to the JVM would be harder due to the constraints already placed on the JVM. Furthermore I don't know of such support in any toolchains, not even for Lua or Scheme, both of which guarantee tail call optimization. (If anybody has an example, please share!)