"If you use Tokio, you should override the default allocator": Is this really a tokio issue?
At first glance, the heavy malloc/free load is inside the ion_rs work the application is doing, rather than anything inherent to tokio.
The threading model (in particular, that many allocations are malloced on one thread and freed on another) might be a significant factor also, but I think to a large extent that's also an application choice rather than inherent to tokio. "A Tokio worker reads the HTTP payload, deserializes it into our domain model using ion-rs, and dispatches the work to a separate worker pool (the business logic can't share threads with the async runtime). The worker pool runs the business logic and hands back a response, which a Tokio worker serializes and sends back to the client." The article mentions "Tokio's work-stealing scheduler moves tasks between worker thread" and that's sort of true (usually people use multithreaded tokio reactors) but I think is much less of a factor than the application's decision to hand off to another thread 100% of the time.
It would have been nice to mention which system allocator (glibc) you were using early in the article. I initially expected it to be about the famously slow musl allocator.
Maybe glibc uses a little less memory… and that’s all. We use mimalloc for almost 3 years already in production (back then jemalloc was basically unmaintained) and it is great.
We are aware of that and always do smoke, load and canary tests. Also we noticed it depends on the code a lot. If you write Rust in “TypeScript+clone() everywhere” style - it works surprisingly well.
We switched away from glibc to mimalloc because glibc is very hesitant at freeing memory which caused our staging server to crash when there were certain loading patterns.
It results in a smaller binary. It saves on compile time. And it can sometimes use less memory. But generally one of jemalloc or mimalloc will be superior (and often the difference isn't small).
I believe jemalloc has a higher baseline memory usage and bigger code size, there are a lot of small programs that aren’t hammering the allocator and probably prefer saving on the memory vs. (at that scale) minor speedups. Bigger programs where it matters probably already have dependencies figured out and can pull in jemalloc.
Feedback - referring to serialization and deserialization in your specific application as "serde" is fairly confusing, given the ecosystem of crates with serde in the name, centered around serde itself.
For example, "nothing significant happens in the request, so changes to serde show up directly in latency" - are you talking about changes to serde the crate? Or changes to your specific serialization/deserialization implementation? Or both?
As far as I am aware the other allocators like mimalloc are just like... strictly superior.
In terms of throughput yes, but depending on the work load and ancillary factors (e.g. support for valgrind, that sort of stuff) it might not be worth switching. It depends how much allocation factors in, how much cross-threat allocation there is, ...
Oh man! You mean I have to do engineering!? come on!
=P
In all seriousness, I wonder if someone has ever sat down and created a big collection of knobs / switches to consider for all these different kinds of trade offs against multiple domains and conditions? It would be an epic undertaking to deal with, but it would be kind of nice to see such a big list all organized and then go through it and realize 'oh...I now have something I need to check this week for work'.
I *know* there are a whole host of them out there. I know it. I've run into some of them while doing my work. I just don't know what I don't know, and that is frustrating.
17
u/slamb moonfire-nvr 21h ago edited 21h ago
"If you use Tokio, you should override the default allocator": Is this really a tokio issue?
104
u/Icarium-Lifestealer 1d ago
It would have been nice to mention which system allocator (glibc) you were using early in the article. I initially expected it to be about the famously slow musl allocator.
35
u/cbarrick 1d ago
Is there any use case where glibc malloc beats jemalloc?
71
u/Floppie7th 23h ago
Feedback - referring to serialization and deserialization in your specific application as "serde" is fairly confusing, given the ecosystem of crates with serde in the name, centered around serde itself.
For example, "nothing significant happens in the request, so changes to serde show up directly in latency" - are you talking about changes to serde the crate? Or changes to your specific serialization/deserialization implementation? Or both?
7
u/insanitybit2 23h ago
Isn't it basically always advisable to switch? As far as I am aware the other allocators like mimalloc are just like... strictly superior.