bittern
A reference-counted arena for interning and deduplicating data.
Bittern provides Arena<T>, a collection type with roughly three duties:
- Bump allocation: a blazing fast arena for any type, including dynamically sized slices.
- Indexing: items can be accessed and interned by their hash or unique key.
- Reference counting: the arena will live as long as any references to it or its data.
Bump allocators are appropriate for use cases where items will be allocated gradually, but dropped as a group.
That makes bittern ideal for building large graphs, symbol tables, and other deduplicated collections.
Compatibility
This crate fully supports #![no_std] environments. It depends only on core and alloc.
Examples
1) String interning / Symbol table
When interning dynamically sized slices or strings, bittern can store values together in a chunk of allocated memory.
- Fewer allocations and better locality compared to many individual
StringorVec. - Slices are interned into a pointer, which greatly improves the performance of equality checks.
// Demonstrates how to intern strings, without wrangling lifetimes or individual String allocations
use ;
2) Abstract syntax tree
This crate is well suited for building graphs and trees with many identical nodes.
The following example demonstrates a math interpreter that merges equivalent subexpressions.
// Demonstrates a simple expression interpreter using an arena-allocated syntax tree.
// The language uses Lisp-like prefix notation with optional parentheses.
// Feature "derive" must be enabled
use ;
use Hash;
type Int = i64;
type Name = str;
// An expression tree or subtree.
// Strong<Name> is a strong ref, so the Name arena will live until the Expr is dropped.
// Weak<Expr> is a weak ref, so expressions may reference others within the same arena.
// Parses input into an AST.
// Identical expressions will be interned into a single node.
// Evaluates the AST.
// SecondaryMap associates a Strong<Name> with a value
3) Primary keys
Sometimes data should be deduplicated by a single field, rather than the entire struct.
This demonstrates a User struct identified by its id field.
// Demonstrates an arena of Users, deduplicated by their id field.
// Feature "derive" must be enabled
use RefCell;
use ;