[][src]Crate bumpalo_herd

The Bumpalo Herd

The bumpalo library let's one use a bump allocator, an interesting and fast strategy to allocate a lot of small objects. Additionally, it helps solve some of Rust lifetime issues.

Nevertheless, it is not Sync, which makes it hard to use in many situations ‒ like in [rayon] iterators or scoped threads.

This library extends bumpalo with the Herd type. It represents a group of the Bump allocators. Each thread then can get its own instance to allocate from. Unlike just creating one for each thread the convenient way, the allocated memory can survive past the thread/iterator termination, because the lifetime is tied to the Herd itself (the Bump is rescued from the thread behind the scenes).


We use the bump allocation inside a [rayon] iterator. The map_init allows us to grab an arena and use it in each invocation cheaply, but unlike Bump::new allocated one, the memory is usable after the iterator run to its termination.

// Bunch of Bump instances that can be borrowed
let mut herd = Herd::new();

let ints: Vec<&mut usize> = (0usize..1_000)
    .map_init(|| herd.get(), |bump, i| {
        // We have an allocator here we can play with.
        // The computation would be something bigger, sure.

// Available here even though the iterator has already ended.

// Deallocate the memory

// Won't work any more, memory gone
// (don't worry, Rust won't let us)
// dbg!(ints);

Similar thing can be done with scoped threads from [crossbeam_utils]. If we got our allocator through Bump::new, we wouldn't be allowed to use the results in the other thread.

let herd = Herd::new();
let (sender, receiver) = mpsc::sync_channel(10);

// Scoped threads from crossbeam_utils.
// Producer and consumer, can send data from one to another.
// The data lives long enough.
thread::scope(|s| {
    s.spawn(|_| {
        let bump = herd.get();
        let s = bump.alloc_str("Hello");
        drop(sender); // Close the channel.
    s.spawn(|_| {
        for s in receiver {



A group of Bump allocators.


A proxy for a Bump.