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

Eliminating L1 Cache Misses & Branch Stalls
in Hash Maps via 16-Way SIMD Probing.

A hot-path lookup table suffers L1 cache misses and branch mispredictions under 50M lookups/sec; redesign it with 16-byte control arrays and SIMD/SWAR parallel probing.
Start the journey2 stages· Last modified Dec 3, 2026
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

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.

Before you begin · Building blocksAlready know these? Jump to Stage 01 ↓
CPU Cache Lines (64B)
Contiguous metadata maximizes spatial locality.
SIMD / SWAR Bitwise Masking
Parallelizes byte comparisons across 8 or 16 slots.
Full walkthrough · 2 stages
01
STAGE 01intermediate

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.

Before · Chained / Scalar Probing
After · 16-Way SIMD Control Array
ChoiceWhat it buysWhat it costs
Scalar Linear ProbingSimple loop1 slot/cycle + branch stalls
16-Way SIMD Control Group2x faster lookups; 87.5% load1 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
02
STAGE 02advanced

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.

Lookup CallerSIMD Control RegKey/Val Bucket1. 1 cycle: Compare H2 against 16control bytes2. Load key only for set bits in16-bit mask3. Return value (2x faster, 87.5%load)
Engineering Decision Framework
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%.
Decision Exercise · Make the Call

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 · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

Why must a lookup NOT stop probing when it sees a DELETED (0xFE) control byte?

Recommended Learning Flow