reduce copies

This commit is contained in:
Simon Gardling 2025-02-12 23:18:14 -05:00
parent 3815ff1a95
commit ad3d31e24a
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -195,12 +195,12 @@ impl FutureMoves {
.find_map(|(idx, node)| { .find_map(|(idx, node)| {
(node.parent == self.current_root && node.i == i && node.j == j).then_some(idx) (node.parent == self.current_root && node.i == i && node.j == j).then_some(idx)
}) })
.inspect(|&root| { .is_some_and(|root| {
self.current_root = Some(root); self.current_root = Some(root);
self.current_depth = self.max_depth - self.depth_of(root); self.current_depth = self.max_depth - self.depth_of(root);
self.prune_unrelated(); self.prune_unrelated();
true
}) })
.is_some()
} }
fn prune_unrelated(&mut self) { fn prune_unrelated(&mut self) {
@ -216,28 +216,40 @@ impl FutureMoves {
stack.extend(self.arena[idx].children.iter()); stack.extend(self.arena[idx].children.iter());
} }
let mut new_arena = Vec::with_capacity(self.arena.len());
let mut index_map = vec![None; self.arena.len()]; let mut index_map = vec![None; self.arena.len()];
// PERF: reverse iterator to avoid unneeded `memcpy`s when deconstructing the Arena // PERF: reverse iterator to avoid unneeded `memcpy`s when deconstructing the Arena
for (old_idx, keep) in retain.into_iter().enumerate().rev() { self.arena = retain
let mut node = self.arena.remove(old_idx); .into_iter()
if keep { .enumerate()
index_map[old_idx] = Some(new_arena.len()); .rev()
.enumerate()
.flat_map(|(new_idx, (old_idx, keep))| {
let node = self.arena.remove(old_idx);
if keep {
Some((new_idx, (old_idx, node)))
} else {
None
}
})
.map(|(new_idx, (old_idx, mut node))| {
index_map[old_idx] = Some(new_idx);
node.parent = node.parent.and_then(|p| index_map[p]); node.parent = node.parent.and_then(|p| index_map[p]);
node.children = node node.children.retain_mut(|c| {
.children if let Some(new_c) = index_map[*c] {
.into_iter() *c = new_c;
.filter_map(|c| index_map[c]) true
.collect(); } else {
false
}
});
new_arena.push(node); node
} })
} .collect();
self.arena = new_arena;
self.current_root = index_map[root]; self.current_root = index_map[root];
} }
} }
@ -249,7 +261,7 @@ pub struct ComplexAgent {
impl ComplexAgent { impl ComplexAgent {
pub const fn new(color: Piece) -> Self { pub const fn new(color: Piece) -> Self {
const MAX_DEPTH: usize = 10; const MAX_DEPTH: usize = 8;
const MAX_ARENA: usize = 20_000_000; const MAX_ARENA: usize = 20_000_000;
Self { Self {
color, color,