Skip to content

Commit

Permalink
Optimization: early exit get_aggressive_path -93%
Browse files Browse the repository at this point in the history
Added a benchmark task where the player is near.
On the task where the player is near, big speedup. No change for task
where player is far (previous benchmark).
This should make a good difference in real play, since the bot is
chasing us it shouldn't always on the other side of the map.
  • Loading branch information
JesseEmond committed Oct 5, 2024
1 parent 2710622 commit 0d6de5f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
44 changes: 25 additions & 19 deletions bot/benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,32 @@ use pprof::criterion::{Output, PProfProfiler};
use devnull_bot::simulation::{get_aggressive_path, make_grid, Pos};

pub fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("get_aggressive_path 22x15", |b| {
let grid = make_grid(vec![
"######################",
"# #",
"# ######## ######## #",
"# # # #",
"# #### ### # #### # #",
"# #### # #### # #",
"# #### ### # #### # #",
"# # # #",
"# #### ### # # #### #",
"# # # # # # # #",
"# # ### # # # #",
"# # # #",
"# ######## ######## #",
"# #",
"######################",
]);
let grid = make_grid(vec![
"######################",
"# #",
"# ######## ######## #",
"# # # #",
"# #### ### # #### # #",
"# #### # #### # #",
"# #### ### # #### # #",
"# # # #",
"# #### ### # # #### #",
"# # # # # # # #",
"# # ### # # # #",
"# # # #",
"# ######## ######## #",
"# #",
"######################",
]);
c.bench_function("get_aggressive_path 22x15 far", |b| {
b.iter(|| get_aggressive_path(
black_box(&grid), &Pos { x: 5, y: 1 }, &Pos { x: 18, y: 13 }));
black_box(&grid), black_box(&Pos { x: 5, y: 1 }),
black_box(&Pos { x: 18, y: 13 })));
});
c.bench_function("get_aggressive_path 22x15 close", |b| {
b.iter(|| get_aggressive_path(
black_box(&grid), black_box(&Pos { x: 5, y: 1 }),
black_box(&Pos { x: 9, y: 1 })));
});
}

Expand Down
5 changes: 4 additions & 1 deletion bot/src/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ pub fn get_aggressive_path(grid: &Grid, from: &Pos, to: &Pos) -> Vec<Pos> {
// - sort is called at the start of the loop, so any newly added nodes in
// the same loop will keep their initial relative order from 'empty_tiles'
// instead of being in the order seen
// TODO: can early exit if found target
// TODO: Refactor to a struct, impl old slower method, unit test verifying
// that the behavior is the same.
const HIGH_COST: usize = 9999999;
Expand All @@ -202,6 +201,10 @@ pub fn get_aggressive_path(grid: &Grid, from: &Pos, to: &Pos) -> Vec<Pos> {
frontier_cost += 1;
}
let pos = frontier.pop_back().unwrap();
if pos == *to {
// Early exit if we found the target
break;
}
let mut frontier_adds = Vec::new();
let mut next_frontier_adds = Vec::new();
// Note: order is irrelevant, since we enforce order to match the JS
Expand Down

0 comments on commit 0d6de5f

Please sign in to comment.