compiler optimizations + focus the benchmark futher

This commit is contained in:
Simon Gardling 2025-02-18 22:33:29 -05:00
parent 9854067ca7
commit 52179c040b
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
3 changed files with 36 additions and 28 deletions

View File

@ -16,6 +16,8 @@ path = "src/main.rs"
# for profiling # for profiling
debug = true debug = true
# increases perf at the cost of compile-time
codegen-units = 1
lto = true lto = true
[features] [features]

View File

@ -4,24 +4,21 @@ use std::time::Duration;
// use crate::future_move::FutureMove; // use crate::future_move::FutureMove;
use othello::{board::Board, future_moves::FutureMoves, piece::Piece}; use othello::{board::Board, future_moves::FutureMoves, piece::Piece};
fn future_move_bench(depth: usize, expire: usize) { fn extend_layers_test(depth: usize, expire: usize) {
let mut fut = FutureMoves::new(Piece::Black, depth, expire); let mut fut = FutureMoves::new(Piece::Black, depth, expire);
fut.update(&Board::new().starting_pos()); fut.create_root_raw(Board::new().starting_pos());
let _best_move = fut.best_move().inspect(|&(i, j)| { fut.extend_layers();
if !fut.update_root_coord(i, j) {
panic!("update_root_coord failed");
}
});
} }
fn criterion_benchmark(c: &mut Criterion) { fn criterion_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("future_move (expire 4)"); const EXPIRE: usize = 4;
group.measurement_time(Duration::from_secs(60)); let mut group = c.benchmark_group(format!("extend_layers (expire {})", EXPIRE));
group.measurement_time(Duration::from_secs(10));
for (depth, expire) in [(2, 4), (3, 4), (4, 4), (5, 4), (6, 4)].iter() { for (depth, expire) in (2..6).zip([EXPIRE].iter().cycle()) {
group.throughput(Throughput::Elements(*depth as u64)); group.throughput(Throughput::Elements(depth as u64));
group.bench_with_input(BenchmarkId::from_parameter(depth), depth, |b, depth| { group.bench_with_input(BenchmarkId::from_parameter(depth), &depth, |b, depth| {
b.iter(|| future_move_bench(*depth, *expire)); b.iter(|| extend_layers_test(*depth, *expire));
}); });
} }
group.finish(); group.finish();

View File

@ -97,7 +97,8 @@ impl FutureMoves {
} }
/// Generate children for all children of `nodes` /// Generate children for all children of `nodes`
fn extend_layers(&mut self) { /// only `pub` for the sake of benchmarking
pub fn extend_layers(&mut self) {
let mut next_nodes: Vec<usize> = (0..self.arena.len()) let mut next_nodes: Vec<usize> = (0..self.arena.len())
// we want to select all nodes that don't have children, or are lazy (need to maybe be regenerated) // we want to select all nodes that don't have children, or are lazy (need to maybe be regenerated)
.filter(|&idx| self.arena[idx].children.is_empty() || self.arena[idx].lazy_children) .filter(|&idx| self.arena[idx].children.is_empty() || self.arena[idx].lazy_children)
@ -283,12 +284,17 @@ impl FutureMoves {
if let Some(curr_board_idx) = curr_board { if let Some(curr_board_idx) = curr_board {
self.update_root_idx(curr_board_idx); self.update_root_idx(curr_board_idx);
} else { } else {
// println!("Generating root of FutureMoves"); self.create_root_raw(*board);
self.update_root_idx(0);
}
}
pub fn create_root_raw(&mut self, board: Board) {
self.arena.clear(); self.arena.clear();
self.arena.push(Move { self.arena.push(Move {
i: 0, i: 0,
j: 0, j: 0,
board: *board, board,
winner: Winner::None, winner: Winner::None,
parent: None, parent: None,
children: Vec::new(), children: Vec::new(),
@ -296,8 +302,6 @@ impl FutureMoves {
color: !self.agent_color, color: !self.agent_color,
lazy_children: false, lazy_children: false,
}); });
self.update_root_idx(0);
}
} }
/// Update the root based on the coordinate of the move /// Update the root based on the coordinate of the move
@ -317,9 +321,14 @@ impl FutureMoves {
.is_some() .is_some()
} }
fn update_root_idx(&mut self, idx: usize) { fn update_root_idx_raw(&mut self, idx: usize) {
self.current_root = Some(idx); self.current_root = Some(idx);
self.current_depth -= self.depth_of(idx) - 1; self.current_depth -= self.depth_of(idx) - 1;
}
fn update_root_idx(&mut self, idx: usize) {
self.update_root_idx_raw(idx);
self.refocus_tree(); self.refocus_tree();
self.extend_layers(); self.extend_layers();
self.compute_values(0..self.arena.len()); self.compute_values(0..self.arena.len());