Skip to content

Chapter 15: Systems Data Structures

A data structure in a production system is chosen by its worst case under the system’s access pattern, not by its textbook complexity. The KV block pool needs a hash index that never stalls on a rehash and an LRU that evicts in O(1)O(1); the tokenizer needs a hash map tuned for small integer keys and a priority queue it can delete from; the router needs a hash that moves few keys when a replica joins. This chapter builds each of those structures from its call site, deriving what it must do before showing how it does it.

Concepts from other sections (32)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

Every structure here has a caller in the system the books build. The hash maps index KV blocks and tokenizer merges, the heaps pick top-kk tokens and the next request, the radix tree finds shared prompt prefixes, the Bloom filter screens duplicate paragraphs, and the ring routes requests to replicas. The same ideas appear in three languages: C for the runtime (tl_vec, tl_map, the LRU, tl_topk_f32), Rust for the tokenizer and engine, and Go for the gateway. The interface is the contract; the language is a detail.

Two themes run through the chapter. First, open addressing is a probe-length problem: linear probing, Robin Hood, and Swiss tables differ in how they bound the worst probe, not the average one, and the load factor decides when that bound breaks. Second, probabilistic structures trade certainty for space: a Bloom filter never misses a member and is wrong about non-members at a rate you choose, and a consistent hash ring trades exact balance for minimal movement, then wins balance back with a load cap.

Learning objectives

  • Build open-addressing hash tables (Robin Hood with backward-shift deletion, and a Swiss table with SWAR control-byte matching) and explain how each bounds probe length at load 7/8.
  • Build binary heaps for a stable priority queue with O(1)O(1) lazy deletion and for allocation-free top-kk selection.
  • Build allocation-free and failure-atomic containers in C: a growable array and an intrusive LRU list.
  • Build a radix tree over token ids with an index-linked LRU of unlocked leaves for prefix caching.
  • Size a Bloom filter from nn and pp, and route keys with a consistent hash ring under bounded loads.

You should be comfortable with the language primers in Programming and Algorithms: C, Rust, and Go. Load factors and hashing probability come from Discrete Math 2 in Mathematics. The interview-pattern chapters of this book, Arrays and Hashing, Trees, and Probabilistic Structures, are good warm-ups; bloom.py in the last of these is the worked example behind the Bloom filter section. The practice C drills 01 to 04 build linear probing and an intrusive list as baselines.

Try to implement each structure from its section before reading the reference design: Abseil’s notes for Swiss tables, SGLang’s paper for the radix tree.

The sections are numbered by where they sit in the C runtime, but two of them are foundations for the rest. A Robin Hood hash map is the chapter’s first hash-table section, developing hashing, probing, and the load factor from first principles, and A binary heap with lazy deletion is its first heap section. If these ideas are new to you, read those two first; the Swiss table and C top-kk build on them.

  1. A growable array. The type-erased C array tl_vec, with geometric growth for O(1)O(1) amortized appends and a failure-atomic growth order; it holds each sequence’s block table in the KV pool.
  2. A Swiss table. The C map tl_map from u64 to u64: 7-bit fingerprints in control bytes matched 8 at a time with SWAR, load 7/8, and tombstones only where a probe may have passed. It is the KV pool’s prefix-hash index.
  3. An intrusive list and an LRU. Links embedded in each element and a sentinel head give O(1)O(1) touch and evict with no allocation, for the pool’s evictable cached blocks.
  4. Heap top-k in C. An optional standalone C unit: a size-kk min-heap over logits in the caller’s buffers, with ties to the lower index and parity with the Python sampler through shared fixtures.
  5. A Robin Hood hash map. The Rust map behind the tokenizer’s vocabulary and merge table: Fx hashing from the top bits, Robin Hood insertion, early-exit lookup, and backward-shift deletion.
  6. A binary heap with lazy deletion. A stable Rust heap with O(n)O(n) heapify, plus handles and generation counters for O(1)O(1) removal; the BPE merge queue and the engine’s waiting queue.
  7. A radix tree over token ids. A compressed trie in granules of one KV block, with match_prefix returning the longest cached prefix, locks that pin running requests, and an index-linked LRU of leaves. It is the radix prefix cache.
  8. A Bloom filter. m=−nln⁡p/(ln⁡2)2m = -n \ln p / (\ln 2)^2 bits and k=(m/n)ln⁡2k = (m/n) \ln 2 hashes, double hashing from FNV-1a and SplitMix64, and a portable byte format shared with the corpus pipeline’s exact-dedup screen.
  9. A consistent hash ring with bounded loads. A Go ring with virtual nodes where no replica takes more than ⌈c⋅avg⌉\lceil c \cdot \text{avg} \rceil keys; it drives the gateway’s affinity routing.

The engine flag --prefix-cache=hash|radix gives both the Swiss table (hash index in C) and the radix tree (in Rust) a production call site, and Block manager with prefix cache in AI Engineering benchmarks one against the other.

These structures are used throughout the other books:

  • The KV block pool and the prefix cache in Inference in AI Engineering use the growable array, the Swiss table, the LRU, and the radix tree.
  • The Gateway chapter of AI Engineering uses the bounded-load ring for affinity routing.
  • The Corpus Pipeline chapter of Data Engineering uses the Bloom screen in exact dedup.

The same designs run in production: Google’s Abseil makes Swiss tables the default C++ hash map; SGLang and vLLM keep radix and hash prefix caches over paged KV blocks; and Akamai and Vimeo use consistent hashing, including the bounded-load variant, in production load balancers.