[][src]Crate compact_arena

A crate with a few single-typed arenas that work exclusively with indexes. The indexes are sized with the arena. This can reduce memory footprint when changing pointers to indices e.g. on 64-bit systems.

The arenas use "branded indices": The indices contain lifetimes that bind them to the arena so you cannot mix up two arenas by accident. See Gankro's thesis for more information about the concept and it's implementation in Rust.

Use the in_arena and similar methods to run code within the scope of an arena:

Examples

compact_arena::in_arena(|arena| {
    let idx = arena.add(1usize);
    assert_eq!(1, arena[idx]);
});

You can use in_sized_arena to change the initial size of the backing memory:

compact_arena::in_sized_arena(1, |arena| {
    ..
});

You can nest calls to work with multiple arenas:

compact_arena::in_sized_arena(1, |a| {
    compact_arena::in_sized_arena(1, |b| {
        ..
    })
});

The compiler will give you a scary lifetime error if you mix up arenas:

This example deliberately fails to compile
compact_arena::in_sized_arena(8, |a| {
    compact_arena::in_sized_arena(8, |b| {
        let i = a.add(1usize);
        b[i]
    }
}

Structs

Idx

An index into the arena

NanoArena

A "nano" arena containing 255 elements. This variant only works with types implementing Default.

SmallArena

A "Small" arena based on a resizable slice (i.e. a Vec) that can be indexed with 32-bit Idx32s. This can help reduce memory overhead when using many pointer-heavy objects on 64-bit systems.

TinyArena

A "tiny" arena containing <64K elements. This variant only works with types implementing Default.

Functions

in_arena

Run code using an arena. The indirection through a FnOnce is required to bind the indices to the arena.

in_nano_arena

Run code using a nano arena. The indirection through a FnOnce is required to bind the indices to the arena. This version only works for types that implement Default and Copy. You can use the uninit feature to remove that restriction, at the cost of some unsafe code.

in_sized_arena

Same as with, but allows specifying the initial size of the arena.

in_tiny_arena

Run code using a tiny arena. The indirection through a FnOnce is required to bind the indices to the arena. This version only works for types that implement Default and Copy. You can use the uninit feature to remove that restriction, at the cost of some unsafe code.

Type Definitions

Idx8

The index type for a nano arena is 8 bits large

Idx16

The index type for a tiny arena is 16 bits large

Idx32

The index type for a small arena is 32 bits large