The state of Rust allocators in 2026: stabilised (opens external site)
View full thread · View on Hacker News
To get push notifications on iPhone or iPad, add Hack and Chat to your Home Screen: tap Share → Add to Home Screen, then open the installed app and enable push in Settings.
View full thread · View on Hacker News
Replies (4)
Replies (3)
Replies (1)
Replies (2)
Replies (2)
Continue this thread (2 replies) →
Continue this thread (2 replies) →
But the global allocator is the response to your previous comment:
> When you need it, you need it for the whole program (including all the dependencies).
Also think of things like embedded scripting languages. Or having a special allocator for physics objects in a game as opposed to GUI objects. Yes, you can use a global allocator tuned to the workload. You can also tune allocators for each one. You don't have to, but you can.
Replies (1)
You need tight allocation control and arena usage when squeezing the most performance possible out of your system is non-negotiable. Most people don't need to write such code.
Replies (4)
E.g. in the aforementioned audio programming you may do the setup, init phase normally and only go for custom allocators afterwards.
When the code uses dynamic allocation liberally, there is no such option: once a malloc fails you can only panic. You can reserve a piece of memory for recovery action, but it doesn't help much if recovery path engages most of the codebase. E. g. on IoT stuff most of the complexity is in the network stack - and you need the network stack if you want to get your logs.
And yes, recursion and stack is also a problem, even if more manageable than malloc. You can avoid recursion completely, but there still a risk of stack overflow on an untested code path. Zig promised staticaly-known stack depth once, but, afaik, didn't get there.
Replies (1)
For example, you sometimes want a bump allocator (like the bumpalo crate). You almost never want to use it for a HashMap.
Replies (2)
To give a concrete example, if you are using bump allocators for a web server to limit the amount of memory a request handler can use or to maximize CPU cache usage or avoid doings syscalls or mutexes while handling the request. Then you want the allocator to apply to everything that the request does. Otherwise there is no point in using it in the first place.
If you mean that it is difficult to use it for a hashmap. It is a bit weird to program in that way but it isn't really difficult if you understand it.
You may think "I'll have a special hashmap for the side of the program that uses the most memory". Then the problem here is that, if you are writing a large project in rust, you have to use the standard library and some dependencies. You can't write all that yourself because rust specifically makes writing that stuff very difficult.
If you are writing in C you can write a bump allocator in 10 minutes if you know how to do it. It is basically impossible to do that in rust.
Replies (2)
Your bump allocator is just a type with a "how much is left?" usize and a blob of reserved memory it owns, plus an implementation of the Allocator trait which has two (unsafe) functions.
The first one is easy, we're a bump allocator, deallocation is a no-op:
So that just leaves implementing In our bump allocator that's just not very much work. Decide if there's enough space, if there isn't fail early, otherwise make a NonNull<[u8]> and decrease our "How much is left" counter.Is it slightly more work than in C? Yeah, I guess so, but not much.
Replies (1)
So there's probably value in having some Rust examples which use a trivial Allocator for Vecs and Boxes in such a loop. And then in looking at which other collection types are most often used in this context. String is the most obvious thing people might want. String is analogous to Java's StringBuilder or maybe to C++ std::string although neither is a perfect analogy, it's a Vec<u8> inside, but of course today that's Vec<u8,Global> and so to make it use Allocator that needs to be modified to Vec<u8,A: Allocator> instead and then all the related API changes made.
I think in practice the community and to some extent the Auteurs behind each of the popular Handmade languages lean too hard on their custom allocators and the equivalent Rust just wouldn't allocate in many cases. An Odin programmer will idiomatically cause the program to allocate a temporary growable array of strings with two strings in it where the Rust programmer would str::split_once to get both strings without allocating. The Odin allocation was "cheap" but the Rust didn't allocate.
Replies (1)
It has a lot of applications but people just don't use it for those applications.
It is just difficult to understand why people use it unless you actually understand what effects it has. I think a lot of people are making the mistake of assuming they would have realized the need for it naturally, but it is pretty hard to just stumble into it as an idea.
I understood the importance because of writing storage engines in rust for some years where I had a lot of trouble avoiding memory fragmentation with global allocators and also having page faults destroying performance. Even then you don't immediately think "oh I should use a bump allocator", you would still need to understand how the design of that allocator effects the problems you have.
Replies (1)
After some thought, the best use case I could come up with was as follows: a set of K growable arrays such that in total they have M entries but each might have N = 0..=M entries of its own and so with SmallVec that needs K*M space but with an Allocator you're sure it all fits in M space which may be a significant improvement.
Except I think the correct representation for that is a single Boxed array or similar and K slices of that Boxed array instead.
Replies (1)
If a function doesn't know the size it needs for a vector for example. It will at least have an upper bound, then it can allocate with the upper bound on the temporary alloc and construct the thing. After it is constructed it gets moved to the more long lived allocator. So the program is really only accessing three sections of memory at a time including the stack. Allocation/deallocation are just integer increment/decrement. And I can put an upper bound on the amount of memory a section of the program will use.
This way you don't need the SSO types at all.