diff --git a/bot/benches/benchmarks.rs b/bot/benches/benchmarks.rs index f326f39..1252e97 100644 --- a/bot/benches/benchmarks.rs +++ b/bot/benches/benchmarks.rs @@ -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 }))); }); } diff --git a/bot/src/simulation.rs b/bot/src/simulation.rs index 2597934..9b91e1b 100644 --- a/bot/src/simulation.rs +++ b/bot/src/simulation.rs @@ -184,7 +184,6 @@ pub fn get_aggressive_path(grid: &Grid, from: &Pos, to: &Pos) -> Vec { // - 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; @@ -202,6 +201,10 @@ pub fn get_aggressive_path(grid: &Grid, from: &Pos, to: &Pos) -> Vec { 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