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

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

219 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. Postgres has 11 allocators, erlang has at least 8. Databases and VMs are canonical use cases for systems PLs.
    Replies (1)
    1. Allocators and things that look like allocators are pretty common in large projects. But allocators that can serve a standard container are significantly less common.

      For example, you sometimes want a bump allocator (like the bumpalo crate). You almost never want to use it for a HashMap.

      Replies (2)
      1. Idk. If i were writing a web server to service server generated content, id want to do a bump allocator per user request, and it's hard to imagine not needing hashmap for many such servers.
      2. You do always want to use for something like a hashmap. Why else would you want a custom allocator otherwise? Those things are what consume the majority of the memory in a program.

        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 (3)
        1. If youre writing in zig, literally any std datatype works with any custom allocator, and most 3p libraries will too, and if you go through std's allocator interface, you'll be able to do static analysis on that too!
        2. > 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.

          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:

            deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {}
          
          So that just leaves implementing

            allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> { todo!() }
          
          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)
          1. You have to manage the lifetimes too, also you need to be able to set_offset() on bump allocator to use it effectively in my experience. You can't just increment/increment then drop it. Having a way of rolling it back to point x makes it a lot better.
        3. The reason you really do not want a bump allocator for your hash map is that rehashing will reallocate everything, and there's no way to reclaim the freed memory blocks.

          Know your algorithms and data structures.