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.
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`).
...in what way is this related? Ownership is ownership, it's always a factor. Allocation is different because if you don't want any dynamic allocation, you don't want an allocator in the first place, ownership is an orthogonal concern
Frankly I'm not sure syntactically, didn't have to touch no-std in a bit, so I assume you're right. I do know that no_alloc is a crate tag people use and it's a readable enough way to phrase it
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
It's not only about performance. It is also about embedded system that don't have much memory, and have no fallback when they are out of memory. When ending up in some untested and faulty state you'd prefer getting some logs to just panicking and rebooting.
When the code uses dynamic allocation liberally, there is no such option: once a malloc fails you can only panic. You can reserve a piece of memory for recovery action, but it doesn't help much if recovery path engages most of the codebase. E. g. on IoT stuff most of the complexity is in the network stack - and you need the network stack if you want to get your logs.
And yes, recursion and stack is also a problem, even if more manageable than malloc. You can avoid recursion completely, but there still a risk of stack overflow on an untested code path. Zig promised staticaly-known stack depth once, but, afaik, didn't get there.
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.
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.
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.
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.
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!
> 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:
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.
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.
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.
This is a bit of a misunderstanding. Using a bump allocator is also useful for avoiding things like SmallVec. It can also good for reducing page faults or using the CPU cache better.
It has a lot of applications but people just don't use it for those applications.
It is just difficult to understand why people use it unless you actually understand what effects it has. I think a lot of people are making the mistake of assuming they would have realized the need for it naturally, but it is pretty hard to just stumble into it as an idea.
I understood the importance because of writing storage engines in rust for some years where I had a lot of trouble avoiding memory fragmentation with global allocators and also having page faults destroying performance. Even then you don't immediately think "oh I should use a bump allocator", you would still need to understand how the design of that allocator effects the problems you have.
> 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.
I'm not saying it is a bad language as I use it since 9 years and still write new code in it.
But if you compare writing an bump allocator in c or zig vs rust, it is comically bloated. Same applies to anything like a vector or a hashmap or anything else low level.
You get a binary that is multiple megabytes if you just write a webserver on the path of least resistence.
the Rust version of bcachefs has an object size 50% bigger than the C version, without trying to optimize for object size and with a whole bunch of new code to replace things that we used to get from the kernel but weren't Rust.
Even if that doesn't shrink I consider that a very good tradeoff for what we're getting.
May I ask if you're comparing apples to apples (statically linked everything) in the last case? I'm not very well versed in C or C++, but I do know that a common practice there is dynamically linking the world, so I'm curious if the size difference stays if you link statically in both cases?
I write zig like C++ code where there is no headers (I use modules only) and I don't depend on libc or use any standard headers. The output binaries are extremely small if you also enable the linker garbage collection.
So everything is statically linked and only depends on linux syscall API. It is not apples to apples compared to rust but you can't practically do that in rust anyway.
This isn't something baked into the core language like C++ features tend to be. This is standardization of library code, thoroughly vetting what people already had to do but in incompatible ways.
This is the kind of work that is making Rust far easier and more productive for systems work, including even kernel development, than the bad old days of when every project was a monolith and everyone was reinventing the wheel.
Over the past several months I've been converting bcachefs to Rust, and as part of that I have a whole truckload of code that I can now release as small self contained crates that anyone can use: safe RCU, an async runtime suitable for kernel IO paths, a fully lockless implementation of Cuckoo - custom allocators getting standardized is directly pertinent because now all that code (RCU, especially) will compose much better with other people's crates.
Rust's async is 1000x more complicated than mere custom allocators, so the complexity argument is invalid. You are complaining about a mote of sand when there is a gigantic mammoth stomping on your head.
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!
Yet, in the lang adoption game, many who were looking at Zig thanks to native support for allocators will now re-look at Rust to solve their problem.
And many who said they'd never use custom allocators when discussing using Zig will see how neat it is now that they can do it in Rust.
> Last time I tried, even the standard containers were giving different layouts to allocate and deallocate.
Do you have some concrete examples? I couldn't reproduce this but maybe I'm looking in the wrong places.
do those of us who come from a c++ background this all looks oddly familiar and equally painful
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.
When you need it, you need it for the whole program (including all the dependencies).
rust has GlobalAlloc
Which is useless if you don't want any dynamic allocation at all.
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`).
...in what way is this related? Ownership is ownership, it's always a factor. Allocation is different because if you don't want any dynamic allocation, you don't want an allocator in the first place, ownership is an orthogonal concern
Huh, that's a thing? I thought there was just `no_std` and then you can simply leave out `extern crate alloc`.
Frankly I'm not sure syntactically, didn't have to touch no-std in a bit, so I assume you're right. I do know that no_alloc is a crate tag people use and it's a readable enough way to phrase it
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).
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
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.
It's not only about performance. It is also about embedded system that don't have much memory, and have no fallback when they are out of memory. When ending up in some untested and faulty state you'd prefer getting some logs to just panicking and rebooting.
When the code uses dynamic allocation liberally, there is no such option: once a malloc fails you can only panic. You can reserve a piece of memory for recovery action, but it doesn't help much if recovery path engages most of the codebase. E. g. on IoT stuff most of the complexity is in the network stack - and you need the network stack if you want to get your logs.
And yes, recursion and stack is also a problem, even if more manageable than malloc. You can avoid recursion completely, but there still a risk of stack overflow on an untested code path. Zig promised staticaly-known stack depth once, but, afaik, didn't get there.
Rust is kind of targeting those users. I want Rust to cater to this usecase. It is fine not to use custom allocators if you don’t want to.
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.
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.
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.
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.
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!
> 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:
So that just leaves implementingIn 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.
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.
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.
This is a bit of a misunderstanding. Using a bump allocator is also useful for avoiding things like SmallVec. It can also good for reducing page faults or using the CPU cache better.
It has a lot of applications but people just don't use it for those applications.
It is just difficult to understand why people use it unless you actually understand what effects it has. I think a lot of people are making the mistake of assuming they would have realized the need for it naturally, but it is pretty hard to just stumble into it as an idea.
I understood the importance because of writing storage engines in rust for some years where I had a lot of trouble avoiding memory fragmentation with global allocators and also having page faults destroying performance. Even then you don't immediately think "oh I should use a bump allocator", you would still need to understand how the design of that allocator effects the problems you have.
Ambient authority was a mistake, for everything.
> 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.
> I consider those as unnecessary complexity for a niche use case
Custom allocators are certainly no niche use case, they are very common low level programming, from microcontrollers to audio software to video games.
It already is bloated.
I'm not saying it is a bad language as I use it since 9 years and still write new code in it.
But if you compare writing an bump allocator in c or zig vs rust, it is comically bloated. Same applies to anything like a vector or a hashmap or anything else low level.
You get a binary that is multiple megabytes if you just write a webserver on the path of least resistence.
the Rust version of bcachefs has an object size 50% bigger than the C version, without trying to optimize for object size and with a whole bunch of new code to replace things that we used to get from the kernel but weren't Rust.
Even if that doesn't shrink I consider that a very good tradeoff for what we're getting.
May I ask if you're comparing apples to apples (statically linked everything) in the last case? I'm not very well versed in C or C++, but I do know that a common practice there is dynamically linking the world, so I'm curious if the size difference stays if you link statically in both cases?
I write zig like C++ code where there is no headers (I use modules only) and I don't depend on libc or use any standard headers. The output binaries are extremely small if you also enable the linker garbage collection.
So everything is statically linked and only depends on linux syscall API. It is not apples to apples compared to rust but you can't practically do that in rust anyway.
This isn't something baked into the core language like C++ features tend to be. This is standardization of library code, thoroughly vetting what people already had to do but in incompatible ways.
This is the kind of work that is making Rust far easier and more productive for systems work, including even kernel development, than the bad old days of when every project was a monolith and everyone was reinventing the wheel.
Over the past several months I've been converting bcachefs to Rust, and as part of that I have a whole truckload of code that I can now release as small self contained crates that anyone can use: safe RCU, an async runtime suitable for kernel IO paths, a fully lockless implementation of Cuckoo - custom allocators getting standardized is directly pertinent because now all that code (RCU, especially) will compose much better with other people's crates.
As proven multiple times since JOVIAL, complex features are there for a reason.
It's an art to decide which use cases should be denied or not.
Rust's async is 1000x more complicated than mere custom allocators, so the complexity argument is invalid. You are complaining about a mote of sand when there is a gigantic mammoth stomping on your head.
so... what exactly was changed from straight-forward implementation of the feature that has been "unstable" for so many years?
Whether it should be dyn-safe or have associated types (mutually exclusive choice) was also a big point of contention.