MAJOR (25%+) perf increase with propegate iterator and copies

This commit is contained in:
Simon Gardling 2025-02-18 20:13:19 -05:00
parent 3096c4d85b
commit 176505b897
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -166,7 +166,7 @@ impl Board {
} }
/// Get a reference to a backing [`BitBoard`] /// Get a reference to a backing [`BitBoard`]
pub const fn board(&self, color: Piece) -> &BitBoard { const fn board(&self, color: Piece) -> &BitBoard {
match color { match color {
Piece::Black => &self.black_board, Piece::Black => &self.black_board,
Piece::White => &self.white_board, Piece::White => &self.white_board,
@ -174,7 +174,7 @@ impl Board {
} }
/// Get a mutable reference to a backing [`BitBoard`] /// Get a mutable reference to a backing [`BitBoard`]
pub const fn board_mut(&mut self, color: Piece) -> &mut BitBoard { const fn board_mut(&mut self, color: Piece) -> &mut BitBoard {
match color { match color {
Piece::Black => &mut self.black_board, Piece::Black => &mut self.black_board,
Piece::White => &mut self.white_board, Piece::White => &mut self.white_board,
@ -249,13 +249,19 @@ impl Board {
return 0; return 0;
}; };
let pos_s: Vec<(usize, usize)> = self.propegate_from_dry(i, j, starting_color).collect(); // PERF! avoid clones and collections here using raw pointers
let pos_len = pos_s.len(); let iterator = unsafe {
for (i, j) in pos_s { // SAFETY! `propegate_from_dry` should not have overlapping chains
// if overlapping chains were to exist, `self.place_unchecked` could collide with `self.get`
(&*(self as *const Self)).propegate_from_dry(i, j, starting_color)
};
let mut count = 0;
for &(i, j) in iterator {
self.place_unchecked(i, j, starting_color); self.place_unchecked(i, j, starting_color);
count += 1;
} }
pos_len count
} }
/// Propegate piece captures originating from (i, j) /// Propegate piece captures originating from (i, j)
@ -266,7 +272,7 @@ impl Board {
i: usize, i: usize,
j: usize, j: usize,
starting_color: Piece, starting_color: Piece,
) -> impl Iterator<Item = (usize, usize)> + use<'_> { ) -> impl Iterator<Item = &(usize, usize)> + use<'_> {
ADJ_LOOKUP[i][j] ADJ_LOOKUP[i][j]
.iter() .iter()
.filter_map(move |chain| { .filter_map(move |chain| {
@ -281,7 +287,6 @@ impl Board {
end_idx.and_then(|idx| chain.get(..idx)) end_idx.and_then(|idx| chain.get(..idx))
}) })
.flatten() .flatten()
.cloned()
} }
/// Count the number of a type of [`Piece`] on the board /// Count the number of a type of [`Piece`] on the board