Live data from Hacker News

Comparison of Rust async and Linux thread context switch time and memory use

github.com

1–10 of 201 posts

Re: Comparison of Rust async and Linux thread context switch time and memory use

#6
post #4

Quickest 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

#7
I 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_wait() or something similar. With blocking I/O, you call read(), and the kernel wakes up your thread when the read() completes. With non-blocking I/O, you call read(), get EAGAIN, call epoll_wait(), the kernel wakes up your thread when data is ready, and then you call read() a second time.

In both scenarios, you’re calling a blocking system call and waking up the thread later.

Of course, there are scenarios when epoll_wait() returns multiple events, which reduces the number of context switches. But the general result is that it’s not always easy to beat blocking I/O and kernel threads.

Re: Comparison of Rust async and Linux thread context switch time and memory use

#9
post #7

I 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.

Re: Comparison of Rust async and Linux thread context switch time and memory use

#10
> 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 Rocket which relies on kernel context switching.

But if Rust async and kernel thread has the same switch time as shown by this benchmark, then why is actix-web so much faster than Rocket?

[1] https://www.techempower.com/benchmarks/#section=data-r20&hw=...

Post reply on HN