Thursday, April 27, 2017

Tail Call Elimination

Jordan Morgan:

The best part is, how it works isn’t magical: It just prevents the compiler from pushing more frames, thus preventing more stack growth. Why not let the callee reuse the frame stack of the caller instead of popping another one on?


That said, tail call elimination is not mutually exclusive to recursion — though it’s a case study for its benefits. In fact, in Apple’s sample code in their WWDC session mentioned above, drawRect: reaps its rewards even though it’s not an inherently recursive function.


In particular, in our world as iOS developers it can also happen if[…]


There are some use cases to disabling the magic here if you’d so choose. Namely, for the sake of profiling — you’ll get a true, clean stack trace by turning this off.

Kay Butter:

Tail Call Elimination is not always easy to rely on though with ARC inserting release or autorelease calls after what should be a tail call :/

Comments RSS · Twitter

Leave a Comment