4 ms·
So, does it have tail calls yet?
by ScottBurson 8y ago
So, does it have tail calls yet?
- chrisseaton 8y agoYes you can implement tail calls using exceptions.
- keymone 8y agowhat would performance look like?
- chrisseaton 8y agoIf you throw and catch an exception in the same compilation unit it just turns into a jump instruction, so fine.
- keymone 8y agoDidn’t know that, neat!
- ScottBurson 8y agoIf I understand what you're saying, you're suggesting that a single self-tail-recursive function can be rewritten using an exception thrown and caught within the function. That's something, but what about a set of mutually tail-recursive functions? Of course one can always use a trampoline, but then you have to pay the cost of consing thunks. It would still be worthwhile to have true tail-call instructions in the bytecode.
- Veedrac 8y agoMy understanding is that Graal's compilation units are basically an amalgamation of all of the hot code in an area; function boundaries don't really come into it in the same way they do in C. If you're reliably tail recursing, that's going to be in the same compilation unit. This image might make it clearer: https://www.researchgate.net/profile/Chris_Seaton/publication/285051808/figure/fig3/AS:614311105929231@1523474589199/Self-optimising-ASTs-in-Truffle-114.png https://www.researchgate.net/profile/Chris_Seaton/publicatio...
- chrisseaton 8y agoTruffle frames are just Java objects, so when the exception returns us to the root of the method, we can create a new frame to run an entirely different method. We aren't looking to modify Java, so we aren't the people to ask for a tail-call instruction in the bytecode.
- ScottBurson 8y agoOh, you're heap-allocating frames? You know what I'm going to ask about next, right? First-class continuations :-)