Sub topic 15
Wide-node trees: why B-trees beat binary trees in memory
This chapter assumes you know binary search trees, Big-O notation, and the cache-line idea from earlier chapters: memory moves in blocks of about 64 bytes, and pointer chasing forces a separate load for each node.
The problem with binary nodes
A balanced binary search tree with a million keys is about 20 levels deep. Each node usually holds one key and two pointers, and the allocator may place nodes anywhere in memory. Each step down the tree is therefore a dependent load. The CPU must finish reading one node before it knows the address of the next. Near the root, the nodes stay in cache. Deeper down, almost every level costs a cache miss.
Big-O says the lookup is O(log₂ n). It does not say that each of those steps can cost a trip to main memory.
Wide nodes
A B-tree node holds many keys in a sorted array, plus child pointers. A lookup does a short search inside the node, then follows one pointer. If the node is sized to cover a few cache lines, one memory fetch brings in many keys that are all useful.
The depth shrinks because the branching factor grows. With 16 keys per node, a million keys need about 5 levels, since log₁₆(10⁶) is roughly 5. That is 5 dependent loads instead of 20.
| Property | Binary tree | B-tree (16 keys per node) |
|---|---|---|
| Levels for 1,000,000 keys | about 20 | about 5 |
| Keys examined per cache-line fetch | about 1 | several |
| Search inside a node | none | sequential scan of an array |
| Pointer overhead per key | high | low |
Why scanning many keys is cheap
Comparing 16 keys inside one node looks like more work than comparing one. In practice it is faster. The keys sit next to each other, so after the first miss the rest are cache hits, and the hardware prefetcher handles the sequential reads well. The extra comparisons cost a few cycles each. A cache miss costs hundreds.
Here is a node sketch in C. It uses 15 keys of 4 bytes and a count, so the keys and count fill exactly one 64-byte line.
#include <stdint.h>
#include <stdio.h>
#define MAX_KEYS 15
struct LeafNode {
uint32_t count;
uint32_t keys[MAX_KEYS]; /* 4 + 60 = 64 bytes */
};
/* Returns index of key in the node, or -1 if absent. */
int leaf_find(const struct LeafNode *n, uint32_t key) {
for (uint32_t i = 0; i < n->count; i++)
if (n->keys[i] == key)
return (int)i;
return -1;
}
int main(void) {
struct LeafNode n = {3, {10, 20, 30}};
printf("%zu bytes, find 20 -> %d\n", sizeof n, leaf_find(&n, 20));
return 0;
}Trade-offs and common errors
- Choosing node size. Match it to a cache line or a small multiple of one. Nodes that are too large make the in-node scan costly. Nodes that are too small bring back the deep tree.
- Insertion cost. Inserting into a sorted array shifts keys within the node, and a full node must split. Both are cheap on small arrays, but they are more work than a binary tree's pointer update.
- Alignment. A 64-byte node that starts mid-line spans two lines and loses the benefit. Allocate nodes on 64-byte boundaries.
- Ordered maps in standard libraries. Many standard ordered maps use binary red-black trees. For read-heavy workloads, a B-tree map often wins by a large factor. Measure with your own data.
Exercise
A balanced binary tree and a B-tree with 32 keys per node each hold about one billion keys. Roughly how many levels does each have, and why does the B-tree usually do fewer cache misses per lookup?
Show answer
The binary tree has about 30 levels (log₂ of 10⁹). The B-tree has about 6 levels (log₃₂ of 10⁹ is roughly 6). Each level costs about one dependent memory fetch, and the keys inside a wide node are contiguous, so the extra comparisons hit cache. The B-tree therefore does far fewer misses.