Crate slabigator

Source
Expand description

CI Crates.io Documentation

§Slabigator

A high-performance linked list that doesn’t perform dynamic memory allocations after initialization. It allocates all necessary memory upfront with a fixed capacity, making it ideal for performance-critical applications where memory allocation patterns need to be predictable.

§Features

Slabigator was designed to do a few things extremely well:

  • Add to the head of the list in O(1) time - Returns a stable slot number for future reference
  • Pop from the tail of the list in O(1) time
  • Delete any element given its slot number in O(1) time
  • Fixed memory allocation - No dynamic allocations after initialization

It’s designed to be:

  • Fast: O(1) operations with minimal overhead
  • Predictable: No memory allocations during operations
  • Simple: Small, focused API for specific use cases
  • Maintainable: Small codebase with zero dependencies

§Usage

use slabigator::Slab;

// Create a new slab with a capacity of 3 elements
let mut slab = Slab::with_capacity(3).unwrap();

// Add elements to the front - each operation returns a slot number
let slot_a = slab.push_front("a").unwrap();
let slot_b = slab.push_front("b").unwrap();
let slot_c = slab.push_front("c").unwrap();

// Slab is now full (capacity = 3)
assert!(slab.is_full());
assert_eq!(slab.len(), 3);

// Access elements directly by their slots
assert_eq!(slab.get(slot_a).unwrap(), &"a");
assert_eq!(slab.get(slot_b).unwrap(), &"b");
assert_eq!(slab.get(slot_c).unwrap(), &"c");

// Remove an element by its slot
slab.remove(slot_b).unwrap();
assert_eq!(slab.len(), 2);

// Pop elements from the back (FIFO behavior)
assert_eq!(slab.pop_back().unwrap(), "a");
assert_eq!(slab.pop_back().unwrap(), "c");
assert!(slab.is_empty());

§When to Use Slabigator

Slabigator is ideal for scenarios where:

  • You need to maintain a list with stable references to elements
  • Memory allocation predictability is critical
  • You need fast (O(1)) operations for all common list operations
  • You know the maximum size of the list in advance
  • You need a simple FIFO queue with the ability to remove arbitrary elements

Common use cases include:

  • Real-time systems where allocation jitter is problematic
  • Memory-constrained environments
  • High-performance queues for task management
  • Cache implementations with fixed capacity
  • Game development for object pools

§Cargo Features

Slabigator comes with several feature flags to customize its behavior:

  • releasefast: Assumes that remove() will always be called with a valid index. This saves memory by removing validation checks, but must be used with extreme caution. Disabled by default.
  • slot_u32: Uses u32 as the slot type (default)
  • slot_u64: Uses u64 as the slot type
  • slot_usize: Uses usize as the slot type

§Installation

Add this to your Cargo.toml:

[dependencies]
slabigator = "0.9"

§Trait Implementations

Slabigator implements several useful Rust traits:

  • Default: Creates a slab with a default capacity of 16
  • FromIterator<T>: Creates a slab from any iterator
  • Extend<T>: Extends a slab with elements from an iterator
  • Index<Slot> and IndexMut<Slot>: Allows direct indexing with [] syntax
  • IntoIterator for &Slab<T>: Enables iteration with for loops

§Slabigator

A high-performance linked list with fixed capacity that doesn’t perform dynamic memory allocations after initialization. It provides O(1) operations for adding, removing, and accessing elements.

§Overview

Slabigator is designed for scenarios where memory allocation predictability is critical and you need stable references to elements. It allocates all memory upfront and provides slot numbers as stable references to elements.

§Key Features

  • Fixed capacity - no allocations during operations
  • O(1) push_front operation
  • O(1) pop_back operation
  • O(1) removal of any element by slot
  • Slots provide stable references to elements
  • Implements useful Rust traits like FromIterator and Extend

§Basic Usage

use slabigator::Slab;

// Create a slab with capacity for 3 elements
let mut slab = Slab::with_capacity(3).unwrap();

// Push elements to the front (returns slot numbers)
let a = slab.push_front("a").unwrap();
let b = slab.push_front("b").unwrap();
let c = slab.push_front("c").unwrap();

// Access by slot
assert_eq!(slab.get(a).unwrap(), &"a");
assert_eq!(slab.get(b).unwrap(), &"b");
assert_eq!(slab.get(c).unwrap(), &"c");

// Remove an element
slab.remove(b).unwrap();
assert_eq!(slab.len(), 2);

// Iterate (order is from head to tail)
let elements: Vec<_> = slab.iter().collect();
assert_eq!(elements, vec![&"c", &"a"]);

// Pop from the back (FIFO queue behavior)
assert_eq!(slab.pop_back().unwrap(), "a");
assert_eq!(slab.pop_back().unwrap(), "c");
assert!(slab.is_empty());

§Advanced Features

§Default capacity

use slabigator::Slab;

// Creates a slab with default capacity (16)
let slab: Slab<i32> = Slab::default();
assert_eq!(slab.capacity(), 16);

§Creating from an iterator

use slabigator::Slab;

let values = vec![1, 2, 3, 4, 5];
let slab: Slab<_> = values.iter().copied().collect();

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

§Extending a slab

use slabigator::Slab;

let mut slab = Slab::with_capacity(5).unwrap();
slab.push_front(1).unwrap();
slab.push_front(2).unwrap();

// Extend with more elements
slab.extend(vec![3, 4, 5]);
assert_eq!(slab.len(), 5);

Structs§

Slab
A fixed-capacity linked list that doesn’t perform dynamic memory allocations after initialization.
SlabIterator
An iterator over the elements of a slab.

Enums§

Error
Error types that can occur during Slab operations.

Type Aliases§

Slot
Slot type used for element references. This is u32 by default or when the slot_u32 feature is enabled.