Quickest summary: Rust async is >3x faster and lighter than Linux threads. This is a great accomplishment for Rust.
Comparison of Rust async and Linux thread context switch time and memory use
11–20 of 201 posts
Re: Comparison of Rust async and Linux thread context switch time and memory use
#12I am not surprised that the cost of context switching due to I/O readiness can often be roughly equal between async tasks and kernel threads. Normal blocking I/O can be surprisingly efficient because of various factors, such as a reduced need for system calls. Think about it this way—if you have a user-space thread which wakes up due to I/O readiness, then this means that the relevant kernel thread woke up from epoll…
io_uring will help a lot here.
What io_uring does do is provide a way to poll without needing to wait, but if you haven’t received new events when you poll, you’re not on the fast path any more. Whether you are often on the fast path for io_uring will depend on the particulars of your application and its I/O patterns.
Re: Comparison of Rust async and Linux thread context switch time and memory use
#13> A context switch takes around 0.2µs between async tasks, versus 1.7µs between kernel threads. But this advantage goes away if the context switch is due to I/O readiness: both converge to 1.7µs. This is a big surprise. If you look at the Techempower web benchmark [1], the performance of actix-web is about 20x higher than that of Rocket. The common explanation is that actix-web is async and hence much faster than Roc…
Re: Comparison of Rust async and Linux thread context switch time and memory use
#14Re: Comparison of Rust async and Linux thread context switch time and memory use
#15Quickest summary: Rust async is >3x faster and lighter than Linux threads. This is a great accomplishment for Rust.
It sound to me like comparing apples and oranges though. Parallelism (threads) and concurrency (aysnc in Rust) are not the same thing and can be actually used in combination.
Re: Comparison of Rust async and Linux thread context switch time and memory use
#16I am not surprised that the cost of context switching due to I/O readiness can often be roughly equal between async tasks and kernel threads. Normal blocking I/O can be surprisingly efficient because of various factors, such as a reduced need for system calls. Think about it this way—if you have a user-space thread which wakes up due to I/O readiness, then this means that the relevant kernel thread woke up from epoll…
So the whole thing is a bit of a false equivalence. Better interfaces that reduce context switches are desirable, but even where they exist often you are just substituting a user thread for a kernel one, and in the general case, there is likely to always be system interfaces that never make it into the brave new world -- take SysV IPC for example (a 1975 era API), it seems doubtful anyone would put the effort into making it async, but there will probably still be times where you might want to consume those interfaces for compatibility or some other obscure reason.
Also consider the case where a user program has a need for some substantial thread pools of its own, it might be the case in some scenarios that reusing resources that must already exist in user space and live in warmed caches makes more sense. Neither async or Linux threads are "better", it will always depend on a particular use case, and even then the right answer might well be some combination of both.
Re: Comparison of Rust async and Linux thread context switch time and memory use
#17I am not surprised that the cost of context switching due to I/O readiness can often be roughly equal between async tasks and kernel threads. Normal blocking I/O can be surprisingly efficient because of various factors, such as a reduced need for system calls. Think about it this way—if you have a user-space thread which wakes up due to I/O readiness, then this means that the relevant kernel thread woke up from epoll…
And among Rust frameworks the same pattern holds. The fastest Rust frameworks are async while a synchronous frmework such as Rocket is about 20x slower.
[1] https://www.techempower.com/benchmarks/#section=data-r20&hw=...
[2] https://www.techempower.com/benchmarks/#section=data-r20&hw=...
Re: Comparison of Rust async and Linux thread context switch time and memory use
#18Re: Comparison of Rust async and Linux thread context switch time and memory use
#19This is not at all a fair comparison unless you're using io_uring.