Advertisement
Advanced C++, Coroutines

What are the performance and memory considerations of coroutines?

Short answer

Coroutine frames are typically heap-allocated (though the compiler may elide the allocation—HALO optimization—when the lifetime is bounded and visible). Suspension/resumption is cheap versus thread context switches.

Save C++ mock interview
Advertisement
AModel answer
Coroutine frames are typically heap-allocated (though the compiler may elide the allocation—HALO optimization—when the lifetime is bounded and visible). Suspension/resumption is cheap versus thread context switches. Concerns include the allocation cost, potential for many live frames, and ensuring awaited objects/captured references outlive suspension. They excel at high-concurrency I/O without a thread per task.
Real-world example
An async server handles thousands of connections with coroutines instead of thousands of OS threads, saving memory.
Likely follow-ups
  • What is HALO (allocation elision)? | Why are coroutines cheaper than threads for I/O?
Related topics