6 ms·
Philosophy of Coroutines
- vacuity 3y agoCoroutines really are powerful. Here's to hoping Rust stabilizes generators (full semicoroutines, not the enhanced iteration construct) sooner rather than later. It would give programmers greater control to express state machine logic without being boxed into async/await. I do wonder if the `Context` parameter used for async tasks is appropriate for arbitrary generators, so if someone could chime in on this I would be grateful.
- mratsim 3y agoOr provide continuations and use them to implement coroutines.
- qudat 3y agoA couple of us have been experimenting with deliminited continuations and I think it’s gonna take off soon in TS https://youtu.be/uRbqLGj_6mI?si=kgKKjpCnehJ9bpIG https://youtu.be/uRbqLGj_6mI?si=kgKKjpCnehJ9bpIG https://github.com/neurosnap/starfx https://github.com/neurosnap/starfx https://github.com/thefrontside/effection/tree/v3 https://github.com/thefrontside/effection/tree/v3
- vacuity 3y agoTo my understanding, delimited continuations and coroutines are equivalent in power. Out of curiosity, can delimited continuations be implemented as efficiently (in Rust or otherwise) as stackless coroutines?
- vore 3y agoThese don’t look like continuations to me, just ways to use generators? Continuations would involve reifying the current control flow into a value, which these examples don’t seem to do and still rely on generators to yield control.
- qudat 3y agoThese libraries rely on deliminited continuations: https://github.com/thefrontside/continuation https://github.com/thefrontside/continuation Because of this tiny implementation we are able to express any async flow construct with less code than relying on something like async/await or callbacks.
- smasher164 3y agoSingle-shot continuations in the form of lexical effect handlers would be a good fit for a systems language nowadays. I feel it's a bit too late to staple that onto Rust though.
- pflanze 3y agoI've been using genawaiter[1] for writing generators in Rust. Are there things you can't build on top of async-await? Or is it just that it's simpler without that indirection? [1]: https://crates.io/crates/genawaiter https://crates.io/crates/genawaiter
- vacuity 3y agoI was thinking that the semantics around async/await (function coloring) would make some usages more frustrating. This crate probably sidesteps that, if it is a problem at all. I have a bad habit of not dabbling around when I lack understanding.
- duped 3y agoThis feels circular, since async/await is futures which are generators
- SkiFire13 3y agoI don't think `Context` is appropriate for generic generators, since it has no way to get data in or out of the `async` block. The more general `generator` feature allows a custom context type though.
- vacuity 3y agoI must've looked at an outdated design or something. Thanks for the info.
- krylon 3y agoLua makes coroutines first class citizens. I have only built one toy project with it, but it was very enjoyable. Someone even managed to implement Erlang-style concurrency on top of Lua's coroutines. That was a lot of fun to play with. (/stares into the night, reminiscing/)
- max_k 3y agoIf you like Lua coroutines and C++20 coroutines, I have written a piece of code that allows to combine both: https://github.com/CM4all/libcommon/blob/master/src/lua/CoAwaitable.hxx https://github.com/CM4all/libcommon/blob/master/src/lua/CoAw... It's used for example by our "myproxy" project: https://github.com/CM4all/myproxy/blob/master/src/Connection.cxx https://github.com/CM4all/myproxy/blob/master/src/Connection... (Ctrl-F Lua::CoAwaitable) - a Lua coroutine is launched from within a C++20 coroutine and the C++20 coroutine awaits the Lua coroutine. Everything integrated in a non-blocking I/O event loop.
- krylon 3y agoI am not a C++ person, but that does sound nice. I'm just not ready for the kind of commitment it takes to dive into C++. ;-)
- kazinator 3y agoPoor man's delimited continuations.
- bear8642 3y agoCould you expand? I have a limited understanding of continuations and never used them myself.
- tonyg 3y agoYep. Delimited continuations and proper tail calls gives a superpower. (Even PTC alone...)
- gpderetta 3y agoBut stackful coroutines and (one shot) delimited continuations are equivalent in power and expressivity. Unless you are referring to stackless coroutines or multi-shot continuations.
- kazinator 3y agoNever heard of one shot delimited continuations; it sounds like an euphemism for "broken". Delimited continuations are functions; you can call them as many times as you like, with different arguments. They can call themselves recursively too. Of course when I say "delimited continuation", I mean "delimited continuation" not "broken delimited continuation". (If delimited continuations are used for simulating coroutines, it is my experience that they end up being called only once; each continuation invocation performs a time slice to the next yield. That's a particular simple use of continuations, not a type.)
- exfalso 3y agoI think coroutines are one of those abstractions that are easy to fall in love with, but in reality they do more harm than good. There are extremely few circumstances where coroutines are useful, and those are not the ones most people use them for. Coroutines are not cheap threads, and if they are used as such, your software will end up either leaking resources or contending on resources under any considerable load. A very simple demonstrative is: yes you can have a million coroutines, but you have 10 database connections. No matter how smart you make the scheduler, you will be bound by your IO resources in almost all applications. (This also hints at the few instances where coroutines can be useful: pure compute). The above problem is exacerbated by coroutines literally undoing one of the most powerful abstractions for resource management: RAII. Your resource allocations stop being tied to lexical scope, and a misplaced yield will leak the resource very easily. And even if you know about this, it is quite difficult to do correct resource management or provide proper backpressure, and the resulting code will be unintuitive and difficult to maintain. You'll need to introduce explicit queues/semaphore-like structures in places where a simple threadpool would have sufficed for all your resource management needs. So what are the examples where coroutines are useful? Pure compute. This is very very rare, but for example one could use coroutines to fuse stream processors and create bounded-memory compute. There was also one paper that described how software transactional memory was used to essentially race coroutines to find optimal circuitry layouts. This kind of compute is kind of it. And this is most definitely not what people are using coroutines for. Instead, they throw around "threads are expensive" and treat Rust async like Java futures, completely missing the point. Sidenote: the particular way that Rust async was adopted, and specifically the tokio family of libraries is a whole other hellhole which sidetracked the entire Rust ecosystem, but admittedly this is not the coroutine concept's fault.
- tadfisher 3y agoCoroutines and concurrency are orthogonal. Coroutines can be used as a primitive in concurrent programs, but the core concept is that of a suspended computation; think replacing callbacks with 'async', not replacing threads with coroutines. For example, coroutines work great in GUI code because typically you have an event loop, and you want to schedule work to return a value (or send an event) later on during some future execution of the loop. This can all be done on a single thread without bringing concurrency into the mix. As far as resource contention, I can provide a couple of counter-examples. In Kotlin, if you know you are performing I/O-bound work, you use the "IO" scheduler, which uses an unbounded thread pool to block a thread per-task. In Java, Project Loom makes this altogether unnecessary by yielding IO-blocked virtual threads for you.
- stephc_int13 3y agoI admit that coroutines can make the code simpler to write and also to understand, they can feel natural in some cases to replace complex state machines. But I am not in favor of "magical" abstractions, especially anything related to hidden control flow. I don't have much experience with coroutines but I have the intuition that tracking bugs can be nasty.
- wglb 3y agoPerhaps with experience with coroutines in the right problem environment you might have a different idea. There isn't really anything magical about them. They are a good way to build cooperating sequential processes. My experience is that coroutines not only made it easier to find bugs in interrupt driven code, but it made it easier to avoid them in the first place, as the code in front of you was much easier to understand.
- movpasd 3y agoI'm totally on board with fewer magical abstractions. But I would consider coroutines to be at the same layer of abstraction as simple procedures. Now, you can build coroutine systems on top of procedural semantics (and trivially vice-versa) — indeed this is how many coroutine implementations are built under the hood — and since we're so used to procedural programming it's natural to assume that coroutines are a magical abstraction layer on top of procedures. But from a strictly semantic perspective (disregarding implementation), I think coroutines are best thought of as their own distinct class of non-procedural control flow, as generalisations of procedures. Being generalisations, coroutines provide fewer restrictions, but also fewer guarantees than procedures. So I don't think they're appropriate for all code. Specifically, coroutines kill temporal proximity guarantees: if everything is a coroutine, everything can suspend at any time (to be fair, any procedure can fail to return, but this is rarer). It's similar to exceptions in that respect. So your point about tracking bugs is extremely relevant. With great power comes great responsibility. The current approach is to isolate coroutine functionality from the procedural core (either by very explicitly making them objects like Python generators, or by colouring them and requiring them to be executed in a context like in Kotlin). But I don't think they are quite as magical as they seem, rather just different. (Tangential note: I've always thought it would be interesting to have a language that flipped this, and embedded procedural guarantees within an all-coroutine (or even all-continuations) semantic structure.)
- ioquatix 3y agohttps://github.com/socketry/async https://github.com/socketry/async uses coroutines and I think in general it’s been a great model with very few downsides in practice.
- wglb 3y agoI first used coroutines in the 1970s in Sigma 5 assembler for an application that was in an interrupt-rich environment. Once we tuned our approach, using a macro or two, the code for an process attached to an interrupt could hardly have been clearer. As a result, we were able to build a program to dial out and send text (this was in the days when the data modem and the dialing modem were separate) with no multi-thread errors and only one single-thread error. This was significantly easier than doing coroutines in C, which is allegedly close to the machine. In a later project, we discovered that bliss 36 had a built-in coroutine feature, but we didn't see any advantage to using this in building a compiler.
- xwowsersx 3y agoWhen the author here speaks of coroutines vs explicit state machine, by the latter they are referring to doing things like checking if `currentState == State.LOADING` or setting `currentState = State.COMPLETE`?
- iainmerrick 3y agoMostly the second one. You’ll have a single bit of boilerplate checking the current state and jumping to the right bit of code; but when you’re actually implementing the core logic of your state machine, you’ll be doing lots of “state = do_the_next_thing; return” instead of just calling “do_the_next_thing()” directly.
- xwowsersx 3y agoThanks, when I said "or" there I meant to provide just another example of a state machine type implementation so they weren't to the exclusion of each other..theu were both examples of explicit state machine. Thanks for your point though a lot state = vs return next_thing.
- skybrian 3y agoSetting up pipelines seem fairly easy to do in JavaScript using async/await. Unfortunately, the Streams API is complicated and so are Node’s streams. I ended up inventing my own.
- runald 3y agohttps://www.chiark.greenend.org.uk/~sgtatham/coroutines.html https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html int function(void) { static int i, state = 0; switch (state) { case 0: /* start of function */ for (i = 0; i < 10; i++) { state = 1; /* so we will come back to "case 1" */ return i; case 1:; /* resume control straight after the return */ } } } Huh, I didn't know you could put case statements arbitrarily anywhere inside a switch block in C. Past the initial WTF, it's a nice hack for implementing coroutines.
- jedimastert 3y agoAs I understand it, they're kinda syntactic sugar for goto statements.
- gemstones 3y agoPersonal preferences differ, but I would find this code so much cleaner if it was a goto.
- zoky 3y agoOh yeah, you can toss ‘em in just about anywhere. The most well-known application of this would be Duff’s device: https://en.wikipedia.org/wiki/Duff%27s_device https://en.wikipedia.org/wiki/Duff%27s_device
- grumbel 3y agoThe one big issue I have with coroutines is that they are often impossible to serialize. With a state machine you can just take your state and write it to disk, but with coroutines that state is hidden in the stack of the coroutine and the stack is not a first-class data structure you can access by normal programming language means.
- OskarS 3y agoStackless coroutines are arguably pretty serializable, since they are (usually) implemented by bundling all the relevant data into a struct. There’s no a priori reason that couldn’t be serialized, thiugh I admit I cannot think of an example I’ve seen it used.
- clavigne 3y agoWe use it at work to send FSMs over the network for distributing tasks. Admittedly we always do this with continuations that haven't started yet, but there is no technical reason we couldn't serialize the already started ones (although you'd have to make sure you also send the coroutines you are currently awaiting on.)
- 10000truths 3y agoI think that the biggest missing "killer feature" for systems programming languages is the ability to treat stackless coroutines as just another "plain old data" struct. Here's a list of things I can do with structs that I can't do with C++ coroutines or Rust generator objects (much to my chagrin): * Get their size/alignment at compile time * Allocate them on the stack * Allocate them in a memory region of my choosing * Create a contiguous array of them * Copy them * Serialize/deserialize them Imagine being able to save coroutines to your filesystem, reboot your machine, restart your program, and have it pick up where it left off. Or, migrating lots of actively running coroutines to another machine by serializing it and sending it over the network. Or creating a high-performance, high-concurrency event loop using a giant map of coroutine objects.
- ttctciyf 3y agoMaybe you missed that the article explicitly talks about the cloning/deep copy of coroutines: > Using my C/C++ preprocessor coroutine system, this is perfectly possible. In that system, all the persistent variables of the coroutine – including the state variable that says where to resume from next – have to live in an explicitly declared structure (in C) or be members of a class (in C++). Either way, there’s no difficulty with making an exact copy [...] > After you do that, you’ve got two copies of the coroutine, and each of them will resume from the same part of the code when it next runs [...] This isn’t a deliberate feature of my preprocessor system; it’s just a thing that drops out naturally from the implementation strategy Resuming from an on-disk copy seems tougher - you need to supply all relevant execution context.
- deleted 3y ago[deleted]
- anonymoushn 3y agoYou can do 1-4 with Zig frames, but 5 and 6 are tougher since there's no guarantee that you won't end up with internal pointers.
- 10000truths 3y ago
- vinay_ys 3y agoThis takes me back more than 20 years when I first started to use Putty and learnt about Simon Tatham, and incidentally I learnt about coroutines at the same time from another source and later learned that PuTTY used them!
- samsquire 3y agoThank you for this article. I also really like coroutines but how they manifest in codebases can sometimes be hard to follow (async Rust, async Python). I have played around with protothreads and use generators in Python but Marce Coll has an amazing blog post about coroutines in assembly, which is down right now. If you save the RSP register somewhere and restore it later, you can switch stacks. I'm currently working on an architecture to unify coroutines and threads. I am taking inspiration from "Bulk synchronous parallel programming" in that I am using barriers. I hope to apply structured concurrency to it too. A coroutine can be remote in a thread or in a different current thread and parallel. I am curious about stackless python.
- ithkuil 3y agoIf you literally switch stacks, wouldn't that be a (user space aka green aka lightweight) thread?
- layer8 3y agoSwitching stacks isn’t the difficult part, it’s the related stack memory management.
- samsquire 3y agoCould you explain more and elaborate? The previous coroutine state needs to be recovered onto the stack and registers for the coroutine to function correctly from its previous point. Temporal memoizes API calls and replays the same function deterministically, so it doesn't attempt to jump into half way into a function, that a coroutine could do.
- fanf2 3y agoSwitching stacks is just longjmp(). Memory management for stacks has tricky questions about how much address space to reserve for each stack and how to ensure there are guard pages to catch stack overflows. Or you can YOLO it https://dotat.at/@/2010-01-22-coroutines-in-less-than-20-lines-of-standard-c.html https://dotat.at/@/2010-01-22-coroutines-in-less-than-20-lin...
- drkrab 3y agoCoroutines are great in a single threaded environment. But if you mix them with threads - even behind the scenes - there will be dragons. Erlang solves that problem with proper light weight isolated processes.
- mananaysiempre 3y ago> I talk about generators as if they were the coroutine system in Python, but in fact, they’re one of two: Python also has a completely different kind of thing that it actually uses the name ‘coroutine’ for, in which function definitions start with async def and suspend with await. The async and await system of coroutines in Python is in fact layered on top of the yield system, via an intermediate construct called yield from—which isn’t even technically required, it’s just that implementing it as a loop passing values through from and to the subcoroutine is exceptionally tedious when done carefully[1] and can’t be abstracted as a function call. As a first approximation, await x in Python is equivalent to yield from x.__await__(). It should have been completely equivalent, arguably, but history got in the way. David Beazley has a talk[2] where he walks you through implementing an async runtime (to put it pompously) and you can see how the whole thing hangs together. It’s really quite simple, and in my experience it’s much better than trying to wrap your head around the asyncio docs, because while the language-level foundations are extremely nice and simple, asyncio the stdlib module just kind of sucks. [1] https://peps.python.org/pep-0380/#formal-semantics https://peps.python.org/pep-0380/#formal-semantics [2] https://www.youtube.com/watch?v=Y4Gt3Xjd7G8 https://www.youtube.com/watch?v=Y4Gt3Xjd7G8