When implementing SwissTable, reset deleted slots to EMPTY if the group already has an EMPTY slot to avoid tombstones.
TL;DR30-second simplified summary▾
Sustains 87.5% load factor and 2x faster lookups than chained maps.
H1/H2 Splitting & 16-Byte Control Groups.
Why this matters: Filter 16 slots in 1 CPU cycle before touching key memory.
H1 picks the 16-slot group; H2 (7 bits, 0x00..0x7F) is compared in parallel against 16 control bytes. Negative control bytes encode EMPTY (0x80) and DELETED (0xFE).
| Choice | What it buys | What it costs |
|---|---|---|
| Chained HashMap | Simple tombstones | Heap pointer chasing; L1 misses |
| SwissTable (16-Byte SIMD) | 16-way parallel probe; 87.5% load | Requires SIMD intrinsics / 16B alignment |
CTRL_EMPTY, CTRL_DELETED = 0x80, 0xFE
def probe_group(ctrl_bytes: list, h2: int):
# Simulates _mm_cmpeq_epi8 + _mm_movemask_epi8 across 16 control bytes
matches = [i for i, c in enumerate(ctrl_bytes) if c == h2]
has_empty = any(c == CTRL_EMPTY for c in ctrl_bytes)
return matches, has_empty
Tombstone-Free Deletes & Java Bitwise Emulation.
Why this matters: Prevent probe-chain degradation under heavy delete churn.
If the 16-slot group already contains an EMPTY byte, deletion resets the slot directly to EMPTY instead of DELETED, avoiding tombstone accumulation.
- Problem
- How to sustain <10 ns lookups at 87.5% load factor without linked-list cache misses or scalar probe loops.
- Naive Solution
- Use separate chaining or scalar linear probing comparing full keys one slot at a time.
- Why It Fails
- Chaining chases heap pointers across cache lines; scalar probing triggers unpredictable branch mispredictions on every collision.
- Alternatives
- Separate chaining, Robin Hood scalar probing, or SwissTable 16-byte SIMD control arrays.
- Engineering Decision
- Store 7-bit H2 fingerprints in a 16-byte control array and match all 16 slots in parallel via SIMD before accessing the key array.
- Trade-Offs
- Adds 1 byte of control metadata per slot, but doubles lookup speed and eliminates 99.2% of unnecessary key comparisons.
Why does SwissTable delete directly to EMPTY (instead of DELETED) whenever the 16-slot group already contains an EMPTY slot?
public static int matchByteMask(long word8, int h2) {
// SWAR (SIMD Within A Register) 8-byte parallel control-byte match
long broadcast = (h2 & 0xFFL) * 0x0101010101010101L;
long x = word8 ^ broadcast;
long hasZero = (x - 0x0101010101010101L) & ~x & 0x8080808080808080L;
return Long.numberOfTrailingZeros(hasZero) >>> 3;
}
Practice: Make the trade-off yourself.
What does the 7-bit H2 fingerprint in SwissTable's control byte achieve?