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 <noreply@anthropic.com>
This commit is contained in:
rektdieandclaude committed 2026-11-15 23:39:12 +01:00
1 parent 74fb5eb2fb
commit db228fc5f9
2 files changed
+52 -5

No files matched your search

+9 -3
View File
@@ -16,8 +16,8 @@ namespace rigby {
// Tree
// ---------------------------------------------------------------------------
void Tree::resize_mb(size_t mb) {
const size_t count = std::max<size_t>(mb * 1024 * 1024 / sizeof(Node), 1024);
void Tree::resize_bytes(size_t bytes) {
const size_t count = std::max<size_t>(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<std::chrono::milliseconds>(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);
}