From db228fc5f9277567da717400638f6d6d38e86372 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 15 Nov 2026 23:39:12 +0100 Subject: [PATCH] feat: value hash for transpositions Rigby evaluated every new leaf from scratch, although most leaves are positions it has already searched: with temporary instrumentation, 37-85% of the leaf evaluations in a 5-second search repeated a position evaluated earlier in the same search (85% from the start position, 81% in a quiet middlegame, 37% in Kiwipete). Monty, Vine and Jackal all keep a hash table of positions' Q for this. Rigby now does the same: every node on a playout's path stores its Q (from the view of the side to move there) with its visit count under the position's key; a different position always replaces an entry, the same one only with at least as many visits. A leaf whose position is found takes the stored Q instead of the value network and quiescence search, so it starts from what a deeper search already knew. Draws by repetition or the 50-move rule are not stored (they belong to the path, not the position). The table lives across searches and is cleared on ucinewgame. It takes a 32nd of Hash (12-byte entries; 174k entries at 64 MB), the tree the rest. Checked: bench deterministic; RIGBY_VERIFY_TREE build driven over UCI with pauses (tree reuse up to 83k nodes, collections at Hash 1 MB, Hash changes, wtime/winc, movetime, mate-in-one positions found, go infinite + stop) without an abort. Bench: 615835 Co-Authored-By: Claude Opus 5.5 --- src/mcts.cpp | 12 +++++++++--- src/mcts.h | 45 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/mcts.cpp b/src/mcts.cpp index fd51f11..f56a414 100644 --- a/src/mcts.cpp +++ b/src/mcts.cpp @@ -16,8 +16,8 @@ namespace rigby { // Tree // --------------------------------------------------------------------------- -void Tree::resize_mb(size_t mb) { - const size_t count = std::max(mb * 1024 * 1024 / sizeof(Node), 1024); +void Tree::resize_bytes(size_t bytes) { + const size_t count = std::max(bytes / sizeof(Node), 1024); nodes_.assign(count, Node{}); nodes_.shrink_to_fit(); clear(); @@ -129,7 +129,7 @@ size_t Tree::compact(u32 new_root, u32 min_visits) { // Searcher // --------------------------------------------------------------------------- -Searcher::Searcher() { tree_.resize_mb(64); } +Searcher::Searcher() { set_hash(64); } i64 Searcher::elapsed_ms() const { return std::chrono::duration_cast(Clock::now() - start_time_).count(); @@ -315,6 +315,9 @@ float Searcher::evaluate_leaf(u32 idx, const Position& pos, int depth) { return 0.0f; } + // A position searched before (a transposition, or from an earlier search) + // brings its Q along. + if (float q; value_hash_.probe(pos.key, q)) return q; return eval::value(pos); } @@ -443,6 +446,9 @@ void Searcher::playout() { Node& node = tree_[path_[i]]; node.visits++; node.q += (value - node.q) / float(node.visits); + // The Q from the view of the side to move at the node. Draws by + // repetition or the 50-move rule belong to the path, not the position. + if (node.state != TERMINAL_DRAW) value_hash_.store(history_[root_history_size_ - 1 + i], -node.q, node.visits); if (i) butterfly_.update(root_pos_.stm ^ int((i - 1) & 1), Move::from_raw(node.move), bonus); } diff --git a/src/mcts.h b/src/mcts.h index 229b026..029b9bd 100644 --- a/src/mcts.h +++ b/src/mcts.h @@ -35,7 +35,7 @@ struct Node { class Tree { public: - void resize_mb(size_t mb); + void resize_bytes(size_t bytes); void clear(); // Returns the index of `count` fresh nodes, or 0 when the arena is full. u32 alloc(u32 count); @@ -85,6 +85,42 @@ private: std::array, 64>, 2> table_{}; }; +// Q of positions searched before, from the view of the side to move there +// (as in Monty and Vine). Every node of a playout's path stores its Q here; +// a leaf whose position is found takes that value instead of a fresh +// evaluation. Transpositions are common: 37-85% of leaf evaluations in a +// 5-second search repeat a position already evaluated in the same search. +class ValueHash { +public: + void resize(size_t entries) { + table_.assign(std::max(entries, 1), Entry{}); + table_.shrink_to_fit(); + } + void clear() { std::fill(table_.begin(), table_.end(), Entry{}); } + bool probe(u64 key, float& q) const { + const Entry& e = table_[key % table_.size()]; + if (!e.visits || e.check != u32(key >> 32)) return false; + q = e.q; + return true; + } + // A different position always replaces the entry; the same one only with + // at least as many visits. + void store(u64 key, float q, u32 visits) { + Entry& e = table_[key % table_.size()]; + if (e.check == u32(key >> 32) && visits < e.visits) return; + e = Entry{u32(key >> 32), visits, q}; + } + static constexpr size_t ENTRY_BYTES = 12; + +private: + struct Entry { + u32 check = 0; + u32 visits = 0; + float q = 0.0f; + }; + std::vector table_; +}; + struct SearchLimits { u64 nodes = 0; i64 movetime = 0; @@ -124,7 +160,10 @@ public: Searcher(); void set_hash(size_t mb) { - tree_.resize_mb(mb); + // A 32nd of the memory goes to the value hash, the rest to the tree. + const size_t bytes = mb * 1024 * 1024; + value_hash_.resize(bytes / 32 / ValueHash::ENTRY_BYTES); + tree_.resize_bytes(bytes - bytes / 32); tree_valid_ = false; } @@ -132,6 +171,7 @@ public: void new_game() { tree_valid_ = false; butterfly_.clear(); + value_hash_.clear(); } // Synchronous search. `history` holds the keys of every position that led to @@ -146,6 +186,7 @@ private: // Kept between moves of a game, like the tree. ButterflyHistory butterfly_; Position root_pos_; + ValueHash value_hash_; std::vector history_; size_t root_history_size_ = 0; std::vector path_;