HeadlinesBriefing favicon HeadlinesBriefing.com

Tail‑Call Optimization in C Is Recent

Hacker News •
×

Tail‑call optimization in C is a relatively recent development640. Historically the C calling convention left argument cleanup to the caller, so a callee could not simply return in place of the caller; the call became a non‑tail call. In 1994 I inspected the logos of that era’s compilers and found no support for the kind of tail calls shown in the article. By 2001 Mark Probst added a separate calling convention to GCC; he noted in section 6.4 that the optimization could not handle indirect calls, a limitation that would hinder interpreter dispatch. The optimization also required a separate calling convention, which was a significant design change.

Last year I read Xu and Kjolstad’s “Copy‑and‑Patch Compilation” paper, which relies on tail‑call optimization. Their experiments involve 100,000 code snippets, while our Gforth implementation currently handles fewer than 2000 VM‑instruction patterns. After running a series of tests, I discovered that both GCC and Clang can perform the required tail calls. The ability to generate a larger number of snippets would unlock techniques that are impractical with a goto *‑based system. This demonstrates that modern compilers have matured to support such optimizations natively.

I have not revisited the topic since; GCC’s goto * was sufficient for many cases, but recent performance notes mention sibcalls, prompting a re‑evaluation. We have yet to integrate these improvements into Gforth, so kudos go to the Python community for arriving at this optimization first.