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
This is an incredible feature, and it's a hard requirement for this particular project, and a good fit because the workloads map to threads. Manually managing such counters with the standard Rust allocator is not a practical option.
But one problem I see with the new Rust allocators is that it doesn't seem like there's any kind of thread-local allocator? If so, you have to pass an allocator through every nook and cranny of the system as opposed to just riding on the thread context.
Even if the allocator is available as a "global" thread-local variable, which might be an option, that's a lot of places that need to be updated with .new_in(). And you have to introduce some kind of linting rule that rejects any code that isn't using the custom allocator.
I don't suppose a solution to this problem exists or is planned?
Replies (3)
Replies (1)
This isn't true for jemalloc. You can allocate from one thread and deallocate on another.
Replies (1)
Btw nothing stops you from implanting an allocators that has TLS state and counters. You can even have a global allocators that counts per thread and delicates to an arbitrary allocator that may not have those counters.
I'm not saying this is a good idea, just that it seems like Rust does maybe accommodate what you're talking about already.
Replies (1)
I guess where I'm getting as is that there are genuine use cases where you want to enforce an allocator throughout a subsystem without rewriting absolutely every line that can allocate. But Rust does not have any way to pass "implicit" context down the stack, from what I know.
With Zig, allocators are already explicit, but Rust didn't start out that way, and it doesn't really fit into how Rust code is written today.
Replies (1)
You kind of can do dynamically scoped variables (though the only way I know to do this itself uses a thread-local stack and doesn't play nice with async), but you wouldn't be able to rewrite e.g. Box::new to use it instead of the thing bound to the global allocator. You could, however, provide an implementation of a global allocator that resolves your dynamically-scoped allocator for each allocation.
Yeah you would pay for the overhead of a thread-local on every allocation. Rust nightly does at least support a faster version of TLS (#[thread_local]) instead of the thread_local! macro. Not ideal, but there would necessarily be _some_ overhead involved with any strategy for resolving a dynamically-bound Allocator impl at runtime instead of a static one.