Sub topic 14
Cache-friendly hash tables: open addressing versus chaining
This chapter assumes you know how cache lines work, why sequential access is fast, and how hash tables map keys to buckets. It builds on the earlier chapter on arrays versus linked structures.
Why chaining costs cache misses
A chained table stores each bucket as a linked list. A lookup reads the bucket pointer, then loads a node, then follows next to the following node. Each node is a separate heap allocation, so each hop is likely a cache miss. Each hop also depends on the previous load, so the CPU cannot overlap the misses.
Open addressing stores entries directly in one array. A colliding key goes into another slot of the same array, found by a probe sequence. With linear probing, the next slot is simply the adjacent one.
A linear-probing table
This table uses 16-byte slots, so four slots fit in a 64-byte line. The key 0 marks an empty slot, so it cannot be stored.
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>
#define CAP 1024 /* power of two */
#define EMPTY 0
struct Slot { uint64_t key; uint64_t value; };
struct Table { struct Slot slots[CAP]; };
static size_t hash(uint64_t k) {
return (size_t)((k * 0x9E3779B97F4A7C15ull) >> 54); /* 64 - log2(CAP) */
}
bool table_put(struct Table *t, uint64_t key, uint64_t value) {
size_t i = hash(key);
for (size_t n = 0; n < CAP; n++) {
if (t->slots[i].key == EMPTY || t->slots[i].key == key) {
t->slots[i].key = key;
t->slots[i].value = value;
return true;
}
i = (i + 1) & (CAP - 1);
}
return false; /* full */
}
bool table_get(const struct Table *t, uint64_t key, uint64_t *out) {
size_t i = hash(key);
while (t->slots[i].key != EMPTY) {
if (t->slots[i].key == key) { *out = t->slots[i].value; return true; }
i = (i + 1) & (CAP - 1);
}
return false;
}
int main(void) {
static struct Table t; /* zeroed, so every slot is EMPTY */
table_put(&t, 42, 7);
uint64_t v;
if (table_get(&t, 42, &v)) printf("%llu\n", (unsigned long long)v);
return 0;
}Trade-offs
| Aspect | Chaining | Open addressing |
|---|---|---|
| Memory per entry | Entry plus a pointer and allocator overhead | Entry only |
| Locality on collision | Poor, one miss per node | Good, neighbors share a line |
| High load factor | Degrades gradually | Degrades sharply |
| Deletion | Unlink the node | Needs tombstones or backward shifting |
Common errors
- Letting the load factor grow. Linear probing forms clusters, and probe lengths climb quickly above roughly 70 to 80 percent. Resize before then.
- Storing large values inline. Wide slots reduce how many fit per line. Keep keys and small values in the table, and put large payloads elsewhere.
- Clearing a slot on delete. This breaks probe chains for keys stored after it. Use a tombstone marker.
- Using a weak hash. Poor mixing creates clusters that cancel the locality benefit.
Exercise
A chained table and a linear-probing table both hold 1,000,000 small entries at similar load. Why does the probing table usually do fewer cache misses per lookup, and what is the cost of pushing its load factor to 95 percent?
Show answer
Probing keeps entries in one contiguous array, so a collision usually lands in the same or the next cache line, while chaining needs a dependent pointer load per node. At 95 percent load, clusters make probe sequences long, so lookups scan many slots and misses become frequent, and unsuccessful lookups suffer most.