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 (2)

  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.