3 ms·
Being able to guarantee tail calls is a useful optimization in far more cases than code replaceable by loops. I’ve used it myself where dispatch targets themsel
by vgatherps 4y ago
Being able to guarantee tail calls is a useful optimization in far more cases than code replaceable by loops. I’ve used it myself where dispatch targets themselves are dynamic (so can’t be trivially made into a loop) for significant performance gains.
Some other folks who’ve done the same thing (and can post publicly) for code that’s not just “must recurse because loops are for lowly imperative serfs”:
* https://blog.reverberate.org/2021/04/21/musttail-efficient-interpreters.html https://blog.reverberate.org/2021/04/21/musttail-efficient-i...
* http://lua-users.org/lists/lua-l/2011-02/msg00742.html http://lua-users.org/lists/lua-l/2011-02/msg00742.html
* https://cantortrading.fi/rust_decimal_str/ https://cantortrading.fi/rust_decimal_str/
Now does JavaScript really need/care about any of this? Probably not.
- MrBuddyCasino 4y agoInteresting use case, didn't occur to me that tail calls can also just be a performance optimisation technique to help out the compiler and branch predictor. I assumed hot loops could be implemented just as well using GOTOs, but maybe not?
- vgatherps 4y agoTail calls are basically GOTOs, yeah. The bring a HUGE benefit of very clearly defining state flow between gotos which makes the compiler's job super easy.
- blagie 4y agoEverything can be implemented using IF statements and GOTOs. That's how early processors worked, and Turing completeness and all. We don't _really_ need function calls, while loops, or for loops either. That doesn't mean it's a good idea.
- chriswarbo 4y ago> I assumed hot loops could be implemented just as well using GOTOs, but maybe not? Tail calls are GOTOs; that's the whole argument https://apps.dtic.mil/sti/citations/ADA030751 https://apps.dtic.mil/sti/citations/ADA030751
- HALtheWise 4y agoIn the particular linked case of protobuf parsing, a loop with goto's doesn't produce very well optimized code because of specific internal details about how modern C compilers do optimizations. You could certainly imagine a compiler that can fully optimize a go-to heavy program, in which case the code cleanliness argument would be the only reason.