When optimizing open-addressed hash maps, separate 7-bit H2 control bytes from keys and stop probes only on EMPTY.
TL;DR30-second simplified summary▾
Sustains 87.5% load factor and 2x faster lookups at 50M ops/sec.
Decoupling Control Metadata from Key-Value Slots.
Why this matters: Keep 16 slot fingerprints inside a single 16-byte SIMD register.
By packing 16 control bytes contiguously, one _mm_loadu_si128 + _mm_cmpeq_epi8 checks an entire group without touching the key array.
| Choice | What it buys | What it costs |
|---|---|---|
| Scalar Linear Probing | Simple loop | 1 slot/cycle + branch stalls |
| 16-Way SIMD Control Group | 2x faster lookups; 87.5% load | 1 byte control metadata per slot |
def swisstable_lookup(groups: list, key_hash: int, key, eq_fn):
h1, h2 = key_hash >> 7, key_hash & 0x7F
g_idx = h1 % len(groups)
for step in range(len(groups)):
ctrl, keys, vals = groups[(g_idx + step) % len(groups)]
for i in range(16):
if ctrl[i] == h2 and eq_fn(keys[i], key):
return vals[i]
if 0x80 in ctrl:
return None
SWAR 8-Way Parallel Probing in Java.
Why this matters: Apply the exact same control-byte parallelism in pure Java without JNI.
Using 64-bit long SWAR bit-tricks tests 8 control bytes in parallel in pure Java.
- Problem
- Eliminate L1 cache misses and branch mispredictions at 50M hash-map lookups/sec.
- Naive Solution
- Use a chained hash map or scalar linear probing.
- Why It Fails
- Chained nodes scatter across heap memory; scalar probing evaluates unpredictable key-equality branches on every slot.
- Alternatives
- Increase bucket array 4x (wastes RAM), Robin Hood hashing, or SwissTable 16-byte SIMD control bytes.
- Engineering Decision
- Pack 7-bit H2 tags into 16-byte control groups and probe 16 slots in parallel with SIMD/SWAR.
- Trade-Offs
- Uses 1 control byte per slot, but doubles lookup throughput and raises safe load factor to 87.5%.
Why does a SwissTable lookup stop probing immediately when a 16-slot group contains at least one EMPTY (0x80) control byte?
public static long findMatchingSlots(long ctrlWord8, int h2) {
long pattern = (h2 & 0x7FL) * 0x0101010101010101L;
long x = ctrlWord8 ^ pattern;
return (x - 0x0101010101010101L) & ~x & 0x8080808080808080L;
}
Practice: Make the trade-off yourself.
Why must a lookup NOT stop probing when it sees a DELETED (0xFE) control byte?