Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: remove second hash history keeping #355

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions src/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

Board::Board(const std::string &fen) {
state_history_.reserve(MAX_PLY);
hash_history_.reserve(512);

side_to_move_ = WHITE;
en_passant_square_ = NO_SQ;
Expand All @@ -27,8 +26,6 @@ Board::Board(const Board &other) {

state_history_ = other.state_history_;

hash_history_ = other.hash_history_;

pieces_bb_ = other.pieces_bb_;
board_ = other.board_;

Expand Down Expand Up @@ -58,8 +55,6 @@ Board &Board::operator=(const Board &other) {

state_history_ = other.state_history_;

hash_history_ = other.hash_history_;

pieces_bb_ = other.pieces_bb_;
board_ = other.board_;

Expand Down Expand Up @@ -188,7 +183,6 @@ void Board::setFen(const std::string &fen, bool update_acc) {
}

state_history_.clear();
hash_history_.clear();
accumulators_->clear();

hash_key_ = zobrist();
Expand Down Expand Up @@ -261,9 +255,9 @@ std::string Board::getFen() const {
bool Board::isRepetition(int draw) const {
uint8_t c = 0;

for (int i = static_cast<int>(hash_history_.size()) - 2;
i >= 0 && i >= static_cast<int>(hash_history_.size()) - half_move_clock_ - 1; i -= 2) {
if (hash_history_[i] == hash_key_) c++;
for (int i = static_cast<int>(state_history_.size()) - 2;
i >= 0 && i >= static_cast<int>(state_history_.size()) - half_move_clock_ - 1; i -= 2) {
if (state_history_[i].hash == hash_key_) c++;
if (c == draw) return true;
}

Expand Down Expand Up @@ -326,8 +320,8 @@ bool Board::isAttacked(Color c, Square sq, Bitboard occ) const {
}

void Board::makeNullMove() {
state_history_.emplace_back(en_passant_square_, castling_rights_, half_move_clock_, NONE);

state_history_.emplace_back(hash_key_, castling_rights_, en_passant_square_, half_move_clock_,
NONE);
// Update the hash key
hash_key_ ^= zobrist::sideToMove();
if (en_passant_square_ != NO_SQ)
Expand All @@ -345,15 +339,14 @@ void Board::unmakeNullMove() {
const State restore = state_history_.back();
state_history_.pop_back();

en_passant_square_ = restore.en_passant;
en_passant_square_ = restore.enpassant;

hash_key_ ^= zobrist::sideToMove();
if (en_passant_square_ != NO_SQ)
hash_key_ ^= zobrist::enpassant(squareFile(en_passant_square_));

castling_rights_ = restore.castling;
half_move_clock_ = restore.half_move;

half_move_clock_ = restore.half_moves;
full_move_number_--;
side_to_move_ = ~side_to_move_;
}
Expand Down
40 changes: 22 additions & 18 deletions src/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "types.h"
#include "zobrist.h"


extern TranspositionTable TTable;

class Board {
Expand Down Expand Up @@ -89,6 +88,14 @@ class Board {

[[nodiscard]] Bitboard all() const;

/// @brief Returns the square of the king for a certain color
/// @param color
/// @return
[[nodiscard]] Square kingSq(Color color) const {
assert(pieces(PieceType::KING, color) != 0);
return builtin::lsb(pieces(PieceType::KING, color));
}

// Gets individual piece bitboards

[[nodiscard]] constexpr Bitboard pieces(Piece piece) const { return pieces_bb_[piece]; }
Expand All @@ -111,6 +118,11 @@ class Board {
/// @return
[[nodiscard]] Color colorOf(Square loc) const;

/// @brief Checks if a square is attacked by the given color.
/// @param c
/// @param sq
/// @param occ
/// @return
[[nodiscard]] bool isAttacked(Color c, Square sq, Bitboard occ) const;

void updateHash(Move move);
Expand Down Expand Up @@ -141,8 +153,6 @@ class Board {

void clearStacks();

void clearHash() { hash_history_.clear(); }

friend std::ostream &operator<<(std::ostream &os, const Board &b);

bool chess960 = false;
Expand All @@ -152,10 +162,6 @@ class Board {

std::vector<State> state_history_;

// keeps track of previous hashes, used for
// repetition detection
std::vector<U64> hash_history_;

std::array<Bitboard, 12> pieces_bb_ = {};
std::array<Piece, MAX_SQ> board_{};

Expand Down Expand Up @@ -230,10 +236,10 @@ inline void Board::updateHash(Move move) {
const Piece capture = board_[to_sq];
const Rank rank = squareRank(to_sq);

hash_history_.emplace_back(hash_key_);

if (en_passant_square_ != NO_SQ)
if (en_passant_square_ != NO_SQ) {
hash_key_ ^= zobrist::enpassant(squareFile(en_passant_square_));
}

en_passant_square_ = NO_SQ;

hash_key_ ^= zobrist::castling(castling_rights_.getHashIndex());
Expand Down Expand Up @@ -342,7 +348,8 @@ void Board::makeMove(const Move move) {
// STORE STATE HISTORY
// *****************************

state_history_.emplace_back(en_passant_square_, castling_rights_, half_move_clock_, capture);
state_history_.emplace_back(hash_key_, castling_rights_, en_passant_square_, half_move_clock_,
capture);

if constexpr (updateNNUE) accumulators_->push();

Expand Down Expand Up @@ -418,7 +425,6 @@ void Board::makeMove(const Move move) {
template <bool updateNNUE>
void Board::unmakeMove(Move move) {
const State restore = state_history_.back();

const Square from_sq = from(move);
const Square to_sq = to(move);

Expand All @@ -433,15 +439,13 @@ void Board::unmakeMove(Move move) {
accumulators_->pop();
}

hash_key_ = hash_history_.back();
hash_key_ = restore.hash;
en_passant_square_ = restore.enpassant;
castling_rights_ = restore.castling;
half_move_clock_ = restore.half_moves;

hash_history_.pop_back();
state_history_.pop_back();

en_passant_square_ = restore.en_passant;
castling_rights_ = restore.castling;
half_move_clock_ = restore.half_move;

full_move_number_--;
side_to_move_ = ~side_to_move_;

Expand Down
2 changes: 0 additions & 2 deletions src/datagen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "rand.h"
#include "syzygy/Fathom/src/tbprobe.h"


namespace datagen {

std::string stringFenData(const fenData &fen_data, double score) {
Expand Down Expand Up @@ -121,7 +120,6 @@ void TrainingData::randomPlayout(std::ofstream &file, Board &board, Movelist &mo
}

board.refreshNNUE(board.getAccumulator());
board.clearHash();
search->board = board;

fenData sfens;
Expand Down
17 changes: 10 additions & 7 deletions src/types/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
#include "castling_rights.h"

struct State {
Square en_passant{};
CastlingRights castling{};
uint8_t half_move{};
Piece captured_piece = NONE;
U64 hash;
CastlingRights castling;
Square enpassant;
uint8_t half_moves;
Piece captured_piece;

State(Square en_passant, CastlingRights castling, uint8_t half_move, Piece captured_piece)
: en_passant(en_passant),
State(const U64 &hash, const CastlingRights &castling, const Square &enpassant,
const uint8_t &half_moves, const Piece &captured_piece)
: hash(hash),
castling(castling),
half_move(half_move),
enpassant(enpassant),
half_moves(half_moves),
captured_piece(captured_piece) {}
};