by gpderetta · 5y ago · 0 comments
After a lot of back and forward, the committee decided to pick the most proposal (i.e. the MS one) with the most complete implementation and potentially less ambitious. These are both good good features, but we ended up with a very complex, hard to use, design that requires unspecified compiler magic to remove all overhead.
Interestingly enough the committee went in the opposite direction for networking recently rejection the battle tested ASIO, again from chriskohlhoff, for a yet unproven design initially from Facebook, now Nvidia.
edit: personally I'm a huge fan of stackfull coroutines, but I could have lived with zero overhead stackless coroutines. The current design is IMHO the worst solution. Then again, sometimes it is better to ship something than argue forever.
Facebook is very invested in coroutines, all major internal libraries at this point have a coroutine interface (in particular Thrift auto-generates coro client interfaces), and they have enjoyed wide adoption, way beyond the small cabal of C++ gurus.
Personally what sold me is cancellation handling. It really is magical.
Are your concerns about frame allocation? Is this something that the nanosecond-latency crowd particularly cares about? :)
My issue is stackless coroutines would be perfect for generators but the required allocation kind of kills it :(.
My experience with fibers is that they're stack overflow generators. Presumably you want lots of them running concurrently, so you'll need a small stack size, and now you have to be extremely careful about what code you run in them as it's very easy to run out of stack.
You can't rely on overcommit because if you actually end up touching the pages you're stuck with them.
> My issue is stackless coroutines would be perfect for generators but the required allocation kind of kills it :(.
That's true, though it depends on the granularity of the operations you want to run in your generators. Ranges+algorithms should cover most of the simple/small cases?
Having said that, I think that full coroutines can be no worse than stackless coroutines for stack usage. There are multiple options, including segmented stacks (which do have performance and ABI implications of course).
My preferred solution would be to have first class continuations and have the compiler convert them to stackless coroutines if the calling continuation never escapes the top level frame. In fact even yielding from inlined, non recursive functions would still allow the conversion to be performed while giving better ergonomics than the current implementation. The trick here is how to extend the language in such a way that this can be a guaranteed optimization instead of QoI. If this can be made to work, I think the same exact optimization could remove the frame allocation for stackless coroutines. Unfortunately I suspect this might require adding the equivalent of rust lifetimes to C++.
You can still avoid overflows even on a pure library implementation without compiler helps by switching to the main thread continuation (and its stack) for any stack consuming operation (or any operation that is known not to context switch). This can be extremely cheap, just an additional register swap over the normal function calling convention.
YMMV.
They should have stayed away from ASIO. It looked like they were going to, for a while, and it looked like it was going to be better than boost's implementation. But here I find out that was never the case and the portions that needed filled in required compiler support.
But straight ASIO is pretty bad.
Edit: btw, I'm a huge fan of Eric work and he was my GsoC mentor back in '06. It is just sad that we got into an either-or situation and we couldn't get Net TS in the standard.
Chris proposal was "just" syntactic sugar over that. He went so far as producing a working implementation on a clang branch. The most important difference to what was eventually standardized is that the coroutine object is a non type erased value type (basically very similar to a lambda, and in fact an extension on top of it). The big issue is that it was very hard to use safely as the address of local variables could potentially change between invocations if the coroutine is copied around.