allocator optimizations

This commit is contained in:
2025-02-10 01:33:21 -05:00
parent 598c38efd6
commit 3d3eb01143
5 changed files with 41 additions and 50 deletions

View File

@@ -136,6 +136,9 @@ impl Board {
/// Propegate piece captures originating from (i, j)
/// DO NOT USE THIS ALONE, this should be called as a part of
/// [`Board::place`] or [`Board::place_and_prop_unchecked`]
// TODO! this function is responsible for approx 64% of the time spent computing moves in `ComplexAgent`
// IDEAS: early-exit from each chain so we don't have to call `diag` (which allocs a lot and uses a lot of cycles)
// NOTE! got it down to 24.86% (61.1% decrease) with allocator optimizations
fn propegate_from_dry(&self, i: usize, j: usize, starting_color: Piece) -> Vec<(usize, usize)> {
// Create all chains from the piece being propegated from in `i` and `j` coordinates
let (i_chain, j_chain) = (
@@ -143,21 +146,16 @@ impl Board {
split_from(0, BOARD_SIZE - 1, j),
);
let mut chains: Vec<Vec<(usize, usize)>> = i_chain
.into_iter()
.map(|range| range.into_iter().map(|i| (i, j)).collect())
.collect();
let mut chains: Vec<Vec<(usize, usize)>> = Vec::with_capacity(8);
chains.extend(
j_chain
.into_iter()
.map(|range| range.into_iter().map(|j| (i, j)).collect()),
);
chains.extend(i_chain.map(|range| range.into_iter().map(|i| (i, j)).collect()));
chains.extend(j_chain.map(|range| range.into_iter().map(|j| (i, j)).collect()));
// handle diagonals
chains.extend(diag(i, j, 0, 0, BOARD_SIZE - 1, BOARD_SIZE - 1));
let mut fill: Vec<(usize, usize)> = Vec::new();
let mut fill: Vec<(usize, usize)> = Vec::with_capacity(chains.iter().map(Vec::len).sum());
for chain in chains {
for (chain_length, &(new_i, new_j)) in chain.iter().enumerate() {
@@ -167,15 +165,14 @@ impl Board {
};
if piece == &starting_color {
// fill all opposite colors with this color
let Some(history) = chain.get(..chain_length) else {
break;
};
// fill all opposite colors with this color
for &(i_o, j_o) in history {
fill.push((i_o, j_o));
// get history of this chain
if let Some(history) = chain.get(..chain_length) {
// fill all opposite colors with this color
for &(i_o, j_o) in history {
fill.push((i_o, j_o));
}
}
// either the other pieces were replaced, or this was an invalid chain,
// in both cases, the loop needs to be breaked
break;