masstree
A high-performance concurrent ordered map for Rust. It stores keys as &[u8] and supports variable-length keys by building a trie of B+trees, based on the Masstree paper
Disclaimer: This is an independent implementation. It is not endorsed by, affiliated with, or connected to the original Masstree authors or their institutions.
Features
- Ordered map for byte keys (lexicographic ordering)
- Lock-free reads with version validation
- Concurrent inserts and deletes with fine-grained leaf locking
- Zero-copy range scans with
scan_refandscan_prefix - Memory reclamation via epoch-based deferred cleanup
- Lazy leaf coalescing for deleted entries
- Two node widths:
MassTree(WIDTH=24) andMassTree15(WIDTH=15)
Status
v0.3.0 — Core feature complete. It has been heavily tested but I am not sure about whether it should be usd in actual projects. Such low-level cncurrent data structures usually need a lot of stress testing and have a lot of edge cases that are not easily noticeable. The unsafe code passes miri with strict-provenance flag, but that doesn't really ensure correctness.
| Feature | Status |
|---|---|
get, get_ref |
Lock-free with version validation |
insert |
Fine-grained leaf locking |
remove |
Concurrent deletion with memory reclamation |
scan, scan_ref, scan_prefix |
Zero-copy range iteration |
| Leaf coalescing | Lazy queue-based cleanup |
| Memory reclamation | Seize-based epoch reclamation |
Tests: 755 tests (466 unit + 88 ported from C++ reference + integration). Miri strict provenance clean.
Not yet implemented: Entry API, DoubleEndedIterator, Extend/FromIterator.
Install
[]
= { = "0.3", = ["mimalloc"] }
MSRV is Rust 1.92+ (Edition 2024).
The mimalloc feature sets the global allocator. If your project already uses a custom allocator, omit this feature.
Quick Start
use MassTree;
let tree: = new;
let guard = tree.guard;
// Insert
tree.insert_with_guard.unwrap;
tree.insert_with_guard.unwrap;
// Point lookup
assert_eq!;
// Remove
tree.remove_with_guard.unwrap;
assert_eq!;
// Range scan (zero-copy)
tree.scan_ref;
// Prefix scan
tree.scan_prefix;
Ergonomic APIs
For simpler use cases, auto-guard versions create guards internally:
use MassTree;
let tree: = new;
// Auto-guard versions (simpler but slightly more overhead per call)
tree.insert.unwrap;
tree.insert.unwrap;
assert_eq!;
assert_eq!;
assert!;
tree.remove.unwrap;
Range Iteration
use ;
let tree: = new;
let guard = tree.guard;
// Populate
for i in 0..100u64
// Iterator-based range scan
for entry in tree.range
// Full iteration
for entry in tree.iter
When to Use
May work well for:
- Long keys with shared prefixes (URLs, file paths, UUIDs)
- Range scans over ordered data
- Mixed read/write workloads
- High-contention scenarios (the trie structure helps here)
Consider alternatives for:
- Unordered point lookups →
dashmap - Pure insert-only workloads →
scc::TreeIndex - Integer keys only →
congee(ART-based) - Read-heavy with rare writes →
RwLock<BTreeMap>
Variant Selection
Two variants are provided with different performance characteristics:
| Variant | Best For |
|---|---|
MassTree15 |
Range scans, writes, shared-prefix keys, contention |
MassTree (WIDTH=24) |
Random-access reads, single-threaded point ops |
MassTree15 tends to perform better in our benchmarks due to cheaper u64 atomics and better cache utilization. Consider it for most workloads unless you have uniform random-access patterns.
use ;
// Default: WIDTH=24, Arc-based storage
let tree: = new;
// WIDTH=15, Arc-based storage (recommended for most workloads)
let tree15: = new;
// Inline storage for Copy types (no Arc overhead)
let inline: = new;
let inline15: = new;
Benchmarks
6 physical cores, mimalloc allocator, 200 samples per benchmark. Your mileage may vary.
Mixed Read/Write (90% read, 10% write, 6 threads)
| Workload | MassTree15 | IndexSet | TreeIndex | SkipMap |
|---|---|---|---|---|
| Uniform | 19.3 M/s | 10.3 M/s | 11.0 M/s | 7.8 M/s |
| Zipfian | 21.9 M/s | 5.0 M/s | 9.5 M/s | 8.5 M/s |
| High contention | 51.7 M/s | 3.7 M/s | 12.0 M/s | 11.4 M/s |
| Single hot key | 16.7 M/s | 3.3 M/s | 3.5 M/s | 5.4 M/s |
The high-contention result likely reflects the per-node versioning design. Pure insert workloads favor TreeIndex.
Pure Read (6 threads)
| Workload | MassTree15 | IndexSet | TreeIndex | SkipMap |
|---|---|---|---|---|
| Uniform | 27.5 M/s | 12.8 M/s | 19.2 M/s | 12.0 M/s |
| 8-byte keys | 35.2 M/s | 13.8 M/s | 15.9 M/s | 9.6 M/s |
Single-Thread Latency
| Structure | Read Latency |
|---|---|
| MassTree15 | 771 µs |
| TreeIndex | 1,310 µs |
| IndexSet | 1,377 µs |
| SkipMap | 1,864 µs |
vs C++ Reference (6 threads)
| Workload | Rust | C++ | Ratio |
|---|---|---|---|
| 98% reads (rw2g98) | 37.68 M/s | 19.1 M/s | 197% |
| 90% reads (rw2g90) | 29.58 M/s | 13.5 M/s | 219% |
| Hotspot contention (same) | 6.67 M/s | 2.57 M/s | 259% |
| Updates (uscale) | 17.68 M/s | 9.3 M/s | 190% |
| Sequential keys (rw3) | 33.91 M/s | 39.3 M/s | 86% |
| Reverse sequential (rw4) | 27.22 M/s | 35.9 M/s | 76% |
Mixed results. Performs well on contention-heavy workloads but trails on sequential key patterns (76-86%). The C++ implementation has optimizations for sequential access that aren't yet ported.
Note: This implementation diverges from C++ in several ways (notably hyaline-based memory reclamation via seize). Direct comparison is imperfect.
vs RwLock<BTreeMap> (6 threads)
The main question: when does a complex lock-free structure beat a simple RwLock<BTreeMap>?
Mixed read/write workloads (where MassTree is designed to help):
| Workload | MassTree15 | std::RwLock | parking_lot |
|---|---|---|---|
| 90/10 uniform | 13.6 M/s | 3.2 M/s | 5.2 M/s |
| 95/5 zipfian (hot keys) | 36.5 M/s | 6.8 M/s | 10.8 M/s |
MassTree's lock-free reads and per-node versioning help when writers need to make progress. The 2-5x advantage shows up when there's actual contention.
Pure read workloads (where RwLock naturally wins):
| Workload | MassTree15 | RwLock (batched) |
|---|---|---|
| Point reads | 13.2 M/s | 13.0 M/s |
| Range scans | 125 M/s | 1.2 G/s |
For read-only workloads, RwLock has minimal overhead (one atomic to acquire) while MassTree pays for version validation on every access. Range scans are particularly lopsided because RwLock holds the lock for the entire scan. This is expected - lock-free structures pay complexity costs that only matter under contention.
How It Works
Masstree splits keys into 8-byte chunks, creating a trie where each node is a B+tree:
Key: "users/alice/profile" (19 bytes)
└─ Layer 0: "users/al" (8 bytes)
└─ Layer 1: "ice/prof" (8 bytes)
└─ Layer 2: "ile" (3 bytes)
Keys with shared prefixes share upper layers, making lookups efficient for hierarchical data.
Examples
The examples/ directory contains comprehensive usage examples:
Rayon Integration
MassTree works seamlessly with Rayon for parallel bulk operations:
use MassTree15Inline;
use *;
use Arc;
let tree: = new;
// Parallel bulk insert (~10M ops/sec)
.into_par_iter.for_each;
// Parallel lookups (~45M ops/sec)
let sum: u64 = .into_par_iter
.map
.sum;
Tokio Integration
MassTree is thread-safe but guards cannot be held across .await points:
use MassTree15;
use Arc;
let tree: = new;
// Spawn async tasks that share the tree
let handle = spawn;
// For CPU-intensive operations, use spawn_blocking
let tree_clone = clone;
spawn_blocking.await;
Crate Features
mimalloc— Use mimalloc as global allocator (recommended)tracing— Enable structured logging tologs/masstree.jsonl
License
MIT. See LICENSE.
References
AI Assist Disclaimer
It should be obvious that such a high number of tests, benchmarks and docs could not be written by hand this fast. Even though the full design and implementation was written by hand, there's still a a significant amount of AI generated code. I have gone through most of the docs,tests and benches to ensure correctness and also added the 'prompt' for the agent I used to analyze C++ codebase.
Apart from writing the above mentioned things, it was also used to write prototype ideas to optimize the implementation (most of which (like 80-90%) didn't work out well, and I had to just revert or remove them entirely, this can be seen if you go through the commits). The CAS insert fast path and a direct port of leaf coalescing was something that the model (Opus 4.5) was pushing aggressively, even though it was fundamentally unsound for masstree and leads to EXTREMELY subtle data races and synchronization issues and transient stress test failures.