summaryrefslogtreecommitdiff
path: root/src/chess.h
diff options
context:
space:
mode:
authorScott Gasch <[email protected]>2026-10-05 22:22:24 -0700
committerScott Gasch <[email protected]>2026-10-05 22:22:24 -0700
commit447130db3df87b40d080dc26429d45e9213c19f7 (patch)
treea47e3ef8ea671eb557cfe1b45356e8df93a4e4e9 /src/chess.h
parenta8308030473f56f84a9a8c7179fe029524c68b3a (diff)
Lockless main hash table (Hyatt/Crafty scheme)
Replaces the sharded-spinlock protection around every main-hash probe and store with the classic lockless-hashing technique: HASH_ENTRY's existing 8-byte data word (mv/uDepth/bvFlags/iValue, now addressable as u64Data via an anonymous union) and its 8-byte u64Sig are written as two independent plain 64-bit stores -- u64Data first, then u64Sig = realKey ^ u64Data -- instead of under a lock. Every probe reads both words into a local snapshot and verifies (storedSig ^ storedData) == realKey before trusting anything; a concurrent writer racing a reader can only ever produce a self-inconsistent pair that fails this check (an ordinary miss), not corruption, except in the one documented, harmless case where the two racing entries' data words happen to be bit-identical. LockCompareExchange (the old spinlock's cost) was already showing up at 0.51% self-time in a `bench` profile -- real, measured overhead on a path hit roughly once per searched node, now removed. HashLookup's contract changes from "return a pointer into the live table, or NULL" to "return TRUE/FALSE, with *pOut holding a validated local copy on TRUE" -- callers must never read back through a pointer into the live table after this function returns, since there's no longer a lock serializing that against a concurrent writer. The one real caller (search.c) and the DEBUG-only VERIFY_HASH_HIT diagnostic are updated accordingly. testhash.c: AnalyzeFullHashTable's duplicate-entry check used to compare two entries' raw u64Sig directly, which no longer means "same position" now that u64Sig depends on each entry's own data word -- fixed to un-corrupt both sides first. Adds TestLocklessHashAlgebra, a pure algebra test (no concurrency) verifying the XOR round-trip and torn-write self-detection property over 200k random trials, wired into the TEST=1 startup self-test suite. Verified: TestLocklessHashAlgebra passes; single-threaded runs of the locked and lockless binaries produce identical node counts and solve scores across all three curated suites (ecm_ringers/confident_quick/ hard_quick); several minutes of real contention (tiny 4MB hash table, 12 threads at ~1200% CPU on a Ryzen 9 3900X, plus a 4-core run locally) produced zero crashes/assertions/corruption on either machine. Co-Authored-By: Claude Sonnet 5 <[email protected]>
Diffstat (limited to 'src/chess.h')
-rwxr-xr-xsrc/chess.h34
1 files changed, 27 insertions, 7 deletions
diff --git a/src/chess.h b/src/chess.h
index 7125cc7..d637905 100755
--- a/src/chess.h
+++ b/src/chess.h
@@ -3912,11 +3912,27 @@ TestWhoAttacksSquareMagic(void);
typedef struct _HASH_ENTRY
{
- MOVE mv; // 0 1 2 3
- UCHAR uDepth; // 4
- UCHAR bvFlags; // 5 ==> d d d d | thr up low exact
- signed short iValue; // 6 7
- UINT64 u64Sig; // 8 9 A B C D E F == 16 bytes
+ // Lockless-hash data word (Hyatt/Crafty scheme, see hash.c's
+ // module comment): mv/uDepth/bvFlags/iValue pack into one aligned
+ // UINT64 (u64Data) so hash.c can read/write them as a single word
+ // alongside u64Sig, which stores (realKey ^ u64Data) rather than
+ // the plain key. Every existing pHash->mv/uDepth/bvFlags/iValue
+ // access keeps compiling unchanged via this anonymous union/struct
+ // (same idiom MOVE itself already uses above).
+ union
+ {
+ struct
+ {
+ MOVE mv; // 0 1 2 3
+ UCHAR uDepth; // 4
+ UCHAR bvFlags; // 5 ==> d d d d | thr up low exact
+ signed short iValue; // 6 7
+ };
+ UINT64 u64Data; // 0 1 2 3 4 5 6 7, same 8 bytes
+ };
+ UINT64 u64Sig; // 8 9 A B C D E F == 16 bytes total;
+ // holds (realKey ^ u64Data), not the
+ // plain key -- see hash.c.
} HASH_ENTRY;
FLAG
@@ -3952,7 +3968,7 @@ StoreUpperBound(//MOVE mvBestMove,
ULONG uDepth,
FLAG fThreat);
-HASH_ENTRY *
+FLAG
HashLookup(SEARCHER_THREAD_CONTEXT *ctx,
ULONG uDepth,
ULONG uNextDepth,
@@ -3961,7 +3977,8 @@ HashLookup(SEARCHER_THREAD_CONTEXT *ctx,
FLAG *pfThreat,
FLAG *pfAvoidNull,
MOVE *pHashMove,
- SCORE *piScore);
+ SCORE *piScore,
+ HASH_ENTRY *pOut);
COOR
CheckHashForDangerSquare(POSITION *pos);
@@ -3979,6 +3996,9 @@ extern HASH_ENTRY *g_pHashTable;
void
AnalyzeFullHashTable(void);
+FLAG
+TestLocklessHashAlgebra(void);
+
//
// dynamic.c -- enprise/trapped piece hints
//