From 5ca125e336dd4c66abfe59fb570b4b715943a3c8 Mon Sep 17 00:00:00 2001 From: rektdie Date: Thu, 26 Feb 2026 02:21:48 +0100 Subject: [PATCH] tt aging | bench 1743358 --- source/search.cpp | 1 + source/tt.cpp | 31 +++++++++++++++++++++++++++---- source/tt.h | 12 ++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 9736a20..b9d8425 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -896,6 +896,7 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) { template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext* ctx) { searchStopped = false; + ctx->TT->IncreaseAge(); ctx->seldepth = 0; ctx->nodesTable = {}; if constexpr (mode != bench) { diff --git a/source/tt.cpp b/source/tt.cpp index 2eaa1a2..9c06258 100644 --- a/source/tt.cpp +++ b/source/tt.cpp @@ -10,18 +10,41 @@ void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move b current = &bucket->depthPreferred; } else if (bucket->alwaysReplace && bucket->alwaysReplace.hashKey == hashKey) { current = &bucket->alwaysReplace; + } else if (!bucket->alwaysReplace) { + current = &bucket->alwaysReplace; + } else if (!bucket->depthPreferred) { + current = &bucket->depthPreferred; } else if (ttpv) { current = &bucket->depthPreferred; - } else if (!bucket->depthPreferred || bucket->depthPreferred.depth <= depth) { - current = &bucket->depthPreferred; } else { - current = &bucket->alwaysReplace; + const int depthPreferredValue = GetReplacementValue(bucket->depthPreferred); + const int alwaysReplaceValue = GetReplacementValue(bucket->alwaysReplace); + + if (depthPreferredValue <= alwaysReplaceValue) { + current = &bucket->depthPreferred; + } else { + current = &bucket->alwaysReplace; + } + } + + const bool samePosition = current->hashKey == hashKey; + const bool exactBound = nodeType == PV; + const bool entryFromCurrentAge = current->age == age; + const int writeDepth = depth + 4 + ttpv * 2; + + if (samePosition && !exactBound && entryFromCurrentAge && writeDepth <= current->depth) { + return; } current->hashKey = hashKey; current->nodeType = nodeType; current->score = score; current->depth = depth; - current->bestMove = bestMove; + + if (bestMove || !samePosition) { + current->bestMove = bestMove; + } + + current->age = age; current->ttpv = ttpv; } \ No newline at end of file diff --git a/source/tt.h b/source/tt.h index 48861ab..f0c8526 100644 --- a/source/tt.h +++ b/source/tt.h @@ -26,6 +26,7 @@ public: Move bestMove = Move(); uint8_t depth = 0; uint8_t nodeType = 0; + uint8_t age = 0; bool ttpv = false; TTEntry() {} @@ -55,6 +56,13 @@ constexpr int16_t invalidEntry = 11111; class TTable { private: std::vector table; + + uint8_t age = 0; + + int GetReplacementValue(const TTEntry& entry) const { + const int ageDelta = uint8_t(age - entry.age); + return entry.depth - ageDelta * 4 + entry.ttpv * 2; + } public: TTable() : table(defaultHash) {}; @@ -72,6 +80,10 @@ public: __builtin_prefetch(&table[hashKey % table.size()]); } + void IncreaseAge() { + age++; + } + TTEntry GetEntry(U64 &hashKey) { TTBucket *current = &table[hashKey % table.size()]; if (current->depthPreferred.hashKey == hashKey) {