arena-b
A high-performance bump allocator for Rust, designed for scenarios where you need to allocate many temporary objects and deallocate them all at once. Ideal for parsers, game engines, compilers, and request-scoped web server data.
Overview
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.
Quick Start
use Arena;
Features
- High Performance: Bump allocation with thread-local caching and lock-free fast paths
- Memory Safety: Unsafe internals are carefully encapsulated and thoroughly tested
- Checkpoint API: Save and restore allocation state for frame-based or request-scoped patterns
- Zero-Cost Abstractions: Optional features incur no overhead when disabled
- Adaptive Capacity Controls (v0.7):
reserve_additional,shrink_to_fit, andreset_and_shrinklet you grow ahead of allocation spikes and release memory afterward - Lock-Free Object Pool (v0.8): Generic
LockFreePool<T>for zero-contention object reuse in high-frequency allocation patterns - Advanced Debugging (v0.6): Leak detection, validation hooks, and optional backtraces make it easier to audit arena usage during development
- Virtual Memory Instrumentation (v0.6): Inspect committed bytes at runtime and rely on improved macOS/Windows handling for large reserves
What's New in v0.8
- Generic Lock-Free Pool:
LockFreePool<T>provides thread-safe object pooling with atomic CAS operations for game engines, parsers, and high-frequency allocation patterns - Lock-Free Allocator Control:
LockFreeAllocatorwith runtimeenable()/disable()switching andcache_hit_rate()monitoring - Thread-Local Slabs:
ThreadSlabwith generation-based invalidation for per-thread fast-path allocations - Enhanced Statistics:
LockFreeStatsnow supportscache_hit_rate(),record_deallocation(), and isCloneable for snapshots - Debug Improvements:
DebugStatsincludesleak_reportscounter for tracking leak detection calls
What's New in v0.7
- Proactive Reservation: Call
Arena::reserve_additional(bytes)to pre-grow the underlying chunk before a known burst of allocations, reducing contention in hot paths. - Memory Trimming:
Arena::shrink_to_fitandArena::reset_and_shrinkreclaim any extra chunks after a spike, keeping long-running services lean. - Docs & Tooling: README and changelog now cover the adaptive APIs, and CI stays green with the explicit Clippy allowance on
alloc_str_uninit.
What's New in v0.6
- New Allocation APIs:
alloc_slice_fastaccelerates small slice copies andalloc_str_uninitcreates mutable UTF-8 buffers without extra allocations. - Virtual Memory Telemetry:
virtual_memory_committed_bytes()reports the current committed footprint, while rewinds/resets now guarantee proper decommit on every platform. - Lock-Free Overhaul: Per-thread slab caches reduce contention and eliminate previously observed race conditions in the lock-free buffer.
- Panic-Safe Scopes:
Arena::scopenow rolls back automatically even if the scoped closure unwinds, ensuring arenas remain consistent under failure. - Enhanced Debugging: Runtime validation hooks, leak reports, and optional
debug_backtracecapture provide deep diagnostics when thedebugfeature is enabled.
What's New in v0.5
- Checkpoints: Mark allocation points and rewind instantly for bulk deallocation
- Debug Mode: Detect use-after-free bugs with guard patterns and pointer validation
- Virtual Memory: Reserve large address spaces without committing physical memory upfront
- Thread-Local Caching: Reduce lock contention in multi-threaded workloads
- Lock-Free Operations: Minimize synchronization overhead in high-contention scenarios
Installation
Add to your Cargo.toml:
[]
= "0.8"
Or via the command line:
Feature Flags
Enable features based on your requirements:
# Basic bump allocator
= "0.8"
# With debug safety checks (recommended for development)
= { = "0.8", = ["debug"] }
# Full feature set for maximum performance
= { = "0.8", = ["debug", "virtual_memory", "thread_local", "lockfree"] }
| Feature | Description |
|---|---|
debug |
Memory safety validation and use-after-free detection |
virtual_memory |
Efficient handling of large allocations via reserve/commit |
thread_local |
Per-thread allocation buffers to reduce contention |
lockfree |
Lock-free operations for concurrent workloads |
stats |
Allocation statistics tracking (enabled by default) |
Usage Examples
Frame-Based Allocation
Suitable for game loops or per-request processing:
use Arena;
AST Construction for Parsers
Build complex data structures without manual memory management:
use Arena;
Thread-Safe Allocation
Use SyncArena for concurrent access:
use Arc;
use SyncArena;
Performance Characteristics
Typical performance improvements over standard allocation:
- vs
Box: 10–50× faster for allocation-intensive workloads - vs
Vec: 5–20× faster when bulk reset is applicable - Memory overhead: ~64 bytes per chunk; negligible per-allocation overhead
- Thread contention: Minimal when thread-local features are enabled
Run benchmarks locally:
When to Use arena-b
Recommended Use Cases:
- Parsers and compilers (ASTs, intermediate representations)
- Game engines (per-frame temporary allocations)
- Web servers (per-request data structures)
- Any workload with many short-lived objects
Less Suitable For:
- Long-lived objects with heterogeneous lifetimes
- Applications with minimal allocation requirements
- Scenarios requiring fine-grained memory control
API Reference
use Arena;
let arena = new;
// Basic allocation
let number = arena.alloc;
let string = arena.alloc_str;
let small = arena.alloc_slice_fast;
let buf = arena.alloc_str_uninit; // mutable UTF-8 buffer
// Scoped allocation with automatic cleanup (panic-safe)
arena.scope;
// Checkpoint-based bulk deallocation
let checkpoint = arena.checkpoint;
// ... perform allocations ...
unsafe
// Virtual memory telemetry (feature = "virtual_memory")
if let Some = arena.virtual_memory_committed_bytes
// Statistics
println!;
println!;
Documentation
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 details
Examples
Working 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 features
Contributing
Contributions are welcome. Please consider the following:
- Bug reports and feature requests via GitHub Issues
- Performance improvements with benchmark data
- Documentation corrections and improvements
- For significant changes, please open an issue for discussion first
License
Licensed under the MIT License. See LICENSE for details.
Changelog
See CHANGELOG.md for complete version history.
v0.8.0
- Generic
LockFreePool<T>for thread-safe object pooling LockFreeAllocatorwith runtime enable/disable controlThreadSlabfor per-thread fast-path allocations- Enhanced
LockFreeStatswithcache_hit_rate()andClone DebugStatsnow includesleak_reportsfield
v0.7.0
reserve_additionalfor proactive capacity growthshrink_to_fitandreset_and_shrinkfor memory trimming
v0.6.0
- AVX2/NEON SIMD optimizations
alloc_slice_fastandalloc_str_uninitAPIs- Virtual memory telemetry and cross-platform fixes
- Panic-safe scopes
v0.5.0
- Checkpoint and rewind API for bulk deallocation
- Debug mode with memory safety validation
- Virtual memory support for large allocations
- Thread-local caching and lock-free operations