DEFINITION
Any function that is in the tail position, meaning the final action be be performed before a function returns
Without Tail Call Optimization (TCO) the callee returns back to the caller and creates a new stack frame and pushes the return address.
With TCO enables, instead of a call instruction the compiler emits a jmp. returns directly to whatever function called , as if it were part of the same function.
What it does:
- reduces stack memory from to when making consecutive tail calls. Reduces stack overflow occurences
callhas more performance overhead thanjmp
Based on a paper: https://dspace.mit.edu/handle/1721.1/5753
The problem is that itβs not guaranteed, so the compiler can go back to an actual call.
See also
- A process in operating systems is a program in execution β TCO works directly on the stack section of the process memory layout: without it, n consecutive tail calls push n frames; with it, the stack stays flat
- Linear Recursion is a chain of deferred operations β TCO is what converts a linear recursive process into an iterative one: the chain of deferred operations collapses to a single frame because each call jumps rather than nesting
- Functional Programming programs are trees of expressions, not sequences of steps β functional programming relies heavily on recursion instead of loops; TCO is what makes this viable at scale without stack overflow
- A Context Switch Saves the PCB of the Running Process and Restores Another β TCO reduces the number of stack frames that need saving during a context switch; fewer frames means cheaper context switches in recursive call chains
- Prefer sequential memory access, CPUs predict and prefetch based on locality β a flat stack (O(1) frames) is more cache-friendly than a deep stack (O(n) frames): fewer cache lines needed, more predictable memory access pattern