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
debug = true
# increases perf at the cost of compile-time
codegen-units = 1
lto = true
[features]

View File

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

View File

@ -97,7 +97,8 @@ impl FutureMoves {
}
/// 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())
// 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)
@ -283,23 +284,26 @@ impl FutureMoves {
if let Some(curr_board_idx) = curr_board {
self.update_root_idx(curr_board_idx);
} else {
// println!("Generating root of FutureMoves");
self.arena.clear();
self.arena.push(Move {
i: 0,
j: 0,
board: *board,
winner: Winner::None,
parent: None,
children: Vec::new(),
value: 0,
color: !self.agent_color,
lazy_children: false,
});
self.create_root_raw(*board);
self.update_root_idx(0);
}
}
pub fn create_root_raw(&mut self, board: Board) {
self.arena.clear();
self.arena.push(Move {
i: 0,
j: 0,
board,
winner: Winner::None,
parent: None,
children: Vec::new(),
value: 0,
color: !self.agent_color,
lazy_children: false,
});
}
/// Update the root based on the coordinate of the move
/// Returns a boolean, `true` if the operation was successful, false if not
#[must_use = "You must check if the root was properly set"]
@ -317,9 +321,14 @@ impl FutureMoves {
.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_depth -= self.depth_of(idx) - 1;
}
fn update_root_idx(&mut self, idx: usize) {
self.update_root_idx_raw(idx);
self.refocus_tree();
self.extend_layers();
self.compute_values(0..self.arena.len());