It's only been a couple of months since the last article, and I am pleased to see that, after 10 years of limbo, allocators have finally been stabilised! While it feels like the never type did steal a bit of thunder, rust 1.100 was nonetheless a monumental release. Congratulations on the team that shepherded this through.
Sources indicate that so, now we have an MVP of the allocator API, what can we do with it? What does it get us besides closer feature parity with other system languages like C++ & Zig? (Related: Porting fairseq wmt19 translation system to transformer...)
In this article I'll step through a few use cases that might be handy to the layman rust programmer, so hopefully you can see the benefit.
According to recent reports, this was one of the leading cases for custom allocation. Dropping in rust is not free. There has been prior art in actually moving memory to a dedicated thread to Drop highly nested structures. This is actually only half a solution. You're still paying for the deallocation call costs, just it's not in the hot path anymore.
Instead, what if we knew that we would have a period of time in our code, where we're going to allocate a lot of stuff, do something with it, and then free everything? A good example of this is a web request, or a database query.
A request comes in, fills in some data structures, and then when the request finishes, the memory isn't needed anymore so it's dropped. This sort of "burst" pattern of allocation, where you allocate a lot up front, and then tear things down all at once at the end, is quite common.
For this we can use a bump allocator: reserve a big block of memory, and allocate from that, rather than paying the cost of talking to the system allocator for every allocation. Think of it like the BufReader and BufWriter of std io, rather than make repeated syscalls to fetch data, we can buffer that data in a big block, so that we can reduce the overhead considerably. (Related: Block Sparse Matrices for Smaller and Faster Language M...)
Recent findings suggest that you want more memory? Just check if the size fits in our block, return that slice, increment our counter (bump!), and we're done. We can add more blocks as well, if we run out. This makes allocations very cheap, we don't need to keep much state around besides from a counter.
In a recent development, the downsides of this approach is when we come to deallocate memory. We don't keep track around where individual allocations are made. However, this actually solves our drop takes a long time problem, since we can just deallocate everything all at once.
Essentially if the impl of the bump allocator is:.
There are a few tradeoffs for doing things like this in a bump allocator, and not every allocation is going to benefit. Hence why it's important to be able to adjust our allocation strategy locally rather than just globally. However, an astute reader might notice that, if we are bumping large swathes of memory, this is going to possibly cause our memory usage to increase considerably. Well, that brings us to our next section: memory accounting.
The next challenge that can be solved by custom allocators is knowing how much memory particular sections of code is being used. I.e, memory accounting. The key here is that, we can use existing allocators, and sprinkle in some extra bookkeeping to keep a pretty accurate ledger.
There are a number of use cases that accounting can shape the behaviour of code. Say you have a cache of objects, and you want to set a threshold above which, we start evicting entries. Or say you want to do an external sort, but you don't know how big your data structures are at the start, but you want to use up as much memory as possible. Or maybe you are wanting to place a limit on a code sandbox, because it's sharing resources with other sandboxes.
In a recent development, what you can do is wrap an allocator, with a custom accounting layer, and use this to drive your decisions. I.e, here is a very trivial example:.
Then we can wrap the global allocator, and see how much our cache is using:.
Recent findings suggest that now, a single atomic, while is great for an example, does have some perf overhead. So, one of the crates that I have created, while keeping in mind performance, is thresher: an accounting allocator with a callback on threshold. It uses a combined "local" task/thread and "global" counter to avoid atomic contention, feeding back in every 64KiB of allocation.
thresher gets its name from the cutest shark: the Thresher Shark.
Now, what you can do with thresher is, use it to trigger callbacks when allocations are hit:.
This prints out what you'd expect:.
One production use case, is that we're using thresher on logfire to provide us heap profiles of our rust services when memory hits a configured threshold. I did a deep dive into heap profiles and why they are extremely useful in an older blog: a tale of two lengths. In thresher you can find a jemalloc example where you can instrument this yourself!
OK so we can control memory when it is dropped, we can use memory accounting to know how much memory we're using, what's left? How about where it goes?
This is probably the more esoteric of the 3 examples I wanted to give. There are some situations where you actually need to control where the memory lives, and sometimes by doing some allocation tricks you can get better performance out.
One example would be what smallvec and friends do. Instead of going straight to the heap to allocate things, they have a small amount that lives on the stack, and only if allocations go above a certain size, then do they write to the heap.
While smallvec is more battle tested, here is some example code about how you could do this with the allocator API:.
Then we can use our toy stack first allocator:.
Et voila! We have smaller allocations on the stack and then push to the heap as needed. This can give some good performance boosts.
Normally when you align memory allocations, this is based upon the thing that is being allocated for. I.e, is it a u64? align to 8 bytes or a u32? align to 4. If it's not correctly aligned, this can cause undefined behaviour and all sorts. You notice in both the bump and small stack example I had to do some shenanigans to find the next appropriately aligned address. However, there is some scenarios where the alignment of memory is external to what is being allocated.
Industry observers note that in linux, when you read anything from disk via standard syscalls, you are usually reading from the kernel's page cache. I.e, the spare memory the kernel has around to make things transparently fast. Most of the time that is fine, but there are some times when you want to bypass it.
I.e, there is a long standing bug in kubernetes that can cause pod eviction if you are using the page cache. Or: if you are doing your own caching layer, sometimes it doesn't make sense to double up on memory devoted to caching files.
Sources indicate that so, there is a bailout provided, in the guise of O_DIRECT that allows you to directly access files from the file system. However: one of the requirements of using this flag is that you are reading or writing into memory that is aligned to the device's logical block size. This is usually a multiple of 512, but can be less or more depending on the hardware.
The implementation of a block aligned allocator is pretty simple, here's one where we just hardset it to 4096:.
With this, we can now write simple code to read directly using O_DIRECT:.
Here, the read_exact call can fail on some devices if we're not using block size aligned allocations.
Like with the stack first allocation, there are libraries that allow you do this already, such as the aligned-vec crate, with their own data types to work nicely.
Industry observers note that but here's the rub: say you had an application where you wanted to blend and mix in different allocation strategies: some vecs you want to be block aligned, some you want allocated on the stack, but you want to be able to use shared methods for both. Do we want to use smallvec and aligned-vec in order to deal with a data structure that is essentially the same thing, just allocated differently?
std allocators allows you to be generic over the collections, regardless of allocator used:.
And I think that's pretty powerful.
Recent findings suggest that it's going to take a bit of time for the ecosystem to catch up to custom allocators. A lot of popular crates have an MSRV policy that precludes them from using even new standard features, and some existing crates, which I would think are a shoe in for the new allocator API, as of writing do not yet have support.
According to recent reports, the libs team isn't done yet. This is the MVP of allocators, and, like I mentiond in the the last article, this is the beginning of custom allocator support. There is still work to go to support other collection types like maps etc.. so much to be done. I think that what we have is useful enough to start using it now, and also to start adapting and allowing library consumers the ability to provide their own allocators with their API.
I'm excited to see what people build, and what other use cases are unlocked with it!