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