[CHAPTER 03 // crates/tables · engine, SPEC 004] Implemented in engine code

Transposition Table Layout B (64-Byte Cache Line)

Cache-aligned 64-byte buckets with two 24-byte atomic slots, 8-bit key prefilter tag, and tabulation torn-read rejection.

Why does BalouChess do this?

Different move orders constantly reach the exact same chess position. The transposition table remembers previously searched positions — their best move, search bound, depth, and raw static evaluation — so BalouChess can cut off redundant subtrees immediately.

Today a single search thread uses the table. Its format is already safe for several threads without locks, as the planned multi-core search will need: two threads writing to the same slot at once could otherwise mix half of one entry with half of another. BalouChess aligns every bucket to a single 64-byte CPU cache line (Layout B) and verifies an atomic 64-bit check word across the words so torn reads are detected and discarded.

[X-RAY CARD 02 // 64-BYTE LAYOUT-B CACHE LINE INSPECTOR]

Lock-Free 64-Byte Bucket & Torn-Write Detector

64-Byte CPU Cache Line (2 × 24 B Slots + 16 B Alignment Reserve) (64 B)

Slot 0 [Bytes 0..23] · Active Probe Target
Word 0 · data Thread A
0x031c80440000002a data (64-bit): score[0..32] · staticEval+16383[33..47] · move[48..63]
Word 1 · meta Thread A
0x010000000010002d meta (64-bit): bound[0..1] · depth[2..17] · gen[18..33] · rsv[34..55] · tag8=key[0..7][56..63]
Word 2 · check Thread A
0x2ba38db90791cb45 check (64-bit): PositionKey XOR Tabulation16(data, meta)
Slot 1 [Bytes 24..47] · Sibling Entry (24 B) 3 × AtomicU64 = 24 B
Padding [Bytes 48..63] · Cache-Line Reserve (16 B) align(64) = 16 B

VALID HIT — 8-bit low-key tag and 64-bit tabulation check match intact entry.

Expected check: 0x2ba38db90791cb45 Stored check: 0x2ba38db90791cb45

Data Layout, Invariants & Common Pitfalls

Memory & State Layout

Each 64-byte cache-line-aligned bucket holds 2 × 24-byte slots plus 16 padding bytes (engine, SPEC 004 §8). Each 24-byte slot consists of three 64-bit atomic words: data (16-bit move, lossless 15-bit signed static eval up to ±16,383 cp, 33-bit SearchValue including mate/TB ply distance), meta (16-bit depth, 16-bit age, 2-bit bound/empty tag, reserved feature bits, and an 8-bit key prefilter tag in bits 56..63), and a 64-bit verification word protecting the full key across both payload words.

Formal Invariants

  • Memory-safe lock-free access (engine, SPEC 004/R2, R3): crates/tables/src/transposition enforces #![forbid(unsafe_code)] using 64-bit atomics; any torn mixture of two concurrent writes passes verification with probability no higher than a random 64-bit key collision, and any surviving move is re-checked via Position::is_legal (engine, SPEC 004/R4).
  • Strict type-level neural quarantine (engine, SPEC 004/R9): only crates/search can write to the transposition table, accepting SearchValue and Bound exclusively — NetWdl values from neural hints cannot compile into a table store.

Typical Pitfalls & Edge Cases

  • Mate score ply-relative corruption (engine, SPEC 003/R3): a mate in N plies from the root is a mate in (N - ply) plies from the current node; forgetting to normalize mate distances on store and un-normalize on probe produces phantom mates when reached at a different ply.
  • Graph History Interaction (engine, SPEC 003/R4, engine, SPEC 004/R12): PositionKey encodes the board, not the repetition path or 50-move counter; BalouChess always checks draw-by-repetition and 50-move draw in the node before trusting any transposition table cutoff.