636
STOP DOING ASYNC
(lemmy.world)
Post funny things about programming here! (Or just rant about your favourite programming language.)
I honestly don't know, why async got so popular. It seems like the entire reason for doing it was that some blokes were hellbent on using JS on a server and couldn't fathom that a second thread might be a good idea.
Async is good because threads are expensive, might aswell do something else when you need to wait for something anyways.
But only having async and no other thread when you need some computation is obviously awful.. (or when starting anothe rthread is not easily manageable)
Thats why i like go, you just tell it you want to run something in parallel and he will manage the rest.. computational work, shift current work to new thread.. just waiting for IO, async.
The "do something while waiting for something else" is not a reason to use async. That's why blocking system calls and threads exist.
Threads don't need to be expensive. Max stack usage can be determined statically before choosing the size when spawning a thread.
Any other reasons?
If you compare the performance of async rust vs. rust with blocking syscalls there's not even a comparison. 'epoll' and the like are vastly more performant than blocking system io, async then it simply a way to make that kind of system interface nice to program with as you can ignore all that yielding and waking up and write straight-line code.
Now, if all you do is read a config file, yes, all that is absolutely overkill. If you're actually doing serious io though there's no way around this kind of stuff.
I assume by performance you mean CPU usage per io request. Each io call should require a switch to the kernel and back. When you do blocking io the switch back is delayed(switch to other threads while waiting), but not more taxing. How could it be possible for there to be a difference?
Because the kernel doesn't like you spawning 100k threads. Your RAM doesn't, either. Even all the stacks aside, the kernel needs to record everything in data structures which now are bigger and need longer to traverse. Each thread is a process which could e.g. be sent a signal, requiring keeping stuff around that rust definitely doesn't keep around (async functions get compiled to tight state machines).
Specifically with io_uring: You can fire off quite a number of requests, not incurring a context switch (kernel and process share a ring buffer) and later on check on the completion status quite a number, also without having to context switch. If you're (exceedingly) lucky no io_uring call ever cause a context switch as the kernel will work on that queue on another cpu. The whole thing is memory, not CPU, bound.
Anyhow, your mode of inquiry is fundamentally wrong in the first place: It doesn't matter whether you can explain why exactly async is faster (I probably did a horrible job and got stuff wrong), what matters is that benchmarks blow blocking io out of the water. That's the ground truth. As programmers, as a first approximation, or ideas and models of how things work are generally completely wrong.
Well too bad cause they are.
Go ahead and spin up a web worker and transfer a bunch of data to it and tell us how long you had to wait.
The only way I have heard threads are expensive, in the context of handling many io requests, is stack usage. You can tell the os to give less memory (statically determined stack size) to the thread when it's spawned, so this is not a fundamental issue to threads.
Time to transfer data to one thread is related to io speed. Why would this have anything to do with concurrency model?
Well I just told you another one, one actually relevant to the conversation at hand, since it's the only one you can use with JavaScript in the context of a web browser.
You cant say async is the fundamentally better model because threading is purposely crippled in the browser.
The conversation at hand is not "how do io in browser". Its "async is not inherently better than threads"
No, because async is fundamentally a paradigm for how to express asynchronous programming, i.e. situations where you need to wait for something else to happen, threading is not an alternative to that, callbacks are.
Threads are callbacks.