Skip to content

Cache piece lookups and side occupancies in Board - #140

Merged
Hardcode3 merged 4 commits into
mainfrom
board_caching
Dec 2, 2026
Merged

Hardcode3 merged 4 commits into
mainfrom
board_caching

Conversation

@Hardcode3

@Hardcode3 Hardcode3 commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

📌 Cache piece lookups and side occupancies in Board

📄 Description

Board only 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() and enemy() 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:

  • a mailbox (std::array<std::optional<Piece>, 64>): get_piece() becomes a single array read;
  • one occupancy bitboard per color: friendly(), enemy(), white_pieces() and black_pieces() become a single read, and occupied() a single OR.

To keep the three representations and the Zobrist hash consistent, only set_piece() and remove_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:

  • The mutable pieces(Color, Piece::Type) accessor returned a Bitboard&, which could bypass the caches. It is replaced by the read-only get_pieces(Color, Piece::Type). get_pieces(Color) returns the side occupancy and replaces the private side_pieces().
  • Trivial accessors are now declared in the class and defined inline after it, all documented. StartingPosition() and Empty() move to the .cpp.
  • king_square() uses lsb() instead of pop_lsb() on a copy.
  • operator== still compares only the piece bitboards and the state, because the caches are derived from the bitboards.
  • The "Board storage" item is removed from todo.md.

🧩 Type of Change

  • 🚀 Performance improvement
  • 🔨 Refactoring

📊 Severity

  • 🟠 High: Board is the core representation used by move generation, execution and search. Any drift between the caches and the bitboards would produce wrong moves.

🔖 Release

  • 🚫 None: engine behavior is unchanged (same moves, same hashes). The only API change is internal to Board (pieces() → get_pieces()), and no code outside the class called it.

🗒️ Changelog Entry

- Changed: `Board` caches a square-to-piece mailbox and per-color occupancies, making `get_piece()`, `occupied()`, `friendly()` and `enemy()` constant time.

🧪 Testing

  • Built with the clang_debug preset.
  • ctest --preset quick-validation-clang-debug: 111/111 tests pass (unit tests + perft smoke).
  • These tests call set_piece(), remove_piece(), move_piece() and get_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.
  • The intermediate and deep tiers are left to CI (run-pre-merge-checks).

✅ 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

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.

@Hardcode3 Hardcode3 self-assigned this Dec 2, 2026
@Hardcode3 Hardcode3 added type: refactor Restructures or cleans up code without changing behavior run-pre-merge-checks Set this flag to run the final checks before merging a PR to main branch. area: board Board, Bitboard, Square, Piece, Move, FEN, Zobrist keys labels Dec 2, 2026
@Hardcode3 Hardcode3 changed the title Implementing Board caching Cache piece lookups and side occupancies in Board Dec 2, 2026
@Hardcode3
Hardcode3 marked this pull request as ready for review December 2, 2026 14:47
@Hardcode3 Hardcode3 added run-pre-merge-checks Set this flag to run the final checks before merging a PR to main branch. and removed run-pre-merge-checks Set this flag to run the final checks before merging a PR to main branch. labels Dec 2, 2026
@Hardcode3
Hardcode3 merged commit 2448daa into main Dec 2, 2026
18 of 26 checks passed
@Hardcode3
Hardcode3 deleted the board_caching branch December 2, 2026 14:59
@Hardcode3 Hardcode3 added release: none Does not affect the public API or functionality severity: high Major impact on core functionality, needs a careful review labels Dec 2, 2026
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: high Major impact on core functionality, needs a careful review 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