3 ms·
Not exactly. I'm not familiar with nPth specifically, but it appears it uses swapcontext, which is a lot slower than doing it in an event loop the proper way.
by AnkhMorporkian 11y ago
Not exactly. I'm not familiar with nPth specifically, but it appears it uses swapcontext, which is a lot slower than doing it in an event loop the proper way. Like, 1000 times slower (on Linux)[1]. Having proper generators and extendable event loops is a far cleaner solution to the problem, in my humble opinion. Python's asyncio is perhaps the best implementation of such an event loop I've seen.
1: http://www.crystalclearsoftware.com/soc/coroutine/coroutine/fibers.html http://www.crystalclearsoftware.com/soc/coroutine/coroutine/...
- quotemstr 11y agosetjmp also works fine, and besides, "1,000 times slower" is meaningless if the thing that's 1,000 is not actually the bottleneck.
- AnkhMorporkian 11y agoI'm a bit out of my wheelhouse here, I usually do C and rarely touch C++, and in addition the coding standard I have to adhere to prevents setjmp/longjmp, but doesn't setjmp need to be used in conjunction with longjmp, which makes it completely unsafe to use malloc, fopen, etc? Also, it would prevent RAII from running.
- quotemstr 11y agogetcontext and setcontext are fundamentally about saving and restoring CPU registers. setjmp and longjmp are also about saving and restoring CPU registers, and they (and related functions) give you much more complex of whether you want to save the signal mask. Since saving and restoring the signal mask is the objectionable part of setcontext, setjmp is a decent alternative. If you want to unwind, just unwind. setjmp and setcontext neither help nor hurt you here.
- AnkhMorporkian 11y agoFair enough. I still have doubts about whether that would scale up to millions or billions of coroutines, but I will defer to your knowledge in that it's probably Good Enough™ for most purposes. Thanks for the explanation!
- gpderetta 11y agonPth can use swapcontext if everything else fails, but its preferred portable switching strategy is the so called sigaltstack trick witch is usually significantly faster. BTW, I'm the author of the above page. Coroutine libraries have been my personal hobby since forever.
- srean 11y agoIn that case you might enjoy taking a look at http://felix-lang.org/ http://felix-lang.org/ http://felix-lang.org/$/usr/local/lib/felix/felix-2016.01.04-rc2/share/src/web//tut/fibres_index.fdoc http://felix-lang.org/$/usr/local/lib/felix/felix-2016.01.04... I am not involved in its development.