Rust 1.100 发布,分配器 API 结束十年停滞正式稳定
来源标题:The State of Allocators in 2026 – Stabilised
Rust 1.100 发布,分配器 API 在历经约十年停滞後终于稳定,并提供了 MVP。文章介绍了自定义分配器的实用场景,例如在 Web 请求或数据库查询中集中分配大量内存,处理后再统一释放。
以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。
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.
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?
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.
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.
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.
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:
const SIZE: usize = 32 * 1024 * 1024;
struct Bump { // one big 32MiB block on the heap data: Box<Cell<[u8; SIZE]>>, // how much of it we've handed out used: Cell<usize>, }
impl Bump { fn new() -> Self { Bump { data: unsafe { Box::new_zeroed().assume_init() }, used: Cell::new(0) } } }
unsafe impl Allocator for &Bump { fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> { // where we are in memory let data = self.data.as_ptr() as *mut u8;
// how much we've used of data already
let used = self.used.get();
// given the requested alignment, how many bytes do we need past data + used
let alignment_padding = data.wrapping_add(used).align_offset(layout.align());
let start = used + alignment_padding; let end = start + layout.size();
if end <= SIZE { // bump! self.used.set(end); let ptr = unsafe { NonNull::new_unchecked(data.add(start)) }; return Ok(NonNull::slice_from_raw_parts(ptr, layout.size())); }
// an actual bump allocator would grab another block here Err(AllocError) }
unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) { // deliberately a no-op! } }
Then we can use it like so:
struct Node<'a> { value: u64, parent: Option<&'a Node<'a>>, }
// our bump allocator let bump = Bump::new();
// leak here gives us a ref tied to the arena, the memory still goes when we drop our allocator let mut leaf: &Node = Box::leak(Box::new_in(Node { value: 0, parent: None }, &bump));
for i in 1..1_000_000 { leaf = Box::leak(Box::new_in(Node { value: i, parent: Some(leaf) }, &bump)); }
// this takes virtually no time at all! drop(bump);
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.
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:
struct AccountedAllocator<A: Allocator> { inner: A, allocated: AtomicUsize, }
impl<A: Allocator> AccountedAllocator<A> { fn new(inner: A) -> Self { Self { inner, allocated: AtomicUsize::new(0) } }
fn allocated(&self) -> usize { self.allocated.load(Ordering::Relaxed) } }
unsafe impl<A: Allocator> Allocator for &AccountedAllocator<A> { fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> { // pass the allocation through, and count it if it succeeded let ptr = self.inner.allocate(layout)?; self.allocated.fetch_add(layout.size(), Ordering::Relaxed); Ok(ptr) }
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) { unsafe { self.inner.deallocate(ptr, layout) }; self.allocated.fetch_sub(layout.size(), Ordering::Relaxed); } }
Then we can wrap the global allocator, and see how much our cache is using:
let accounted = AccountedAllocator::new(Global);
let mut cache: Vec<u8, _> = Vec::new_in(&accounted);
cache.extend_from_slice(&[0; 1024]); // cache is using 1024 bytes println!("cache is using {} bytes", accounted.allocated());
cache.extend_from_slice(&[0; 4096]); // cache is using 5120 bytes println!("cache is using {} bytes", accounted.allocated());
drop(cache); // cache is using 0 bytes println!("cache is using {} bytes", accounted.allocated());
Shameless Plug: the Thresher crate
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:
let thresher = Thresher::new(); thresher.set_callback_threshold(100 * 1024 * 1024); thresher.set_callback(|allocation| { println!("Threshold reached! Allocated: {} bytes", allocation); });
// This is what we're adding to our vec each loop let bytes = vec![0u8; 10 * 1024 * 1024];
// The vec allocates through the handle, so its buffer counts towards the
// threshold. bytes above uses the global allocator and is not counted
let mut vec = Vec::<u8, _>::new_in(thresher.handle());
for i in 0..10 { println!("Loop {i}, Vec Capacity: {}", vec.capacity()); vec.extend(&bytes); }
This prints out what you'd expect:
Loop 0, Vec Capacity: 0 Loop 1, Vec Capacity: 10485760 Loop 2, Vec Capacity: 20971520 Loop 3, Vec Capacity: 41943040 Loop 4, Vec Capacity: 41943040 Loop 5, Vec Capacity: 83886080 Loop 6, Vec Capacity: 83886080 Loop 7, Vec Capacity: 83886080 Loop 8, Vec Capacity: 83886080 Threshold reached! Allocated: 167772160 bytes Loop 9, Vec Capacity: 167772160
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:
struct SmallStack { // store the first 8KiB on the stack data: Cell<[u8; 8192]>, // how much we've already allocated on our stack used: Cell<usize>, }
impl SmallStack { fn new() -> Self { Self { data: Cell::new([0u8; 8192]), used: Cell::new(0), } } }
unsafe impl Allocator for &SmallStack { fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> { // where we are in memory let data = self.data.as_ptr() as *mut u8;
// how much we've used of data already
let used = self.used.get();
// given the requested alignment, how many bytes do we need past data + used
let alignment_padding = data.wrapping_add(used).align_offset(layout.align());
let start = used + alignment_padding; let end = start + layout.size();
// if we have enough on our local stack: if end <= 8192 { self.used.set(end); let ptr = unsafe { NonNull::new_unchecked(data.add(start)) }; Ok(NonNull::slice_from_raw_parts(ptr, layout.size())) } else { // otherwise allocate on the standard heap Global.allocate(layout) } }
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) { let data = self.data.as_ptr().addr();
// if this isn't an address in our data if !(data..data + 8192).contains(&ptr.as_ptr().addr()) { unsafe { Global.deallocate(ptr, layout) }; } } }
Then we can use our toy stack first allocator:
// needs to live longer than our vec let stack = SmallStack::new();
let mut vec: Vec<u32, _> = Vec::new_in(&stack);
// fits in our stack data buffer for i in 0..32 { vec.push(i); }
// will eventually spill to the heap for i in 32..10_000 { vec.push(i); }
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.
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
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。
