tt buckets | bench 1783856 #137

Merged
rektdie merged 1 commits from tt-buckets into main 2026-02-15 19:51:26 +01:00
3 changed files with 37 additions and 14 deletions

No files matched your search

+3 -3
View File
@@ -47,7 +47,7 @@ static int AdjustEval(Board &board, SearchContext* ctx, int eval) {
} }
static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) {
TTEntry current = ctx->TT.GetRawEntry(board.hashKey); TTEntry current = ctx->TT.GetEntry(board.hashKey);
if (current.hashKey == board.hashKey && current.bestMove == move) { if (current.hashKey == board.hashKey && current.bestMove == move) {
return 100000; return 100000;
} }
@@ -322,7 +322,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
TTEntry entry; TTEntry entry;
if (!ctx->excluded) { if (!ctx->excluded) {
entry = ctx->TT.GetRawEntry(board.hashKey); entry = ctx->TT.GetEntry(board.hashKey);
if (entry.hashKey == board.hashKey) { if (entry.hashKey == board.hashKey) {
bestScore = entry.score; bestScore = entry.score;
} }
@@ -420,7 +420,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
TTEntry entry; TTEntry entry;
if (!ctx->excluded) if (!ctx->excluded)
entry = ctx->TT.GetRawEntry(board.hashKey); entry = ctx->TT.GetEntry(board.hashKey);
const bool ttHit = entry.hashKey == board.hashKey; const bool ttHit = entry.hashKey == board.hashKey;
const bool ttpv = isPV | entry.ttpv; const bool ttpv = isPV | entry.ttpv;
+14 -1
View File
@@ -1,7 +1,20 @@
#include "tt.h" #include "tt.h"
void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove, bool ttpv) { void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove, bool ttpv) {
TTEntry *current = &table[hashKey % table.size()]; TTBucket *bucket = &table[hashKey % table.size()];
TTEntry *current = nullptr;
if (bucket->depthPreferred && bucket->depthPreferred.hashKey == hashKey) {
current = &bucket->depthPreferred;
} else if (bucket->alwaysReplace && bucket->alwaysReplace.hashKey == hashKey) {
current = &bucket->alwaysReplace;
} else if (ttpv) {
current = &bucket->depthPreferred;
} else if (!bucket->depthPreferred || bucket->depthPreferred.depth <= depth) {
current = &bucket->depthPreferred;
} else {
current = &bucket->alwaysReplace;
}
current->hashKey = hashKey; current->hashKey = hashKey;
current->nodeType = nodeType; current->nodeType = nodeType;
+20 -10
View File
@@ -39,16 +39,22 @@ public:
} }
}; };
class TTBucket {
public:
TTEntry depthPreferred;
TTEntry alwaysReplace;
};
// 1024 MB // 1024 MB
constexpr U64 maxHash = (1024 * 1000000) / sizeof(TTEntry); constexpr U64 maxHash = (1024 * 1000000) / sizeof(TTBucket);
// 8 MB // 8 MB
constexpr U64 defaultHash = (8 * 1000000) / sizeof(TTEntry); constexpr U64 defaultHash = (8 * 1000000) / sizeof(TTBucket);
constexpr int16_t invalidEntry = 11111; constexpr int16_t invalidEntry = 11111;
class TTable { class TTable {
private: private:
std::vector<TTEntry> table; std::vector<TTBucket> table;
public: public:
TTable() : table(defaultHash) {}; TTable() : table(defaultHash) {};
@@ -66,9 +72,15 @@ public:
__builtin_prefetch(&table[hashKey % table.size()]); __builtin_prefetch(&table[hashKey % table.size()]);
} }
TTEntry GetRawEntry(U64 &hashKey) { TTEntry GetEntry(U64 &hashKey) {
TTEntry *current = &table[hashKey % table.size()]; TTBucket *current = &table[hashKey % table.size()];
return *current; if (current->depthPreferred.hashKey == hashKey) {
return current->depthPreferred;
} else if (current->alwaysReplace.hashKey == hashKey) {
return current->alwaysReplace;
}
return TTEntry();
} }
// returns used space per mill // returns used space per mill
@@ -76,13 +88,11 @@ public:
int count = 0; int count = 0;
for (int i = 0; i < 1000; i++) { for (int i = 0; i < 1000; i++) {
if (table[i]) count++; if (table[i].depthPreferred || table[i].alwaysReplace) count++;
} }
return count; return count;
} }
TTEntry ReadEntry(U64 &hashKey, int depth, int alpha, int beta); void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove, bool ttpv);
void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move besteMove, bool ttpv);
}; };