When an in-memory ordered index stalls on B+Tree comparison misses, use ART with SIMD Node16 scans and OLC version validation.
TL;DR30-second simplified summary▾
ART adapts fanout across Node4, SIMD Node16, indexed Node48, and direct Node256 while collapsing single-child chains. OLC lets readers traverse without dirtying cache lines.
P · Problem & Why Classic Tries and B+Trees Stall.
If you index 100 million 64-bit integers in a standard 256-way radix trie (8 levels, 1 byte per level), every sparse inner node allocates 256 * 8-byte pointers = 2,048 bytes just to store 2 or 3 children.
That wastes >90% of DRAM on null pointers. On the other hand, a B+Tree has depth log_B(N) (~4–5 levels for 100M keys), but at every level it binary-searches 64–256 keys, causing unpredictable branch mispredictions and repeated full-key comparisons (especially costly for variable-length strings).
256-Way Trie Bloat vs. Adaptive Radix Tree (ART)
- 2 KB per node even for 2 children
- 95%+ null pointers in DRAM
- Deep chains on shared prefixes
- Node4/16/48/256 shrink with fanout
- Path compression collapses chains
- 16-way SIMD compare in 1 cycle
R · Representation: Node4, Node16, Node48 & Node256.
Instead of one giant node struct, ART uses four node sizes and promotes/demotes a node as children are inserted or deleted.
Node4 (<=4 children) fits in a single 64-byte cache line using sorted key bytes. Node16 (5–16 children) stores up to 16 key bytes and uses _mm_cmpeq_epi8 + _mm_movemask_epi8 to test all 16 bytes in parallel. Node48 (17–48 children) uses a 256-byte lookup array mapping byte -> slot index [0..47], avoiding 256 * 8B = 2 KB pointers. Node256 (49–256 children) uses a direct children[byte] array for O(1) indexing.
Node4 -> Node16 (SIMD) -> Node48 (Byte Index) -> Node256 (Direct)
I · Invariant: Lazy Expansion & Path Compression.
Suppose two keys are /api/v1/users/1001 and /api/v1/users/1002. A naive trie creates 14 single-child inner nodes just for /api/v1/users/100.
ART eliminates those nodes with two rules: (1) Lazy Expansion—never create inner nodes for a single leaf; store the full suffix in the leaf until a second key shares a prefix. (2) Path Compression—store the common prefix bytes directly in the inner node header so lookup skips all shared bytes in one memcmp.
Path-Compressed Traversal & Leaf Verification
M · Mechanics at Scale: Optimistic Lock Coupling (OLC).
Even a shared reader-writer lock (pthread_rwlock) writes a reader count to memory on every read, causing CPU cache-line bouncing across cores.
With Optimistic Lock Coupling (OLC), each ART node carries an atomic 64-bit version_lock_obsolete word: bit 0 is the write lock, bit 1 marks an obsolete/replaced node, and the upper bits are a monotonic version counter. Readers never write to memory—they load v = node.read_lock_or_restart(), read child pointers, and call node.read_unlock_or_restart(v). If v changed or bit 0/1 was set, the reader simply restarts from the root.
Optimistic Read Validation vs. Concurrent Writer Bump
- Problem
- How to scale an ordered in-memory index across 64+ CPU cores with minimal read contention.
- Naive Solution
- Use a global RWLock or per-node RWLock on every traversal.
- Why It Fails
- Every reader mutates the lock's atomic counter, invalidating the cache line across all other cores.
- Alternatives
- Per-node RWLocks vs. Lock-Free Bw-Tree vs. ART + Optimistic Lock Coupling (OLC).
- Engineering Decision
- Use ART with OLC: version-validated lock-free reads + fine-grained per-node write locks + Epoch-Based Reclamation.
- Trade-Offs
- Readers never dirty cache lines, at the cost of occasional read restarts under heavy write contention on the same node.
E · Edge Cases, Assumptions & When NOT to Use ART.
ART compares raw unsigned bytes from left to right. If you insert signed integers (-5 vs +5) or variable-length strings where one key is a prefix of another ('app' and 'apple'), naive byte ordering breaks!
Fix both deterministically: (1) Flip the sign bit on signed integers and store in big-endian byte order so unsigned byte order matches numeric order; (2) Append a 0x00 sentinel byte to variable-length strings (or store leaf pointers with key-length metadata) so no key is a prefix of another.
Big-Endian Sign-Bit Flip & Null-Terminated Bytes -> ART
You are indexing 50M variable-length UTF-8 strings ('user', 'user_123', 'user_123_pro') in an in-memory ART. Lookups for 'user' fail or collide with 'user_123'. What invariant was violated?
Practice: Make the trade-off yourself.
Why does ART's Node48 use a 256-byte index array plus 48 child pointers instead of 256 direct pointers?