contiguous-arena
contiguous-arena is a Rust crate that provides a memory-efficient, contiguous data structure for managing collections of homogeneously-typed elements. It is designed to handle frequent allocations and deallocations efficiently by reusing freed memory spans.
The library is designed to allow for allocation of contiguous elements, such that:
- Appending an arbitrary amount of elements will allocate them in a contiguous memory block (
arena.append(T, count)) - The arena uses
Vec<T>as the backing buffer, unlike most arena crates which bundle metadata together with the item. This crate uses 2 additional buffers to store the span metadata, which makes it easier to iterate the arena with crates likerayonor share it with the GPU, for example.
Features
- Memory-efficient storage by reusing freed spans.
- Contiguous memory layout for improved cache performance, easier iteration and GPU memory sharing.
- Support for arbitrary owned types. Note that the arena DOES NOT currently run
drop()! (a PR would be appreciated) - Only depends on std. A PR adding
#![no_std]support would be appreciated.
Be aware of memory fragmentation when using this library: frequest allocations of different sizes will eventually fragment the arena. This crate is ideal for use-cases where most allocations are about the same size.
Example
use Arena;
Benchmarks and tests
To run tests, run cargo test. To run benchmarks, run cargo bench in the project directory. See benchmark results in target/criterion/report/index.html.
Benchmark results on an Intel Xeon E5-2690 v3 with 2133 MHz DDR4 quad-channel RAM:
append 1 element time: [182.92 ns 191.00 ns 199.09 ns]
append 10 elements time: [166.67 ns 170.09 ns 174.47 ns]
append 100 elements time: [170.19 ns 174.47 ns 179.53 ns]
free 10 elements time: [248.43 ns 254.22 ns 260.94 ns]
free 100 elements time: [248.16 ns 251.57 ns 255.99 ns]
reuse span of 10 elements
time: [245.90 ns 247.98 ns 250.80 ns]
reuse span of 100 elements
time: [311.23 ns 323.52 ns 336.90 ns]
(light travels about 30 meters in 100 nanoseconds)
License
The Unlicense (public domain)