| Crates.io | arena-b |
| lib.rs | arena-b |
| version | 1.0.0 |
| created_at | 2025-11-18 06:08:38.612778+00 |
| updated_at | 2026-01-01 03:56:56.411061+00 |
| description | Production-grade bump allocator with lock-free, slab, and virtual-memory tooling for parsers, game engines, and request-scoped services |
| homepage | https://quefep.uk |
| repository | https://github.com/M1tsumi/arena-b |
| max_upload_size | |
| id | 1937944 |
| size | 301,782 |
Ultra-fast bump allocation arena for high-performance Rust applications
arena-b is a compact, battle-tested bump allocator for workloads that allocate many short-lived objects and prefer bulk reclamation. Allocate objects quickly into contiguous chunks and free them all at once using checkpoints, scopes, or a full reset — eliminating per-object deallocation overhead and fragmentation.
Arena allocation follows a simple principle: allocate objects sequentially into a contiguous buffer, then free everything simultaneously when the arena is reset or dropped. This approach eliminates per-object deallocation overhead and avoids memory fragmentation entirely.
use arena_b::Arena;
fn main() {
let arena = Arena::new();
// Allocate objects into the arena
let numbers: Vec<&u32> = (0..1000)
.map(|i| arena.alloc(i))
.collect();
// All allocations are freed when the arena is dropped
// Alternatively, call arena.reset() to free manually
}
checkpoint/rewind_to_checkpoint) and scope() for panic-safe temporary allocations.debug feature is enabled.virtual_memory, thread_local, lockfree, slab, debug, stats.ArenaBuilder to configure arenas declaratively (chunk size, reserve size, thread safety, diagnostics sink).Arena::with_virtual_memory logs and falls back in restricted environments — check logs if you require strict failure behavior.Arena::chunk_usage(), virtual_memory_committed_bytes(), and LockFreeStats::cache_hit_rate().See CHANGELOG.md for the complete release notes and migration tips.
LockFreePool<T> provides thread-safe object pooling with atomic CAS operations for game engines, parsers, and high-frequency allocation patternsLockFreeAllocator with runtime enable()/disable() switching and cache_hit_rate() monitoringThreadSlab with generation-based invalidation for per-thread fast-path allocationsLockFreeStats now supports cache_hit_rate(), record_deallocation(), and is Cloneable for snapshotsDebugStats includes leak_reports counter for tracking leak detection callsArena::reserve_additional(bytes) to pre-grow the underlying chunk before a known burst of allocations, reducing contention in hot paths.Arena::shrink_to_fit and Arena::reset_and_shrink reclaim any extra chunks after a spike, keeping long-running services lean.alloc_str_uninit.alloc_slice_fast accelerates small slice copies and alloc_str_uninit creates mutable UTF-8 buffers without extra allocations.virtual_memory_committed_bytes() reports the current committed footprint, while rewinds/resets now guarantee proper decommit on every platform.Arena::scope now rolls back automatically even if the scoped closure unwinds, ensuring arenas remain consistent under failure.debug_backtrace capture provide deep diagnostics when the debug feature is enabled.Add arena-b to your Cargo.toml:
[dependencies]
arena-b = "1.0.0"
# Add to your project
cargo add arena-b
# Run examples
cargo run --example parser_expr
cargo run --example game_loop
arena-b uses feature flags to minimize compilation overhead:
# Basic bump allocator
arena-b = "1.0.0"
# Development with safety checks
arena-b = { version = "1.0.0", features = ["debug"] }
# Maximum performance for production
arena-b = { version = "1.0.0", features = ["virtual_memory", "thread_local", "lockfree", "slab"] }
| Feature | Description | Performance Impact | When to Use |
|---|---|---|---|
debug |
Memory safety validation and use-after-free detection | ~5% overhead | Development & testing |
virtual_memory |
Efficient handling of large allocations via reserve/commit | Memory efficient | Large arena allocations |
thread_local |
Per-thread allocation buffers to reduce contention | 20-40% faster | Multi-threaded workloads |
lockfree |
Lock-free operations for concurrent workloads | 15-25% faster | High-contention scenarios |
stats |
Allocation statistics tracking | Minimal overhead | Performance monitoring |
slab |
Size-class cache for small allocations | 10-20% faster | Mixed allocation sizes |
Suitable for game loops or per-request processing:
use arena_b::Arena;
fn game_loop() {
let arena = Arena::new();
loop {
let checkpoint = arena.checkpoint();
// Allocate frame-specific data
let entities = allocate_entities(&arena);
let particles = allocate_particles(&arena);
// Process the frame...
// Deallocate all frame data at once
unsafe { arena.rewind_to_checkpoint(checkpoint); }
}
}
Build complex data structures without manual memory management:
use arena_b::Arena;
struct AstNode<'a> {
value: String,
children: Vec<&'a AstNode<'a>>,
}
fn parse_expression<'a>(input: &str, arena: &'a Arena) -> &'a AstNode<'a> {
let node = arena.alloc(AstNode {
value: input.to_string(),
children: Vec::new(),
});
// Child nodes are allocated in the same arena
// All memory is freed when the arena is dropped
node
}
Use SyncArena for concurrent access:
use std::sync::Arc;
use arena_b::SyncArena;
fn main() {
let arena = Arc::new(SyncArena::new());
let handles: Vec<_> = (0..4)
.map(|_| {
let arena = Arc::clone(&arena);
std::thread::spawn(move || {
arena.scope(|scope| {
scope.alloc("thread-local data")
})
})
})
.collect();
for handle in handles {
handle.join().unwrap();
}
}
| Operation | arena-b | std::alloc Box | std::alloc Vec | Improvement |
|---|---|---|---|---|
| Small allocations (≤64B) | ~5ns | ~50ns | ~25ns | 10x faster |
| Medium allocations (≤4KB) | ~20ns | ~200ns | ~100ns | 10x faster |
| Large allocations (>4KB) | ~100ns | ~500ns | ~300ns | 5x faster |
| Bulk reset | ~10ns | N/A | N/A | Instant |
| Memory overhead | ~64B/chunk | ~16B/object | ~24B/object | Minimal |
Run the comprehensive benchmark suite:
cargo bench --all
View detailed performance reports in benches/ directory.
arena-bRecommended use cases:
Not suitable for:
use arena_b::Arena;
let arena = Arena::new();
// Basic allocation
let number = arena.alloc(42u32);
let string = arena.alloc_str("hello world");
let small = arena.alloc_slice_fast(&[1u8, 2, 3]);
let buf = arena.alloc_str_uninit(256); // mutable UTF-8 buffer
// Scoped allocation with automatic cleanup (panic-safe)
arena.scope(|scope| {
let temp = scope.alloc("temporary data");
// Automatically rewound even if this closure panics
assert_eq!(temp, &"temporary data");
});
// Checkpoint-based bulk deallocation
let checkpoint = arena.checkpoint();
// ... perform allocations ...
unsafe { arena.rewind_to_checkpoint(checkpoint); }
// Virtual memory telemetry (feature = "virtual_memory")
#[cfg(feature = "virtual_memory")]
if let Some(bytes) = arena.virtual_memory_committed_bytes() {
println!("Currently committed: {} bytes", bytes);
}
// Statistics
println!("Allocated: {} bytes", arena.bytes_allocated());
println!("Stats: {:?}", arena.stats());
Additional documentation is available in the docs/ directory:
docs/guide.md — Comprehensive usage guidedocs/strategies.md — Allocation strategy selectiondocs/advanced.md — Advanced configuration optionsdocs/architecture.md — Internal design and implementation detailsWorking examples are provided in the examples/ directory:
parser_expr.rs — Expression parser with arena-allocated ASTgame_loop.rs — Game loop with frame-based allocationgraph_pool.rs — Graph traversal with object poolingstring_intern.rs — String interning implementationv0.5_features.rs — Demonstration of v0.5 featuresContributions are welcome. Please consider the following:
Licensed under the MIT License. See LICENSE for details.
See CHANGELOG.md for complete version history.
slab feature)Arena::chunk_usage() telemetry for per-chunk capacity/used