Add a very simple optimalisation

Piece() requires a hash lookup in an lru_cache, and is thus sort-of
expensive.  This patch reduces the amount of calls to Piece()
parent 508bbd81
Pipeline #20066787 passed with stages
in 17 minutes and 44 seconds
......@@ -168,11 +168,12 @@ def attacked(position: Position, side: Side, square: Square) -> bool:
# Pawn
pawn_paths = _PAWN_PATHS[
opponent(side)][1] # type: Iterable[_DirectionPath]
pawn_piece = Piece(_PAWN, side)
for path in pawn_paths:
target = square.in_bounds(path)
if not target:
continue
if position.board.get(target) == Piece(_PAWN, side):
if position.board.get(target) == pawn_piece:
return True
# Pretend our square is a knight, a bishop or a rook. If an identical
......@@ -318,8 +319,9 @@ def is_check(position: Position, side: Side = None) -> bool:
"""
if side is None:
side = position.side_to_play
king_piece = Piece(_KING, side)
for square, piece in position.board.all_pieces():
if piece == Piece(_KING, side):
if piece == king_piece:
return attacked(position, opponent(side), square)
LOGGER.warning('no king in %s', position)
return False
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment