Skip to content

Inline Zobrist updates behind a zobrist::Hash class - #141

Open
Hardcode3 wants to merge 10 commits into
mainfrom
zobrist_inline_mutators
Open

Hardcode3 wants to merge 10 commits into
mainfrom
zobrist_inline_mutators

Conversation

@Hardcode3

@Hardcode3 Hardcode3 commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

📌 Inline Zobrist updates behind a zobrist::Hash class

📄 Description

Zobrist updates were out-of-line free functions in zobrist.cpp, and they modified a hash passed as a plain uint64_t output parameter. Board::set_piece() and remove_piece() call them on every move.

This PR moves them inline into a small zobrist::Hash class that wraps the uint64_t:

// Before
zobrist::mutate_piece(square, piece, m_zobrist_hash);
// After
m_zobrist_hash.toggle_piece(square, piece);
Before After
using Key = uint64_t, NULL_HASH zobrist::Hash, Hash{}
compute_hash(board) Hash::compute(board)
mutate_piece(sq, piece, key) hash.toggle_piece(sq, piece)
mutate_side_to_move(key) hash.toggle_side_to_move()
mutate_castling_rights(state, key) hash.toggle_castling_rights(state)
mutate_en_passant_square(sq, key) hash.toggle_en_passant_square(sq)
mutate_board_state_diff(prev, next, key) hash.apply_state_change(prev, next)
  • Same cost as a uint64_t: every method except compute() is inline, and static_asserts check that Hash keeps the size of a uint64_t and stays trivially copyable.
  • Type safety: there is no constructor from an integer, so a bitboard or a seed cannot be used as a hash by mistake. value() returns the raw 64 bits.
  • Same values: the tables, seed and generation order are unchanged, so every position hashes to the same value as before.
  • BoardState moves to board_state.hpp, so that zobrist.hpp can use it inline without including board.hpp, which includes zobrist.hpp.
  • The comments in zobrist.hpp are rewritten to explain what a hash contains and how to update it.

🧩 Type of Change

  • 🔨 Refactoring
  • 🚀 Performance improvement

📊 Severity

  • 🟡 Medium: the hash backs repetition detection and Board::operator==. The values are unchanged and covered by tests.

🔖 Release

  • 🚫 None: no change in engine behavior. The API changes are internal to the library.

🗒️ Changelog Entry

- Changed: Zobrist hashes are a `zobrist::Hash` class with inline update methods, replacing the `uint64_t` alias and the `zobrist::mutate_*()` functions.

🧪 Testing

  • clang_debug and clang_release build without warnings. 111/111 tests pass in quick-validation-clang-debug and quick-validation-clang-release.
  • The existing Zobrist tests are ported, including the constexpr checks. New tests cover the default value, equality, chaining, stream output and apply_state_change() ignoring the move counters.
  • zobrist.hpp and zobrist.cpp reach 100% region, line and branch coverage.

✅ Checklist

  • I have reviewed my own code before submitting.
  • My code follows the project's coding standards.
  • I have commented my code, particularly in hard-to-understand areas.
  • Documentation has been updated to reflect the changes.
  • I have added tests proving that my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have checked my code for security vulnerabilities.
  • The pull request title is clear and concise.
  • I have reviewed the content of this pull request.

📝 Additional Notes

No benchmark is included, so the speed-up from inlining is expected but not measured.

Hardcode3 and others added 9 commits December 2, 2026 12:57
BoardState moves to its own header so zobrist.hpp can include it without
including board.hpp. Every mutator and castling_rights_index() are now
defined inline in zobrist.hpp; only compute_hash(), which needs the full
Board, stays in zobrist.cpp.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Hardcode3 Hardcode3 changed the title Making zobrist mutators inline Inline Zobrist updates behind a zobrist::Key class Dec 2, 2026
@Hardcode3 Hardcode3 self-assigned this Dec 2, 2026
@Hardcode3 Hardcode3 added type: refactor Restructures or cleans up code without changing behavior area: board Board, Bitboard, Square, Piece, Move, FEN, Zobrist keys run-pre-merge-checks Set this flag to run the final checks before merging a PR to main branch. release: none Does not affect the public API or functionality severity: medium Changes behavior in a localized and tested way labels Dec 2, 2026
@Hardcode3
Hardcode3 marked this pull request as ready for review December 2, 2026 19:17
@Hardcode3 Hardcode3 changed the title Inline Zobrist updates behind a zobrist::Key class Inline Zobrist updates behind a zobrist::Hash class Dec 2, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: board Board, Bitboard, Square, Piece, Move, FEN, Zobrist keys release: none Does not affect the public API or functionality run-pre-merge-checks Set this flag to run the final checks before merging a PR to main branch. severity: medium Changes behavior in a localized and tested way type: refactor Restructures or cleans up code without changing behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant