Avoid Piece creation in BaseBoard.board_fen() for 108% speedup#1182
Open
jacksonthall22 wants to merge 3 commits intoniklasf:masterfrom
Open
Avoid Piece creation in BaseBoard.board_fen() for 108% speedup#1182jacksonthall22 wants to merge 3 commits intoniklasf:masterfrom
Piece creation in BaseBoard.board_fen() for 108% speedup#1182jacksonthall22 wants to merge 3 commits intoniklasf:masterfrom
Conversation
Piece creation in BaseBoard.board_fen() for 52% speedupPiece creation in BaseBoard.board_fen() for 108% speedup
Contributor
|
Just as a side remark: extracting all unique FENs from a large collection of (compressed) pgn files can be efficiently done with vondele/fastpopular. The main branch checks for uniqueness up to Zobrist hash collisions. Full uniqueness can easily be achieved if desired (at the cost of a larger memory footprint), see e.g. here. |
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.
Currently
BaseBoard.board_fen()callsself.piece_at()for each square while building the board FEN, which creates aPieceobject only to get itspiece.symbol(). I've identified that ephemeral object creation as a major speed bottleneck in some code I'm writing for Hyperchess to extract all unique FENs from the Lichess open database.This PR adds a block which skips the intermediate
Piececreation and sets the piece symbol manually by checking the square's piece with fast checks againstself's bitboards (.pawns,.knights, etc.), after checking that neitherpiece_atnorpiece_type_athave been overridden on the board instance (e.g. the change should not skip the existing path that relies on those methods ifselfis someMyCustomBoardthat inherits fromBoard/BaseBoardand defines its ownpiece_at/piece_type_at).Also moves this block up to get the promoted-piece mask once per call instead of once per occupied square:
Disclaimer: code was written with AI and after review I agree it's sound.
Benchmark
This script records time spent inside
board.board_fen()and was used to generate the before/after table below:total_elapsedcalls_per_s