Arena trees in Rust
An arena stores every node of a tree in one vector and links nodes by index instead of by pointer.
Why use one
Traversal is cheap, removal is a flag, and nothing is reference-counted. The cost is that an index is not tied to the arena that made it.
- One allocation for the whole tree
- Indexes are
Copy
A minimal arena
Here is the whole idea, with indexes as handles:
struct Arena { nodes: Vec<Node> }
struct NodeId(usize);
Removing nodes
Removal sets a flag. The slot stays, so every other index stays valid.
Comments
Great post! Subscribe to my channel.