Skip to main content
← PROBLEMS / CODING-ALGORITHM
An end-to-end domain learning journey

Breaking Radix Trie Memory
Explosion & Cache-Line Thrashing.

Indexing 100M sparse keys in a fixed 256-way radix trie consumes ~163.8 GB of mostly null pointers and thrashes CPU L1/L2 caches across 32 cache lines per node.
Start the journey3 stages· Last modified Nov 27, 2026
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

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.

Before you begin · Building blocksAlready know these? Jump to Stage 01 ↓
64-byte CPU cache line mechanics and pointer-chasing latency
Core prerequisite: 64-byte CPU cache line mechanics and pointer-chasing latency.
Trie branching factor vs. space utilization trade-offs
Core prerequisite: Trie branching factor vs. space utilization trade-offs.
Full walkthrough · 3 stages
01
STAGE 01L4 · FUNDAMENTALS

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.

Memory & Cache Footprint

Fixed 256-Way Node vs. Cache-Aligned Node4

Before · Naive 256-Way Fixed Node
  • 2,048B per internal node
  • 252 null pointers (>98% wasted)
  • 32 x 64B cache lines evicted
After · Adaptive Node4 Layout
  • 48B per sparse node
  • 4 compact keys + 4 child pointers
  • 1 x 64B cache line loaded
02
STAGE 02L4 · FUNDAMENTALS

Implementing Cache-Aligned Node Promotion.

Start every branch as a 48B Node4 and promote through Node16 (SIMD) and Node48 (indirection) only as fanout grows.

Adaptive Node Layout

4-Tier ART Node Promotion Lifecycle

Key prefix diverges5th child inserted17th child insertedLeaf (Lazy)Unique suffix stored inlNode4 (<=4)48B; fits in 1 cache linNode16 (<=16)144B; 3 cache lines + SINode48 (<=48)640B; 256B index map

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
03
STAGE 03L5 · APPLIED DESIGN

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.

Delete Hygiene & Reclamation

Fragmented Slabs vs. Demoted ART + Path Compression

Before · Without Node Demotion
  • ~80% peak RAM retained after 90% delete
  • Sparse Node48/Node256 slabs stay pinned
  • High L2/L3 cache-line waste
After · Demotion + Path Compression
  • ~5.5 - 11.5 GB for 100M keys
  • 1 - 3 cache lines per hop
  • Single-child chains collapsed
Engineering Decision Framework
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.
Decision Exercise · Make the Call

After bulk-deleting 90% of keys, your ART index still holds 80% of its peak RAM. What invariant is missing?

ChoiceWhat it buysWhat it costs
Naive 256-Way TrieO(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 Node16Requires node promotion/demotion and path-compression maintenance on insert and delete
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 2
Score: 0/2

How many 64-byte CPU cache lines does a single naive 256-way radix node (2,048 bytes) span?

Recommended Learning Flow