Chasing your tail — Matt Godbolt’s blog
xania.org·5h·
Discuss: Hacker News
📥Inlining
Preview
Report Post

Written by me, proof-read by an LLM. Details at end.

Inlining is fantastic, as we’ve seen recently. There’s a place it surely can’t help though: recursion! If we call our own function, then surely we can’t inline…

Let’s see what the compiler does with the classic recursive “greatest common divisor” routine - surely it can’t avoid calling itself?

And yet: The compiler is able to avoid the recursion! When a function ends by calling another function (including itself), the compiler is often able to replace the call with a jump to that function. This is called “tail call optimisation”. If that function is itself, the “jump to the start” is effectively the same as a loop…

Similar Posts

Loading similar posts...