at this point I don't see much argument for languages that make allocators explicit. the escape hatch is there in rust for the rare case when you need it, and you get safety and all of rust's other benefits now too. well done to the rust core team to get this stabilised.
The time-and-again application cited for the explicit allocators in the Handmade Community languages is the once-per-frame allocations inside a video game or similar software.
So there's probably value in having some Rust examples which use a trivial Allocator for Vecs and Boxes in such a loop. And then in looking at which other collection types are most often used in this context. String is the most obvious thing people might want. String is analogous to Java's StringBuilder or maybe to C++ std::string although neither is a perfect analogy, it's a Vec<u8> inside, but of course today that's Vec<u8,Global> and so to make it use Allocator that needs to be modified to Vec<u8,A: Allocator> instead and then all the related API changes made.
I think in practice the community and to some extent the Auteurs behind each of the popular Handmade languages lean too hard on their custom allocators and the equivalent Rust just wouldn't allocate in many cases. An Odin programmer will idiomatically cause the program to allocate a temporary growable array of strings with two strings in it where the Rust programmer would str::split_once to get both strings without allocating. The Odin allocation was "cheap" but the Rust didn't allocate.
Not really. Case in point: (soft)realtime audio programming. If you need to allocate memory in the audio callback (or DSP helper threads), you must use a real-time safe allocator (bounded worst case, no mutexes). For the rest of the program you may still use the default system allocator.
My toy kernel says otherwise. There are many stages of boot at varying levels of abstraction and being able to conveniently express each one is wonderful.
Also think of things like embedded scripting languages. Or having a special allocator for physics objects in a game as opposed to GUI objects. Yes, you can use a global allocator tuned to the workload. You can also tune allocators for each one. You don't have to, but you can.
>My toy kernel says otherwise. There are many stages of boot at varying levels of abstraction and being able to conveniently express each one is wonderful.
You need tight allocation control and arena usage when squeezing the most performance possible out of your system is non-negotiable. Most people don't need to write such code.
Object pools are somewhat popular. Hugely popular in game development. And they are essentially a custom allocator for languages that don't have custom allocators
Then you should generally go for `no_alloc`, because std allocates, so no_std + no_alloc leaves you with core + a bunch of crates for no_std data manipulation
And then I have to deal with writing tons of code to please the borrow checker myself. At which point I might as well go for a language that doesn't need so much ritual.
It's not just syntax. Depending on the allocation scheme used it can be very difficult to prove to the borrow checker that it's all safe, or even impossible (the same way some data structures in Rust can only be implemented with `unsafe`).
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.
> It's going to take a bit of time for the ecosystem to catch up to custom allocators.
I havn't published any popular rust crate. I think my crates are downloaded by me and robots only, so I don't have a lot of authority. I have no wishes to think about custom memory allocators. I don't want them. I consider those as unnecessary complexity for a niche use case, and I'm a bit annoyed that they will pollute the Rust landscape. I understand why people need them though, but I fear that Rust is going to be the next C++: a bloated programming language with features to please everyone and a lack of simplicity. It's already complex enough.
Good luck to all people who are going to try implementing allocators!
Last time I tried, even the standard containers were giving different layouts to allocate and deallocate.
Also as a bonus, you get to put another template parameter to all of your containers!
at this point I don't see much argument for languages that make allocators explicit. the escape hatch is there in rust for the rare case when you need it, and you get safety and all of rust's other benefits now too. well done to the rust core team to get this stabilised.
The time-and-again application cited for the explicit allocators in the Handmade Community languages is the once-per-frame allocations inside a video game or similar software.
So there's probably value in having some Rust examples which use a trivial Allocator for Vecs and Boxes in such a loop. And then in looking at which other collection types are most often used in this context. String is the most obvious thing people might want. String is analogous to Java's StringBuilder or maybe to C++ std::string although neither is a perfect analogy, it's a Vec<u8> inside, but of course today that's Vec<u8,Global> and so to make it use Allocator that needs to be modified to Vec<u8,A: Allocator> instead and then all the related API changes made.
I think in practice the community and to some extent the Auteurs behind each of the popular Handmade languages lean too hard on their custom allocators and the equivalent Rust just wouldn't allocate in many cases. An Odin programmer will idiomatically cause the program to allocate a temporary growable array of strings with two strings in it where the Rust programmer would str::split_once to get both strings without allocating. The Odin allocation was "cheap" but the Rust didn't allocate.
When you need it, you need it for the whole program (including all the dependencies).
Not really. Case in point: (soft)realtime audio programming. If you need to allocate memory in the audio callback (or DSP helper threads), you must use a real-time safe allocator (bounded worst case, no mutexes). For the rest of the program you may still use the default system allocator.
My toy kernel says otherwise. There are many stages of boot at varying levels of abstraction and being able to conveniently express each one is wonderful.
Also think of things like embedded scripting languages. Or having a special allocator for physics objects in a game as opposed to GUI objects. Yes, you can use a global allocator tuned to the workload. You can also tune allocators for each one. You don't have to, but you can.
>My toy kernel says otherwise. There are many stages of boot at varying levels of abstraction and being able to conveniently express each one is wonderful.
You need tight allocation control and arena usage when squeezing the most performance possible out of your system is non-negotiable. Most people don't need to write such code.
Even performance critical applications have non-performance critical parts.
E.g. in the aforementioned audio programming you may do the setup, init phase normally and only go for custom allocators afterwards.
Object pools are somewhat popular. Hugely popular in game development. And they are essentially a custom allocator for languages that don't have custom allocators
rust has GlobalAlloc
Which is useless if you don't want any dynamic allocation at all.
If you don't want any allocation at all, you don't need an allocator API.
But the global allocator is the response to your previous comment:
> When you need it, you need it for the whole program (including all the dependencies).
Then you should generally go for `no_alloc`, because std allocates, so no_std + no_alloc leaves you with core + a bunch of crates for no_std data manipulation
And then I have to deal with writing tons of code to please the borrow checker myself. At which point I might as well go for a language that doesn't need so much ritual.
Why not use an LLM to help with the actual syntax while you only need to worry about the data flow? I've found they're quite good with that.
It's not just syntax. Depending on the allocation scheme used it can be very difficult to prove to the borrow checker that it's all safe, or even impossible (the same way some data structures in Rust can only be implemented with `unsafe`).
Huh, that's a thing? I thought there was just `no_std` and then you can simply leave out `extern crate alloc`.
Postgres has 11 allocators, erlang has at least 8. Databases and VMs are canonical use cases for systems PLs.
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.
> It's going to take a bit of time for the ecosystem to catch up to custom allocators.
I havn't published any popular rust crate. I think my crates are downloaded by me and robots only, so I don't have a lot of authority. I have no wishes to think about custom memory allocators. I don't want them. I consider those as unnecessary complexity for a niche use case, and I'm a bit annoyed that they will pollute the Rust landscape. I understand why people need them though, but I fear that Rust is going to be the next C++: a bloated programming language with features to please everyone and a lack of simplicity. It's already complex enough.
so... what exactly was changed from straight-forward implementation of the feature that has been "unstable" for so many years?
* Getting all the bajillion stakeholders to agree on what to stabilize. (I think this is the hardest part.)
* Fixing a pile of soundness bugs, (the fixes spawned a couple new still-unstable traits in the process: AllocatorClone and StaticAllocator).
* Specifying that panicking isn't allowed.
* Rewriting the safety docs of the Allocator trait to be more airtight.