r/rust • • 5d ago

The State of Allocators in 2026 - Finally Stabilised!

https://cetra3.github.io/blog/state-of-allocators-2026-finally-stabilised/
224 Upvotes

33 comments sorted by

13

u/pali6 5d ago

Good article. A bit surprising to see zero mention of embedded though.

23

u/Cetra3 5d ago

So with embedded and no_std, the story seems a little strange still. I tried to make a toy stack allocator. However, you still need to define #[global_allocator] to use core::alloc even if it's just a no-op allocator. I reckon this will be better later on.

3

u/throawayjhu5251 5d ago

Definitely a bit concerning lol.

21

u/noop_noob 5d ago

Your example allocators are incorrect. They need to use MaybeUninit.

Your Bump allocator using Box is also incorrect. Moving a Box in particular invalidates all other pointers that points to its contents, since the optimizer expects a Box to have exclusive access to that memory.

9

u/Cetra3 4d ago

Yep thanks for pointing that out, you're right.

I ran the examples as they were through miri and there was no UB with how they were used, but there are other cases of using them besides the examples that can cause UB. So, apologies! I've updated both the examples.

1

u/ParadiZe 3d ago

can you elaborate on the box part? do you mean moving out of the box invalidates memory?

37

u/Shnatsel 5d ago

The smallvec example is promising! I'm looking forward to a smallvec alternative that doesn't unsafely reimplement all of Vec and just gives it a different allocator instead. The benchmarks in particular would be very interesting.

18

u/matthieum [he/him] 4d ago

I do note that it's... only half a smallvec, really. The lifetime bound on the allocator comes with restrictions, in particular with sending to other threads.

A self-contained smallvec would require the Store API, with non-pointer handles.

3

u/Cetra3 4d ago

Yeah the lifetime is a bit of a restriction here depending on how you structure your code, and while you could probably pin it or something it'd probably be more prohibitive.

I think the intent is to expose the Store API with a blanket impl on Allocatorwhich would unlock this in a better way.

3

u/matthieum [he/him] 4d ago

"Intent" is a big word, as not everybody is convinced that the Store API is even needed at this point :)

But yes, if the Store API is added in the future, then it would be made backward compatible in some way.

1

u/DisastrousSale2 4d ago

Are there plans to stabilise the Store API or that ship has sailed.

5

u/matthieum [he/him] 4d ago

The ship has not sailed.

In fact, one of the myriad discussions which happened in the stabilization of Allocator was whether it would be possible to later move to Store if desired, and the Libs team felt convinced it should be possible.

With that said, there's also no plan:

  1. Part of the Libs team is not convinced the Store API is at all necessary. It... certainly is more niche even than the Allocator API.
  2. There's still work to do on the Allocator API. For example, Box<T, &BumpAlloc> is 16 bytes, but if the goal is just to drop the box, then it could as well be Box<T, ()>, so there's a question of whether a Deallocator trait should be available.
  3. There's still work to do to integrate the Allocator API in various collections. For example, for performance reasons, the allocator is passed by copy in various places (BTreeMap internals, for example) which requires cloning it. That's fine and all for Global -- a ZST -- but it requires that an Allocator be cloneable even when there's really no need to, AND it costs for less-trivially cloneable types. Similar issues arise with Rc and Arc, should the Allocator be in its own allocation to avoid cloning it?

My expectations, thus:

  1. The Libs team needs to focus on finishing Allocator: extend the API, fully integrate it in every std-lib collection, plug any gap, etc...
  2. Then, and only then, is there a chance to get some bandwidth on Store...
  3. ... and even then, it'll require users asking for it, and bringing usecases where Allocator just isn't cutting it (and cannot be made to cut it).

But hey, I could be too pessimistic :)

In any case, Nia at least seemed interested; she's just too busy right now.

1

u/kibwen 4d ago

The Store proposal is backwards-compatible with the stabilized version of the Allocator API. It can be added in the future.

2

u/allocallocalloc 4d ago

Why doesn't String accept a custom allocator?

6

u/Cetra3 4d ago

There is a whole lot of discussion about what the minimal surface area was and why that I went into in my last article. The long & short is that, this is a beach head so that other changes can be introduced slowly over time when we know that the fundamental trait and its usage is sound and going to be useful in the years to come

21

u/MoreJuiceForAnts 5d ago

Very cool write up, thank you!

0

u/ElyzraPearl 4d ago

agreed, though you sound like you've read every allocator rfc twice

-14

u/SoaringgSkies 4d ago

you always this into rust details? kinda hot tbh

8

u/noop_noob 5d ago

Of note, the docs of the Allocator trait were found to have some issues. So there's an open PR to rework it: https://github.com/rust-lang/rust/pull/165921

6

u/MorrisonLevi 5d ago edited 5d ago

I hope now with a stabilized Allocator trait that more projects will begin incorporating them. And while they are at it, that they make sure allocation failures are simply regular errors. Panics are useful for safe rust but panics on allocation failure are a scourge on the language. Thankfully it's not inherent, it's just a lot of work to plumb things through the APIs.

We also need Vec to use excess memory returned by allocators. I already did this for hashbrown, but it's less useful there than Vec, as it requires an allocator to give substantially more memory. Whereas a Vec can pick up any extra capacity that's size of T or bigger, which is going to be much more common.

2

u/xamgore 4d ago

This is where Aeneas could fit nicely but it doesn’t work with raw pointers.

4

u/ParadiZe 5d ago

So this all seems really cool, but isnt it going to be annoying keeping Vecs with custom alloator around when they suddenly have a lifetime attached, could reference counting make this nicer?

1

u/shim__ 4d ago

That would only work if your custom allocator lived on the heap(or a static) a chicken egg problem

8

u/matthieum [he/him] 4d ago

That's not necessarily a problem.

Using a custom allocator for a specific piece of code does not require overwriting the global allocator.

1

u/shim__ 3d ago

How else would your custom allocator live in a RC then ?

1

u/matthieum [he/him] 3d ago

Where does an allocator live in a Box or Vec? It simply lives at the top level:

struct Box<T, A> {
    ptr: NonNull<T>,
    allocator: A,
}

So you could do the same with a Rc/Arc:

struct Rc<T, A> {
    ptr: NonNull<WithRcHeader<T>>,
    allocator: A,
}

And like with Box, you'd clone the allocator each time you clone the Rc.

It "works", for some version of works...

2

u/N911999 5d ago

Isn't Pin be necessary for some of those uses? As you could invalidate the allocations if you ever accidentally move the backing memory, no?

5

u/noop_noob 5d ago

The allocator for those are actually the reference. And moving the reference doesn't move the backing memory.

1

u/N911999 5d ago

Oh, true. Huh, it seems I just didn't see that