r/rust • • 1d ago

If you use Tokio, change the default allocator

https://dial9-rs.github.io/blog/allocator-contention-in-a-tokio-service/
186 Upvotes

22 comments sorted by

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?

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

2

u/arcoain 21h ago

yeah not a Tokio-specific issue; Tokio is just a very common reason for resources to be created on one thread and freed on another.

1

u/arcoain 21h ago

yeah not a Tokio-specific issue; Tokio is just a very common reason for resources to be created on one thread and freed on another.

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.

16

u/masklinn 21h ago

The numbers for musl would probably have been significantly worse, since musl completely serialises allocations.

35

u/cbarrick 1d ago

Is there any use case where glibc malloc beats jemalloc?

23

u/drive_an_ufo 23h ago

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.

25

u/Shnatsel 23h ago

9

u/Fabulous-Meaning-966 18h ago

The issues described in these posts don't apply to mimalloc v3.

6

u/drive_an_ufo 20h ago

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.

1

u/angelicosphosphoros 57m ago

Isn't it an issue only if you have lot's of threads? I expect programs using tokio to use limited number of threads so leaks are bound.

1

u/Luxalpa 19h ago

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.

19

u/nicoburns 23h ago

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

3

u/lcvella 22h ago

It buggers me. If the others are almost always better, why don't they replace glibc with one of them?

30

u/cbarrick 22h ago

Rust actually used to use jemalloc by default.

But it's a bad default. I think most users would be surprised to not be using the system libc for allocation.

5

u/lcvella 22h ago

I meant, inside glibc.

10

u/Zde-G 22h ago

Good question. There was no need to support Emacs for more than a decade, not sure what unique ability is still in used by anyone.

4

u/cosmic-parsley 21h ago

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.

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.

11

u/masklinn 21h ago

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

3

u/addmoreice 18h ago

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.