When replacing B+ Trees in DRAM engines, use ART with big-endian key encoding so point lookups and range scans avoid O(log N) cache misses.
TL;DR30-second simplified summary▾
In-memory engines like DuckDB use ART because Node4 (48B) fits in a single 64B cache line and Node16 uses SIMD to compare 16 keys in 1 cycle.
Adaptive Node Sizing (Node4 / Node16 / Node48 / Node256).
Match physical node capacity to actual child fanout so sparse upper branches fit inside 1-3 CPU cache lines instead of 32 cache lines.
Path Compression & Lazy Expansion.
Collapse single-child internal chains into an 8-byte optimistic prefix header and defer node allocation for unique leaf suffixes until a second key diverges.
class ARTNode:
def __init__(self, cap=4, prefix=b''):
self.cap, self.prefix = cap, prefix
self.keys, self.children = [], []
self.index_map = None
def find_child(self, b: int):
if self.cap <= 16:
for i, k in enumerate(self.keys):
if k == b: return self.children[i]
return None
if self.cap == 48:
idx = self.index_map[b]
return None if idx == 255 else self.children[idx]
return self.index_map[b]
Binary-Comparable Keys & Optimistic Prefix Checks.
Transform signed integers (flip MSB) and floats into big-endian byte order so lexicographical radix order matches numerical order for range scans. [INSERT YOUR BENCHMARK / PROD NOTE] for workload-specific p99 range-scan throughput.
Binary-Comparable Keys & Adaptive Node Layout
- Negatives sort after positives (MSB=1)
- ~163.8 GB RAM / 32 cache lines per node
- Lexicographical order matches numeric order
- ~5.5-11.5 GB RAM / 1-3 cache lines per node
- Problem
- How to support both fast O(k) point lookups and ordered range scans on tens of millions of in-memory keys without wasting RAM or suffering O(log N) DRAM cache misses.
- Naive Solution
- Use a fixed 256-way radix trie for O(k) speed, or a balanced Red-Black / B+ Tree for compact memory.
- Why It Fails
- A fixed 256-way trie wastes >98% of RAM (~163.8 GB per 100M sparse keys) on empty pointers, while a Red-Black Tree incurs ~27 pointer-chasing DRAM hops at 100M keys.
- Alternatives
- Compare ART (Node4/16/48/256 + Path Compression) against hash tables (no range scans), in-memory B+ Trees (O(log N) comparisons), and disk-backed B+ Trees/LSM Trees.
- Engineering Decision
- Use an Adaptive Radix Tree with big-endian binary-comparable key encoding (flipping the MSB on signed integers) for in-memory OLTP/OLAP indexes, and use B+ Trees or LSM Trees for disk-backed SSD engines.
- Trade-Offs
- Cuts memory by 10x-30x over naive tries and keeps hot branches in 1-3 CPU cache lines, at the cost of managing four node layouts, path compression, and explicit key-order encoding.
A Node16 in your ART index receives a 17th distinct child byte. What is the exact memory-efficient transition?
| Choice | What it buys | What it costs |
|---|---|---|
| Adaptive Radix Tree (ART) | O(k) lookup complexity independent of dataset size N Compact 1-3 cache lines per node with ~5.5-11.5 GB RAM per 100M sparse keys Supports ordered range scans via binary-comparable key encoding | Higher implementation complexity across four adaptive node layouts and path compression Requires big-endian MSB-flip encoding for signed integers and floats |
| Red-Black Tree | Compact ~4.8 GB RAM per 100M keys with uniform node size Native key comparison without byte-order transformation | O(log N) pointer chasing (~27 DRAM hops at 100M keys) 1 cache line per node hop causes frequent DRAM stalls |
| Naive 256-Way Trie | O(k) lookup complexity with direct 256-slot byte indexing | ~163.8 GB RAM per 100M sparse keys (>98% wasted pointers) 32 cache lines per node (2,048 bytes) destroys CPU cache locality |
Practice: Make the trade-off yourself.
Why does ART's Node48 use a 256-byte index array alongside a 48-pointer array instead of a 48-element sorted key array?