Skip to content

Commit 35af125

Browse files
tt size optimization
1 parent f595148 commit 35af125

File tree

4 files changed

+62
-28
lines changed

4 files changed

+62
-28
lines changed

main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ int runPerft(int depth, const char* fen) {
201201
#endif
202202

203203
int main() {
204+
204205
#ifndef wasm
205206
Interface::CLI interface;
206207
interface.start();

src/Search.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ Move Search::search(ChessBoard&board, const int timeAllowed) {
7474

7575
if (search.lastPV.empty()) {
7676
auto entry = tt.getEntry(board.hashCode, 0);
77-
std::cout << "problem HASH: " << board.hashCode << " TT: key- " << entry.key << ", move- " << entry.bestMove << ", type- " << entry.nodeType <<
78-
", depth- " << entry.depth << ", score- " << entry.score << std::endl;
77+
std::cout << "problem HASH: " << board.hashCode << " TT: key- " << entry.key << ", move- " << entry.bestMove << ", type- " << entry.nodeType() <<
78+
", depth- " << entry.depth() << ", score- " << entry.score() << std::endl;
7979
return NULL_MOVE;
8080
}
8181

@@ -348,14 +348,14 @@ std::vector<Move> Search::collectPV(const int depth) const {
348348
int pvDepth = 0;
349349
while (tt.contains(board.hashCode)) {
350350
const TranspositionTable::Entry entry = tt.getEntry(board.hashCode, 0);
351-
if (entry.nodeType != TranspositionTable::EXACT) break;
351+
if (entry.nodeType() != TranspositionTable::EXACT) break;
352352
Move move = entry.bestMove;
353353

354354
if (pvPositions.contains(board.hashCode)) {
355355
std::cout << "Cycle in PV!" << std::endl;
356356
board.makeMove(move);
357357
pv.push_back(move);
358-
scores.push_back(entry.score);
358+
scores.push_back(entry.score());
359359
pvDepth++;
360360
break;
361361
}
@@ -381,7 +381,7 @@ std::vector<Move> Search::collectPV(const int depth) const {
381381
pvPositions.insert(board.hashCode);
382382
board.makeMove(move);
383383
pv.push_back(move);
384-
scores.push_back(entry.score);
384+
scores.push_back(entry.score());
385385
pvDepth++;
386386
}
387387
std::string pvString;
@@ -404,20 +404,20 @@ std::vector<Move> Search::collectPV(const int depth) const {
404404
bool Search::getTransposition(const uint64_t hash, const int depth, const int ply, int&score, const int&alpha, const int&beta, Move&hashMove) {
405405
if (tt.contains(hash)) {
406406
const TranspositionTable::Entry entry = tt.getEntry(hash, ply);
407-
if (entry.depth >= depth) {
408-
switch (entry.nodeType) {
407+
if (entry.depth() >= depth) {
408+
switch (entry.nodeType()) {
409409
case TranspositionTable::EXACT:
410-
score = entry.score;
410+
score = entry.score();
411411
return true;
412412
case TranspositionTable::UPPERBOUND:
413-
if (entry.score <= alpha) {
414-
score = entry.score;
413+
if (entry.score() <= alpha) {
414+
score = entry.score();
415415
return true;
416416
}
417417
break;
418418
case TranspositionTable::LOWERBOUND:
419-
if (entry.score >= beta) {
420-
score = entry.score;
419+
if (entry.score() >= beta) {
420+
score = entry.score();
421421
return true;
422422
}
423423
default: break;

src/TranspositionTable.cpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "TranspositionTable.h"
22
#include <cmath>
3-
#include <cstdio>
43

54
#include "MoveGenerator.h"
65

@@ -11,9 +10,9 @@ TranspositionTable::Entry TranspositionTable::getEntry(const uint64_t key, const
1110
const int index = key % TT_SIZE;
1211
Entry entry = entries[index];
1312

14-
if (abs(entry.score) >= MIN_MATE_SCORE) {
15-
const int sign = entry.score > 0 ? 1 : -1;
16-
entry.score = sign * (abs(entry.score) - ply);
13+
if (abs(entry.score()) >= MIN_MATE_SCORE) {
14+
const int sign = entry.score() > 0 ? 1 : -1;
15+
entry.setScore(sign * (abs(entry.score()) - ply));
1716
}
1817
reads++;
1918

@@ -22,7 +21,7 @@ TranspositionTable::Entry TranspositionTable::getEntry(const uint64_t key, const
2221

2322
bool TranspositionTable::contains(const uint64_t key) {
2423
const int index = key % TT_SIZE;
25-
const bool exists = entries[index].nodeType != EMPTY;
24+
const bool exists = entries[index].nodeType() != EMPTY;
2625
const bool sameKey = (entries[index].key == key);
2726
if (exists && !sameKey) collisions++;
2827

@@ -37,17 +36,17 @@ void TranspositionTable::setEntry(const ChessBoard&board, const Move bestMove, c
3736
score = sign * (abs(score) + ply);
3837
}
3938

40-
const NodeType savedType = entries[index].nodeType;
39+
const NodeType savedType = entries[index].nodeType();
4140

42-
const Entry entry = {board.hashCode, bestMove, depth, score, nodeType};
41+
const Entry entry = Entry(board.hashCode, bestMove, depth, score, nodeType);
4342

4443
//REPLACEMENT SCHEME
4544
// 1. Prefer EXACT nodes to bounds
4645
// 2. Prefer deeper nodes to shallower
4746

4847
if (savedType != EMPTY) {
4948
if ((savedType != EXACT && nodeType != EXACT) || (savedType == EXACT && nodeType == EXACT)) {
50-
if (entries[index].depth <= depth) write(index, entry);
49+
if (entries[index].depth() <= depth) write(index, entry);
5150
}
5251
else if (savedType != EXACT) write(index, entry);
5352
}
@@ -68,14 +67,16 @@ void TranspositionTable::resetCounters() {
6867
int TranspositionTable::occupancy() const {
6968
int occupied = 0;
7069
for (int i = 0; i < TT_SIZE; i++) {
71-
if (entries[i].nodeType != EMPTY) occupied++;
70+
if (entries[i].nodeType() != EMPTY) occupied++;
7271
}
7372
return occupied;
7473
}
7574

7675
void TranspositionTable::clear() {
7776
resetCounters();
7877
for (Entry&entry: entries) {
79-
entry.nodeType = EMPTY;
78+
entry.setNodeType(EMPTY);
8079
}
8180
}
81+
82+
TranspositionTable::TranspositionTable()= default;

src/TranspositionTable.h

+38-6
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,63 @@ class TranspositionTable {
1818
LOWERBOUND = 2,
1919
UPPERBOUND = 3,
2020
};
21+
2122
struct Entry {
2223
uint64_t key;
23-
Move bestMove;
24-
int depth;
25-
int score;
26-
NodeType nodeType;
24+
Move bestMove{};
25+
uint32_t data;
26+
27+
uint8_t depth() const {
28+
return (data & 0xFF0000) >> 16;
29+
}
30+
31+
int16_t score() const {
32+
return data & 0xFFFF;
33+
}
34+
35+
void setScore(int16_t score) {
36+
data = (data & 0xFFF0000) | score;
37+
}
38+
39+
NodeType nodeType() const {
40+
return (NodeType) ((data & 0xF000000) >> 24);
41+
}
42+
43+
void setNodeType(NodeType nodeType) {
44+
data = (data & 0x0FFFFFF) | (((uint8_t) nodeType) << 24);
45+
}
46+
47+
Entry(uint64_t key, Move bestMove, int depth, int score, NodeType nodeType) {
48+
this->key = key;
49+
this->bestMove = bestMove;
50+
this->data = 0 | (0xFFFF & ((int16_t) score)) | (((uint8_t) depth) << 16) | (((uint8_t) nodeType) << 24);
51+
}
52+
53+
Entry() = default;
2754
};
2855

56+
TranspositionTable();
57+
2958
bool contains(uint64_t key);
3059

3160
Entry getEntry(uint64_t key, int ply);
3261

33-
void setEntry(const ChessBoard&board, Move bestMove, int depth, int score, NodeType nodeType, int ply);
62+
void setEntry(const ChessBoard &board, Move bestMove, int depth, int score, NodeType nodeType, int ply);
3463

3564
int reads;
3665
int writes;
3766
int collisions;
67+
3868
void resetCounters();
69+
3970
int occupancy() const;
71+
4072
void clear();
4173

4274
std::array<Entry, TT_SIZE> entries;
4375

4476
private:
45-
void write(int index, const Entry&entry);
77+
void write(int index, const Entry &entry);
4678

4779

4880
};

0 commit comments

Comments
 (0)