In-Memory Tree Usage
TheLeanIMT provides a high-performance in-memory n-ary Merkle tree with no persistence overhead. This is the fastest mode and is ideal when you have a reliable bootstrapping mechanism or don’t need crash recovery.
Basic Setup
To use in-memory mode, disable default features in yourCargo.toml:
no_std library with zero dependencies for just the core algorithm.
Creating a Tree
Create a tree by specifying:- Hasher: The hash function to use (e.g.,
Blake3Hasher) - N: Branching factor (2, 4, 8, or 16)
- MAX_DEPTH: Maximum tree depth
The branching factor N directly affects tree depth. With N=4, you reduce depth compared to binary trees while maintaining the same leaf capacity. For example:
- N=2, MAX_DEPTH=16 → ~65K leaves
- N=4, MAX_DEPTH=16 → ~4.3B leaves
- N=8, MAX_DEPTH=10 → ~1B leaves
Inserting Leaves
Working with Snapshots
Snapshots are cheap, immutable views of the tree state. They enable lock-free concurrent reads while insertions continue.Generating Proofs
Proofs are generated from snapshots, not the live tree:Performance Characteristics
Insertion Throughput
The single-threaded variant achieves the best performance for low insertion rates:- Single insert: ~500ns per leaf (2M ops/sec)
- Batch insert: Up to 190M leaves/sec with
parallelfeature - Memory overhead: 4 KB per chunk (128 hashes × 32 bytes)
Memory Usage
With structural sharing viaArc:
- Each snapshot shares most data with the tree
- Only modified chunks are copied (copy-on-write)
- Chunk size is 128 hashes = 4 KB per chunk
Complete Example
Here’s a complete working example:Optional Features
Add these features toCargo.toml for additional functionality:
concurrent
Switches to
&self methods with internal RwLock via parking_lot. Enables safe concurrent access from multiple threads.parallel
Enables rayon-parallelized
insert_many for large batches. Automatically kicks in above ROTORTREE_PARALLEL_THRESHOLD (default: 1024 parents).wincode
Adds
wincode serde derives to proof types for efficient serialization using Solana’s wincode format.serde
Adds standard serde support to proof types for compatibility with the broader Rust ecosystem.
Common Pitfalls
Next Steps
Persistent Storage
Learn how to enable WAL-based persistence with crash recovery
Proof Generation
Generate and verify inclusion and consistency proofs
Performance Tuning
Optimize for your workload with compile-time and runtime configuration
Feature Flags
Explore all available feature flags and their tradeoffs
