Unofficial Hacker News companion — get notified on comment replies, and more.

The state of Rust allocators in 2026: stabilised (opens external site)

217 points by fanf2 4 days ago | cetra3.github.io | 90 comments on HN | Vote on HN (opens Hacker News) |

View full thread · View on Hacker News

Comment thread (8)

  1. I use Jemalloc in a specific project because it keeps per-thread allocation counters, so I can both monitor how much memory a thread is actively using (and how much it has allocated and deallocated), and I can limit it.

    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)
    1. I don't think it's possible to implement thread-local “global” allocators. Since you need to use the same allocator to allocate and deallocate memory, transferring such objects across threads could transfer them to a thread without the right allocator — any data allocated with that allocator would need to be !Send, but the type system doesn't allow types to vary like that based on what allocator is active.
      Replies (1)
      1. >Since you need to use the same allocator to allocate and deallocate memory

        This isn't true for jemalloc. You can allocate from one thread and deallocate on another.

        Replies (1)
        1. That is still 100% true for jemalloc you just misunderstood what OP said. You can’t allocate using jemalloc and deallocate using mimalloc. Which is what could happen if you have custom global allocators per thread.

          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.

    2. Not sure I understand what you're looking for exactly, but couldn't you just wrap a thread-local allocator (the design you're talking about) in a global allocator by referencing a static thread-local in that static global allocator? Then you wouldn't have to .new_in(), but you would still have thread-local allocation.

      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)
      1. Sure, but that has the overhead of a thread-local variable, and requires using threads.

        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)
        1. Oh I see, the thing that would work for your use case is essentially a dynamically-scoped allocator variable that all the functions which use the global allocator today would use instead.

          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.

    3. You could use something similar to tracing, where each task switch lets a global variable know which task is currently running. It could be a thread-local to reduce contention. Then a global allocator would always know which task (if any) is the one allocating.