When optimizing in-memory tries, align Node4 to <=64B and demote nodes on delete to reclaim fragmented memory.
TL;DR30-second simplified summary▾
Combining Node4/16/48/256 with Path Compression eliminates both pointer bloat and deep single-child chains.
Diagnosing the 2,048-Byte Sparse Node Trap.
With 80M internal nodes for 100M keys, a 256-way trie requires 163.8 GB just for null pointers and forces 32 cache-line reads per node.
Fixed 256-Way Node vs. Cache-Aligned Node4
- 2,048B per internal node
- 252 null pointers (>98% wasted)
- 32 x 64B cache lines evicted
- 48B per sparse node
- 4 compact keys + 4 child pointers
- 1 x 64B cache line loaded
Implementing Cache-Aligned Node Promotion.
Start every branch as a 48B Node4 and promote through Node16 (SIMD) and Node48 (indirection) only as fanout grows.
4-Tier ART Node Promotion Lifecycle
Nodes allocate only the capacity needed for their current branching factor and promote incrementally.
def promote_node(node):
if node.cap == 4:
node.cap = 16
elif node.cap == 16:
imap = [255] * 256
for idx, k in enumerate(node.keys):
imap[k] = idx
node.index_map = imap
node.cap = 48
Shrinking on Delete & NUMA Cache-Line Hygiene.
Demote nodes (e.g., Node16 -> Node4) when fanout drops below threshold to prevent permanent memory fragmentation after bulk deletes. [INSERT YOUR BENCHMARK / PROD NOTE] for delete-heavy workloads.
Fragmented Slabs vs. Demoted ART + Path Compression
- ~80% peak RAM retained after 90% delete
- Sparse Node48/Node256 slabs stay pinned
- High L2/L3 cache-line waste
- ~5.5 - 11.5 GB for 100M keys
- 1 - 3 cache lines per hop
- Single-child chains collapsed
- Problem
- How to keep a 100M-key sparse radix trie within DRAM and L1/L2 cache-line budgets without losing O(k) prefix-lookup speed.
- Naive Solution
- Allocate a fixed 256-pointer array (2,048 bytes) for every internal node regardless of actual child count.
- Why It Fails
- Real-world sparse keyspaces average 2-4 children per internal node, leaving >98% of pointers null (~163.8 GB for 80M nodes) and evicting 32 cache lines per hop.
- Alternatives
- Compare flat sorted arrays (good for <10K static keys), hash tables (no ordered prefix/range scans), and B-trees (O(log N) key comparisons vs. O(k) radix hops).
- Engineering Decision
- Adopt an Adaptive Radix Tree (Node4/Node16/Node48/Node256) with Path Compression, Lazy Expansion, and node demotion on delete when trie overhead exceeds 2x raw key size or L2 cache misses spike.
- Trade-Offs
- Reduces memory by 14x-30x and shrinks hops to 1-3 cache lines, in exchange for branch promotion/demotion logic and epoch-based reclamation under concurrent deletes.
After bulk-deleting 90% of keys, your ART index still holds 80% of its peak RAM. What invariant is missing?
| Choice | What it buys | What it costs |
|---|---|---|
| Naive 256-Way Trie | O(1) direct byte offset indexing per hop without key scanning | ~163.8 GB RAM for 100M sparse keys 32 cache lines (2,048B) touched per node hop |
| Adaptive Radix Tree (ART) | ~5.5 - 11.5 GB RAM for 100M sparse keys 1 - 3 cache lines per hop with compact Node4 and SIMD Node16 | Requires node promotion/demotion and path-compression maintenance on insert and delete |
Practice: Make the trade-off yourself.
How many 64-byte CPU cache lines does a single naive 256-way radix node (2,048 bytes) span?