Hacker News new | ask | show | jobs
by jeffbee 6 days ago
Every cursed rust application I've been hired to fix has been diagnosed as spending 90%+ of its time pointlessly jumping tokio threads because someone thought that async means fast. I give these 10x throughput boosts by removing concurrency or, sometimes, just rewriting them in Go.
4 comments

You're talking about concurrency and I'm talking about parallelism. :-)

Rust's async situation is still a pain in many ways.

But throwing Rayon at something often results in very reasonable parallel performance gains without cluttering things much. It usually won't get you to an optimal solution, but it's good for 80% with 10% of the pain.

I interpreted GP as talking about maintaining Rust code that was written with performance in mind by people who were not familiar with the difference, and who shot themselves in the foot as a result.
It depends on what you are going for: service latency, or throughput. Rayon's work stealing is inappropriate for any throughput oriented work and questionable for the majority of latency sensitive services. It does have a niche but it's not the first thing I would reach for. Unfortunately it IS the first thing Claude reaches for.
That's why the rust server I work on is an unholy mix of threads and tokio...

The performance oriented (data plane) work happens on dedicated, cpu-pinned threads and sockets outside the tokio runtime. The control plane work happens in tokio async. Interaction between the two is tricky. But decreased latency and increased throughput is valuable. And it's a complex server, so we can't really try too many approaches.

It certainly doesn't help that the data structures are, by necessity, a cephalopod that have to be used across threads.

Rust makes it pretty hard to do fast async, IMO.

Everything wants to push you down the path of using `ARC<Thing>` even when `Thing` is immutable or the lifetime can be mostly guaranteed.

Here's an example of that problem. Let's say I want to do 2 things concurrently against the same data set. I might want to write code that looks like this:

    let v = vec![1, 2, 3];
    let taskA = tokio::spawn(async {
        println!("Here's a vec: {:?}", v);
    });
    let taskB = tokio::spawn(async {
        println!("Here's a vec: {:?}", v);
    });
    tokio::join!(taskA, taskB);
But that immediately gets you in hot water because "async block may outlive the current function, but it borrows `v`, which is owned by the current function"

Now, you and I can clearly see that the async block does not, in fact, outlive the current function. However, AFAIK, there's no real way to convince rust that that is the case.

So now even though `v` is immutable, making this a thread safe operation, we need to introduce unnecessary concurrency constructs or break out unsafe in order to force the language to recognize that "Yes, you can trust me".

For rust to actually be fast with concurrent operations, it needs to be smarter about lifetimes in terms of async.

You don't need to spawn tasks for concurrency.

https://docs.rs/tokio/latest/tokio/macro.join.html

Yeah, and the amount of work that rustc has to do contemplating the safety of these async bodies, their witnesses, etc, especially if the signature is generic, explains most of the reason why rust builds are so long.
Absolutely. I have the exact same problem dealing with Python people who have done the same thing. "This is all modern, async python and therefore can scale. What do you mean our event loops are routinely blocked?!"

I thought we as an industry had hopefully moved past "async == magic code-go-fast sauce", but apparently we have not.

As much as Go annoys me, you're right there as well. I have to admit that Go did "solve" the make-concurrency-and-parallelism-accessible (and relatively safe) space in a way that very, very few other programming languages did (and none that people widely use).

> and none that people widely use

Java already did, especially now with virtual threads. Now other JVM languages can also get this benefit.

Java solved half of it: the risk of race conditions creating data corruption. But it didn't solve efficient threading or IO multiplexing (at least until virtual threads arrived recently), which put a damper on the ability of folks to use the language's concurrency primitives to model units of business logic; that'd just create too many threads even on a modern system. Goroutines have no such issue.

Erlang, as the adjacent comment points, out, did solve that! But at such a significant runtime (performance/memory) and syntactic cost that it's very rarely used, even nowadays after the Elixir renaissance.

Erlang BEAM VM
What absolutely kills me is people who go the other way, hard-line refusal to ever block an executor, then a few days into it they decide they need "backpressure" which they would have got FOR FREE if they had just stayed on the main event loop.
Eeeeh...backpressure via loop blocking only kinda-sorta works, with some nasty externalities. If you're doing heterogenous work (e.g. serving lots of different web routes on one server and only want to backpressure some) loop-blocking can have an unwelcome blast radius, plenty of telemetry/sidecar-ish systems will either bug out or alarm if the loop's blocked, and some event-loop-based systems will start to struggle processing signals/shutting down the runtime while the loop's blocked as well.

Loop-blocking-as-backpressure is sometimes the right move, but only rarely. If you can isolate event loops for specific workloads (e.g. some shameless self promotion, releasing in PyPi this week: https://github.com/zbentley/async-anywhere/blob/main/src/asy...) that becomes a more tractable approach.

There are multiple ways to do it, sure. But I really hate hydraulic analogies and what people want from "backpressure" is what you get from simply not accepting and reading sockets quite as eagerly. You can always read a socket later. That's how TCP works.