PERFORMANCE Signal 497
Tail-call optimization in C is relatively recent
Illustration only Photo by U. Storsberg on Unsplash
GCC and Clang now support tail-call optimization for indirect calls in C, enabling interpreter dispatch patterns that were previously impractical with computed goto approaches.
Engineers building VMs or interpreters can now use tail-call-based dispatch to scale to hundreds of thousands of specialized code snippets, rather than being limited to roughly 2,000 with computed goto. This unlocks optimization techniques like aggressive superinstruction formation that require many more code variants than goto*-based systems can practically handle.
Written by elseif from the cluster below · every claim links back to a sourceThe three things worth knowing
C's traditional calling convention required the caller to remove arguments from the stack, preventing tail-call optimization because code had to execute between the call and return.
GCC and Clang now handle tail-call optimization including indirect calls, which was not supported when Probst first implemented it in GCC in 2001.
Copy-and-patch compilation can use approximately 100,000 code snippets with tail-call optimization, compared to fewer than 2,000 practical in goto*-based dispatch systems like Gforth.
THE CLUSTER