farena
File-backed arena allocator using pread for random access.
Write data to a temporary file, then read it back by location. The data stays on disk instead of in memory, so your process doesn't use extra RAM.
pread lets us read from any offset without seeking, which means:
- No file position to manage between reads
- Thread-safe: multiple threads can read concurrently without locking
Use this when you need scratch space for bytes but can't afford to keep everything in memory.
Limitations
- Each file is limited to 4GB (u32 offsets). For larger data, use multiple files.
FileArenais immutable once built. To add more data, create a new writer, then build a newFileArenacontaining all files.- Temp files use your system's temp directory (
TMPDIR). This crate doesn't check if it's on real disk - make sure it's not a ramdisk liketmpfsorramfs. - This crate does many random reads. Use a fast storage for best performance.
- Each file in a
FileArenakeeps one file descriptor open for its lifetime. Creating arenas with thousands of files may hit your system's ulimit. Check withulimit -nand monitor withlsof -p $$ | wc -l. Increase the limit or reduce file count if needed.
Building multi-file arenas
Use FileArenaBuilder to safely assemble arenas from multiple writers.
It handles file placement automatically, so you don't need to worry
about the ordering contract:
let mut w0 = new?;
let loc0 = w0.push?;
let f0 = w0.finish?;
let mut w1 = new?;
let loc1 = w1.push?;
let f1 = w1.finish?;
let mut builder = new;
builder.add; // Order doesn't matter
builder.add;
let arena = builder.build?;
Usage
use ;
// Write phase
let mut writer = new?;
let loc1 = writer.push?;
let loc2 = writer.push?;
// Read phase — into_arena() is a convenience for single-file arenas
let arena = writer.into_arena?;
assert_eq!;
assert_eq!;
Multiple files (low-level)
Prefer FileArenaBuilder above — it enforces the ordering
contract automatically. FileArena::new is the low-level alternative.
Each writer gets a unique index. Files must be passed to
FileArena::new in index order:
let mut w1 = new?;
let loc1 = w1.push?;
let f1 = w1.finish?;
let mut w2 = new?;
let loc2 = w2.push?;
let f2 = w2.finish?;
let arena = new?;
assert_eq!;
assert_eq!;
Parallel writing
The design supports parallel writing. Each writer gets a unique index,
and FileArenaBuilder handles assembling the arena:
let items = vec!;
// Use .into_par_iter() with rayon for parallel execution
let results: =
.into_iter
.map
.collect;
// Builder places files in the correct order automatically
let mut builder = new;
for in results
let arena = builder.build?;
Graph/tree structures
A common pattern is storing node metadata in memory while keeping large payloads on disk. This is useful when:
- Payloads are large and would consume too much memory
- You need to traverse the structure without loading all data at once
- You construct long text by concatenating payloads (e.g., thread content)
For example, a tree where each node has an ID and a text payload:
// Build your tree with Locations instead of storing text directly
let mut nodes = Vecnew;
let mut writer = new?;
// Write payloads, store locations
for in &
let arena = writer.into_arena?;
// Traverse and read payloads as needed
// Note: get_str_into appends, so we create a fresh buffer each iteration
for node in &nodes
// Or concatenate payloads into a single buffer
let mut full_text = Stringnew;
for node in &nodes
// full_text now contains all payloads concatenated
Buffer reuse
Reuse the same buffer across multiple reads to avoid allocations:
let mut buf = Vecnew;
arena.get_into?;
assert_eq!;
buf.clear; // Reuse without reallocating
arena.get_into?;
assert_eq!;
Unsafe reads
If you know your stored data is valid UTF-8, use get_str_into_unchecked
to skip the UTF-8 validation:
let mut buf = Stringnew;
// SAFETY: we pushed valid UTF-8 above
unsafe ?;
assert_eq!;
Temp directory
Temp files are created in your system's temp directory (respects TMPDIR).
Check your temp directory is on real disk with:
df -h ${TMPDIR:-/tmp}
The filesystem should not be tmpfs or ramfs.
License: MIT