| Crates.io | stack-arena |
| lib.rs | stack-arena |
| version | 0.12.0 |
| created_at | 2025-05-24 14:28:26.791105+00 |
| updated_at | 2025-06-10 06:02:22.874065+00 |
| description | A fast, stack-like arena allocator for efficient memory management, implemented in Rust. |
| homepage | https://github.com/enterprise-search/stack-arena |
| repository | https://github.com/enterprise-search/stack-arena |
| max_upload_size | |
| id | 1687488 |
| size | 111,683 |
A fast, stack-like arena allocator for efficient memory management, implemented in Rust.
Allocator trait for compatibility with allocation APIsStack-arena is ideal for scenarios where:
The library is organized in layers:
Chunk - The lowest level, representing a contiguous region of memoryBufferArena - Manages a single memory chunk with bump allocationStackArena - Manages multiple chunks with LIFO allocation/deallocationObjectStack - High-level interface for building objects incrementallyAdd to your Cargo.toml:
[dependencies]
stack-arena = "0.11"
The ObjectStack provides a user-friendly interface for building and managing objects:
use stack_arena::ObjectStack;
use std::fmt::Write;
let mut stack = ObjectStack::new();
// Push a complete object
stack.push(b"hello ");
// Build an object incrementally
stack.extend("world");
write!(&mut stack, "!").unwrap();
let ptr = stack.finish();
// Access the object
let hello_world = unsafe { std::str::from_utf8_unchecked(ptr.as_ref()) };
assert_eq!(hello_world, "hello world!");
// Pop the object when done
stack.pop();
The StackArena provides more control over memory allocation:
use stack_arena::{StackArena, Allocator};
use std::alloc::Layout;
// Create with default chunk size (4096 bytes)
let mut arena = StackArena::new();
// Or create with a custom chunk size
let mut custom_arena = StackArena::with_chunk_size(8192);
// Allocate memory
let layout = Layout::from_size_align(10, 1).unwrap();
let ptr = unsafe { arena.allocate(layout).unwrap() };
// Use the memory...
unsafe { std::ptr::write_bytes(ptr.as_ptr() as *mut u8, 0xAA, 10) };
// Deallocate when done
unsafe { arena.deallocate(ptr.cast(), layout) };
The library is optimized for scenarios with many small allocations that follow a stack-like pattern. Benchmarks show it significantly outperforms the system allocator in these cases:
Contributions are welcome! Please feel free to submit a Pull Request.
MIT