3 ms·
The word performance is a bit misleading here. A key reason to use co-routines is not necessarily maxing out CPUs but IO. Non-blocking IO and co-routines allow
by jillesvangurp 6y ago
The word performance is a bit misleading here. A key reason to use co-routines is not necessarily maxing out CPUs but IO. Non-blocking IO and co-routines allow handling many connections on very modest hardware. Blocking IO and languages that don't handle concurrency very well (scripting languages like ruby or python) deal with this by forking multiple processes, each of which typically can only do one thing at the time. Java historically worked around this by using threads.
Using processes doesn't scale nearly as well and you typically run out of memory before you run out of CPU this way. Using threads like Java does scales a bit better but there are only so many threads you can juggle. Co-routines or green threads combined with non blocking IO is much better and is also becoming common on the JVM where pretty much most modern frameworks support this. And of course if you use Kotlin, co-routines provide a really solid implementation and programming model for this.
This is also the reason for the popularity of go and node.js, which aren't known for their particular ability to get the last bit of performance out of CPUs but do scale rather nicely when it comes to handling non blocking IO asynchronously.
- game_the0ry 6y ago> Co-routines or green threads combined with non blocking IO is much better and is also becoming common on the JVM where pretty much most modern frameworks support this. Appreciate the insightful comment. Basically, the ideal utilization of compute resource (cpu and ram) is engineering for both async I/O and and maximizing cpu threads, correct? I agree, mostly. I can't say I am very confident in understanding / implementing concurrent threading in any language. I am working on trying to get it, but sometimes I don't know if it is worth the mental over head or the added code complexity. Single threaded async alone took me a while to grasp, and I am sure I still have gaps in my knowledge. But mentally modeling a single threaded execution is trivial. I guess I try maximize for my own understanding and efficiency versus maximizing for performance.