From c1008ef672bff5ca9e9392763cfc67d4fb3125a0 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Mon, 22 Jan 2024 08:12:28 -0800 Subject: [PATCH] [board] Put a cached Moves object into a OnceCell on Position Cache move generation so we don't have to remake it every time. --- board/src/position/position.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/board/src/position/position.rs b/board/src/position/position.rs index abdfbe1..484293e 100644 --- a/board/src/position/position.rs +++ b/board/src/position/position.rs @@ -18,6 +18,7 @@ pub struct Position { pieces: PieceBitBoards, en_passant_square: Option, sight: [OnceCell; 2], + moves: OnceCell, half_move_counter: u16, full_move_number: u16, @@ -31,6 +32,7 @@ impl Position { pieces: PieceBitBoards::default(), en_passant_square: None, sight: [OnceCell::new(), OnceCell::new()], + moves: OnceCell::new(), half_move_counter: 0, full_move_number: 1, } @@ -62,6 +64,7 @@ impl Position { pieces: PieceBitBoards::new([white_pieces, black_pieces]), en_passant_square: None, sight: [OnceCell::new(), OnceCell::new()], + moves: OnceCell::new(), half_move_counter: 0, full_move_number: 1, } @@ -121,8 +124,9 @@ impl Position { self.piece_on_square(square) } - pub fn moves(&self) -> Moves { - Moves::new(self, self.color_to_move) + pub fn moves(&self) -> &Moves { + self.moves + .get_or_init(|| Moves::new(self, self.color_to_move)) } /// Return a BitBoard representing the set of squares containing a piece. @@ -215,6 +219,7 @@ impl Position { en_passant_square, pieces, sight: [OnceCell::new(), OnceCell::new()], + moves: OnceCell::new(), half_move_counter: 0, full_move_number: 1, }