Follow

true co-routines in C 

@amiloradovsky@functional.cafe as far as a know there is no stack in C/C++, the compiler can and will optimize your call graph.

Here is an example:
ix.io/2GE1/cpp
Compiling this in separate translation untis with optimizations and disassembling with objump, I see a call to two from main, then jumps from two to one and back, until in one of them the condition trips and it returns to main. The optimizations should work with c++ style callbacks as well, as long as they are known at compile time. For c style entirely runtime callbacks, you'll need new syntax, but then again you shouldn't be using those unless you are defining an ABI, which would be one insane leaky intrusive ABI, making such leaps of faith.

What people usually call coroutines, I would explains as state machines disguised as function, and in context of this kind of micro-optimizations I think they would be much more complex beasts, subject to many different types of optimizations. C++20 has them, for what it's worth.

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.