Stack Switching: The Hidden Cost of Stackful Coroutines
Coroutines let you pause and resume execution, but the way they manage their call stack determines their memory footprint, context-switching cost, and what kind of code they can run. Stackful coroutines preserve the entire call stack across suspension points, which means they need explicit stack switching machinery. That machinery isn’t free, and understanding its cost explains why language designers increasingly favor stackless alternatives.
What Stack Switching Actually Does
When a stackful coroutine yields control, the runtime must save the current stack pointer, frame pointer, and return address, then restore the stack context of whatever coroutine runs next. This is conceptually similar to thread context switching, but it operates entirely in user space without kernel involvement. The coroutine’s stack lives in a pre-allocated memory region, and switching means loading a new set of CPU registers that point to a different stack.
Unlike stackless coroutines, which only save local variables and resume points, stackful coroutines preserve the entire chain of function calls. That means you can suspend from deep inside nested function calls without any special annotation. The runtime captures the full call stack state, including return addresses and frame pointers, by manipulating the stack pointer register directly.
The Memory Tradeoff
Each stackful coroutine needs its own stack, typically allocated upfront. A common default is 64KB to 1MB per coroutine, which works for thread-replacement scenarios but becomes prohibitive when you want millions of lightweight tasks. You can shrink stack sizes, but too small and you risk stack overflow; too large and you waste memory on coroutines that barely use their allocation.
Some implementations use segmented or split stacks that grow on demand, but these introduce their own overhead. Every function call must check whether there’s enough stack space remaining, adding a branch to the hot path. When growth is needed, the runtime allocates a new segment and adjusts stack pointers, which can be slower than a simple function prologue.
Contrast this with stackless coroutines, which allocate only the exact state needed for suspension—often just a handful of bytes. The compiler knows precisely which variables must survive across suspension points and packs them into a heap-allocated coroutine frame. There’s no per-coroutine stack, so you can comfortably run millions of them.
Context Switch Mechanics
The actual stack switch involves saving and restoring a set of registers: stack pointer, frame pointer, instruction pointer, and any callee-saved registers the ABI requires. On x86-64, that’s typically six to fourteen registers depending on calling convention. On ARM64, it’s similar. The runtime uses architecture-specific assembly or platform APIs like ucontext or SwitchToFiber to perform the swap.
This register shuffling is fast compared to a kernel thread switch—no syscall, no TLB flush, no scheduler interaction—but it’s not zero. Each context switch costs dozens to hundreds of cycles depending on cache warmth and microarchitecture. For high-frequency switching scenarios, like cooperative schedulers that yield every few microseconds, this overhead compounds.
Cache and TLB Implications
Stack switching doesn’t just move register state; it redirects memory access patterns. Each coroutine’s stack occupies a different memory region, so switching coroutines can evict stack data from L1 cache. If coroutines yield frequently and have large working sets, you effectively trash the data cache on every switch.
The TLB impact is usually minimal since stacks typically fit within a few 4KB pages, but segmented stacks or very large coroutine stacks can increase TLB pressure. The bigger issue is that stack-heavy coroutines lose spatial locality—data that was hot in L1 for one coroutine is cold when you resume a different one.
Why Runtimes Still Use Them
Stackful coroutines persist because they support unrestricted suspension. You can call a legacy function that calls another function that yields, without annotating the entire call chain. This makes them attractive for embedding coroutine support into existing codebases or for scenarios where you need to wrap third-party libraries.
Languages like Lua and some Scheme implementations default to stackful coroutines for this reason. Runtimes like Go use their own flavor with growable stacks to balance memory efficiency and call-depth flexibility. The tradeoff is acceptable when coroutine counts stay in the thousands and the convenience of unrestricted yielding outweighs the memory and switching cost.
The Shift Toward Stackless
Modern async runtimes in Rust, C++20, JavaScript, and Python use stackless coroutines precisely to avoid stack switching overhead. The compiler transforms suspension points into state machines, eliminating the need for separate stacks entirely. You lose the ability to suspend from arbitrary call depth, but you gain near-zero memory overhead and context switches that are just indirect jumps.
The annotation burden—marking async functions explicitly—is the price you pay for this efficiency. In practice, most new codebases converge on async-all-the-way-down designs where the annotation cost is front-loaded during initial development, then amortized across the lifetime of the codebase.
Stack switching remains a powerful tool, but its costs shape where and how stackful coroutines fit into the concurrency landscape. When you need deep call stack preservation or can’t refactor for async coloring, stackful coroutines deliver. When you need millions of tasks or microsecond-scale switching, stackless wins.