2 unstable releases

0.2.0 Jan 23, 2023
0.1.0 Nov 8, 2022

#573 in Memory management

Download history 286/week @ 2024-01-02 394/week @ 2024-01-09 560/week @ 2024-01-16 376/week @ 2024-01-23 324/week @ 2024-01-30 398/week @ 2024-02-06 344/week @ 2024-02-13 336/week @ 2024-02-20 365/week @ 2024-02-27 393/week @ 2024-03-05 308/week @ 2024-03-12 383/week @ 2024-03-19 432/week @ 2024-03-26 446/week @ 2024-04-02 299/week @ 2024-04-09 344/week @ 2024-04-16

1,583 downloads per month

MIT license

67 lines

BumpSlab: A bump allocator with reusable slots


A bumpslab is a slab that provides stable references for items inside the collection.

Normally, when a vec grows, its items will move around. Bumpslab is an arena allocator that gives you the pointer, not the key. This lets you chase down the pointer contents without the arena required to use the value. This means the pointer is guaranteed stable for the lifetime of the Slot<>


struct MyThing(usize);

let slab = BumpSlab::new();

let a = slab.push(MyThing(0));
let b = slab.push(MyThing(1));
let c = slab.push(MyThing(2));

let last = slab.push(MyThing(3));

let ptr = last.ptr();

let new_last = slab.push(Mything(4));
let new_ptr = new_last.ptr();

// Slots get reused, known by pointer
assert_eq!(ptr, new_ptr);

When to use BumpSlab:

  • you need stable references (ie driving futures without Arc for waker)
  • you have only one type
  • iteration is less important to you than lookups

Use as an Arc alternative:

The primary motivation for this crate was to enable spawning batches of futures without requiring Arc for the custom waker code. Wakers require a stable reference, typically guaranteed by Arc. However, if you're spawning many futures, this could come with some allocation overhead. BumpSlab gives you a stable reference for the lifetime of the waker and reuses slots as they are freed, keeping superior cache locality when compared to Arc.