mago-allocator 1.40.0

Arena allocation for Mago: a unified `Arena` trait over thread-local and thread-safe bump allocators.
Documentation

Arena allocation for Mago.

Three bump arenas, all implementing the [Arena] trait:

  • [LocalArena]: single-threaded, fastest, Send but !Sync.
  • [SharedArena]: Send + Sync, safe to share across threads (for example behind a &SharedArena handed to every worker in a rayon pass).
  • [ScopedArena]: a thread-local view into a [SharedArena] (via [SharedArena::scoped]) for contention-free, non-escaping scratch.

Because all three implement [Arena] (and the underlying Allocator trait), allocation code is written generically over A: Arena and never needs to know which arena it was given:

fn lower<'arena, A: Arena + ?Sized>(arena: &'arena A, ast: &Ast) -> &'arena Ir<'arena> {
    arena.alloc(Ir::from(ast))
}

Allocations live as long as the borrow of the arena they came from, and are reclaimed wholesale when the arena is dropped or reset. For growable collections, use the aliases in vec, [boxed], and [collections], each built with its *_in(arena) constructor; to collect an iterator straight into the arena, use [CollectIn] (and ParallelCollectIn under the rayon feature); for an immutable string copied into the arena, use [Arena::alloc_str]; to deep-copy an arena-resident value into a different arena, implement [CopyInto].

Each module also re-exports the items of its underlying crate (allocator_api2 for alloc, [boxed], vec; hashbrown for [collections]), so anything not surfaced by the aliases above is still reachable.