C++20 coroutines and C++26 fibers: performance of calls and of context switches

169 Views Asked by At

I have been reading this paper about the addition of fiber_context to the C++ standard library: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p0876r15.pdf. I have been researching about (stackless) coroutines and stackful coroutines (aka fibers), and I would like to know what are the performance considerations between both in terms of:

NOTE: I'm assuming the current boost::fiber library is representative enough of whatever is finally added to future C++ standards.

  • Calling/creating: I know that creating a coroutine implies creating a single frame in the heap (whose size is known at compile-time), while creating a fiber implies creating a full stack (with implies deciding an allocator strategy for the stack, decision that is on the user). But besides the stack, what else is needed for creating a coroutine and a fiber respectively?

  • Context switch: that's where I'm not clear about the performance adventages of coroutines versus fibers. In my mind both cases implies some sort of jump plus saving some registers, which doesn't look like a big difference. Is there such a difference between both? What else must be done to context switch between coroutines and fibers respectively, or why they need to store a different set of registers (if that's the case)?

I understand assembly a bit so you can talk in terms of the specific registers that requires to be stored, where there are stored, why, etc. I can bear low-level stuff. If you can do some estimate in terms of CPU-cycles (specially for context-switches), that would be interesting to know too.

To help you be more specific, I'm happy with understanding the "implementation details" for Linux x86-64 only (and I'm also interested to know if there's aspects of implementing fibers that requires OS-support).

0

There are 0 best solutions below