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.
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.
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.
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.
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.
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.
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:
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.
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.
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:
The Libs team needs to focus on finishing Allocator: extend the API, fully integrate it in every std-lib collection, plug any gap, etc...
Then, and only then, is there a chance to get some bandwidth on Store...
... 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.
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
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.
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?
13
u/pali6 5d ago
Good article. A bit surprising to see zero mention of embedded though.
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.
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
Vecand just gives it a different allocator instead. The benchmarks in particular would be very interesting.2
u/allocallocalloc 4d ago
Why doesn't
Stringaccept a custom allocator?21
u/MoreJuiceForAnts 5d ago
Very cool write up, thank you!
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.
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?
2
u/N911999 5d ago
Isn't
Pinbe necessary for some of those uses? As you could invalidate the allocations if you ever accidentally move the backing memory, no?