Repository navigation
Cache piece lookups and side occupancies in Board - #140
Merged
Merged
Conversation
Hardcode3
force-pushed
the
board_caching
branch
from
December 2, 2026 13:45
a387688 to
f5b5aa1
Compare
Hardcode3
marked this pull request as ready for review
December 2, 2026 14:47
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📌 Cache piece lookups and side occupancies in
Board📄 Description
Boardonly stored one bitboard per color and piece type, so the most frequent queries were recomputed on every call:get_piece()tested up to 12 bitboards to find what stands on a square;occupied(),friendly()andenemy()OR-ed 6 to 12 bitboards each time.This PR adds two caches next to the piece bitboards, so these queries now run in constant time:
std::array<std::optional<Piece>, 64>):get_piece()becomes a single array read;friendly(),enemy(),white_pieces()andblack_pieces()become a single read, andoccupied()a single OR.To keep the three representations and the Zobrist hash consistent, only
set_piece()andremove_piece()write to them. Every other edit goes through these two functions:move_piece()and the FEN constructor. Both accept any square:set_piece()replaces whatever piece stands on the square;remove_piece()does nothing on an empty square.Other changes in
Board:pieces(Color, Piece::Type)accessor returned aBitboard&, which could bypass the caches. It is replaced by the read-onlyget_pieces(Color, Piece::Type).get_pieces(Color)returns the side occupancy and replaces the privateside_pieces().inlineafter it, all documented.StartingPosition()andEmpty()move to the.cpp.king_square()useslsb()instead ofpop_lsb()on a copy.operator==still compares only the piece bitboards and the state, because the caches are derived from the bitboards.todo.md.🧩 Type of Change
📊 Severity
Boardis the core representation used by move generation, execution and search. Any drift between the caches and the bitboards would produce wrong moves.🔖 Release
Board(pieces()→get_pieces()), and no code outside the class called it.🗒️ Changelog Entry
🧪 Testing
clang_debugpreset.ctest --preset quick-validation-clang-debug: 111/111 tests pass (unit tests + perft smoke).set_piece(),remove_piece(),move_piece()andget_piece()on every move. A desync between the mailbox, the occupancies and the bitboards would show up as wrong perft node counts or Zobrist mismatches.run-pre-merge-checks).✅ Checklist
📝 Additional Notes
This PR includes no dedicated cache tests and no benchmark. The existing perft and unit tests cover the invariants. A before/after comparison of perft nodes per second would measure the gain.