[board] Put a cached Moves object into a OnceCell on Position

Cache move generation so we don't have to remake it every time.
This commit is contained in:
Eryn Wells 2024-01-22 08:12:28 -08:00
parent 8dbf44c741
commit c1008ef672

View file

@ -18,6 +18,7 @@ pub struct Position {
pieces: PieceBitBoards,
en_passant_square: Option<Square>,
sight: [OnceCell<BitBoard>; 2],
moves: OnceCell<Moves>,
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,
}