Hacker News new | ask | show | jobs
by vgatherps 1458 days 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-i...

* http://lua-users.org/lists/lua-l/2011-02/msg00742.html

* https://cantortrading.fi/rust_decimal_str/

Now does JavaScript really need/care about any of this? Probably not.

1 comments

Interesting 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?
> 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

Everything 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.

In 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.
Tail 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.