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)| {
(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_depth = self.max_depth - self.depth_of(root);
self.prune_unrelated();
true
})
.is_some()
}
fn prune_unrelated(&mut self) {
@ -216,28 +216,40 @@ impl FutureMoves {
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()];
// PERF: reverse iterator to avoid unneeded `memcpy`s when deconstructing the Arena
for (old_idx, keep) in retain.into_iter().enumerate().rev() {
let mut node = self.arena.remove(old_idx);
if keep {
index_map[old_idx] = Some(new_arena.len());
self.arena = retain
.into_iter()
.enumerate()
.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.children = node
.children
.into_iter()
.filter_map(|c| index_map[c])
.collect();
node.children.retain_mut(|c| {
if let Some(new_c) = index_map[*c] {
*c = new_c;
true
} else {
false
}
});
new_arena.push(node);
}
}
node
})
.collect();
self.arena = new_arena;
self.current_root = index_map[root];
}
}
@ -249,7 +261,7 @@ pub struct ComplexAgent {
impl ComplexAgent {
pub const fn new(color: Piece) -> Self {
const MAX_DEPTH: usize = 10;
const MAX_DEPTH: usize = 8;
const MAX_ARENA: usize = 20_000_000;
Self {
color,