4 ms·
> Anyway: don't use async because it's "more efficient". I don't think efficiency has only one meaning. For instance, you can create millions of coroutines whi
by naasking 4y ago
> Anyway: don't use async because it's "more efficient".
I don't think efficiency has only one meaning. For instance, you can create millions of coroutines while you're limited to only thousands of threads. Doesn't that mean your program is making better use of your hardware and thus is more efficient?
- nmilo 4y agoNo, your processor can only do n things at a time, where n is the number of cores (probably like 8) (simplifying hugely of course). It's pretty easy to fill out 8 cores with 1000s of threads, the coroutines only become useful when almost all of them are blocking on a socket or something, periodically switching on and off to handle it.
- naasking 4y agoYes, but contexts where you're creating a million coroutines are almost certainly I/O bound (like a web server).
- deleted 4y ago[deleted]
- otabdeveloper4 4y agoCoroutines and threads are not that different. Threads usually preallocate some stack space, while coroutines usually don't. But that isn't such a big deal.
- naasking 4y agoThat's not the only difference. Coroutines also typically don't have nearly as many concurrency hazards because you can control when you yield.
- dymk 4y agocoroutines can be either cooperative or not, depending on implementation, so it depends
- gpderetta 4y agoAnd so can threads.
- otabdeveloper4 4y agoThat's an implementation detail. Pthreads work differently, but historically kernel-mode cooperative threads were also popular at one time. (We decided to not go there because cooperative parallelism sucks balls.)
- josephg 4y agoCoroutines also don’t need to flush the TLB when they get scheduled. This is a pretty big deal for performance, when you have a lot of them.
- jlokier 4y agoThread context switches don't flush the TLB either. This is an important performance optimisation in the kernel context switch code.
- jmull 4y agoSure, they can be more efficient in particular situations. But coroutines have overhead (so do threads). Your millions of coroutines might be worse than your thousands of threads in terms of making efficient use of the cpu cores your app or service has available.
- haileys 4y agoYou can actually create millions of threads. Threads are just coroutines managed by the kernel after all.
- naasking 4y agoIf you can create millions of threads with some hardware, then you can create billions of coroutines on that hardware. It's just a fact that you can create 2-3 orders of magnitude more coroutines than you can kernel threads on any given hardware.