#allocator #slab #memory-allocator #no-std

no-std slabby

Maximally efficient allocation and deallocation of a large number of instances of a type

2 releases

0.3.1 May 9, 2024
0.3.0 May 9, 2024
0.2.0 May 9, 2024
0.1.2 May 7, 2024

#243 in Memory management

Download history 1/week @ 2024-09-22 33/week @ 2024-09-29

169 downloads per month

MIT license

10KB
159 lines

slabby

Not production ready. Do not use in production.

crates.io docs.rs

This crate provides maximally efficient allocation and deallocation of a large number of instances of a single type.

You can choose the size of the key for each instance of a Slab, which allows you to use a type smaller than a pointer to hold the indices to your items, which can improve memory locality, at the cost of imposing a limit on the number of elements that can be stored.

Due to the design of this Slab being optimized for memory usage and efficiency of the common cases (adding, removing, and retrieving elements) it liberally uses unsafe code, cannot provide a safe user-facing API, and some operations are more expensive than expected. All such operations are documented.

Usage

let mut slab = slabby::Slab32::new();
unsafe {
    let key1 = slab.insert(1);
    let key2 = slab.insert(2);
    let key3 = slab.insert(3);

    assert_eq!(slab.get(key1), &1);
    assert_eq!(slab.get(key2), &2);
    assert_eq!(slab.get(key3), &3);

    assert_eq!(slab.remove(key2), 2);
    assert_eq!(slab.remove(key1), 1);

    assert_eq!(slab.get(key3), &3);

    slab.insert(4);
    let key5 = slab.insert(5);
    slab.insert(6);

    assert_eq!(slab.len(), 4);

    *slab.get_mut(key5) += 1;
    assert_eq!(slab.remove(key5), 6);

    assert_eq!(slab.len(), 3);
}

No runtime deps