Skip to content

Commit cf63a62

Browse files
committed
feat(benches): add restricted DFS benchmarks
Add benchmarks to investigate DFS poor performance when no path can be found.
1 parent 91cdea8 commit cf63a62

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

benches/algos-fill.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,28 @@ impl Pt {
2727
}
2828

2929
#[inline]
30-
fn successors(pt: &Pt) -> Vec<Pt> {
30+
fn successors_limit(pt: &Pt, size: u16) -> Vec<Pt> {
3131
let mut ret = Vec::with_capacity(4);
3232
if 0 < pt.x {
3333
ret.push(Pt::new(pt.x - 1, pt.y));
3434
}
35-
if pt.x < 32 {
35+
if pt.x < size {
3636
ret.push(Pt::new(pt.x + 1, pt.y));
3737
}
3838
if 0 < pt.y {
3939
ret.push(Pt::new(pt.x, pt.y - 1));
4040
}
41-
if pt.y < 32 {
41+
if pt.y < size {
4242
ret.push(Pt::new(pt.x, pt.y + 1));
4343
}
4444
ret
4545
}
4646

47+
#[inline]
48+
fn successors(pt: &Pt) -> Vec<Pt> {
49+
successors_limit(pt, 32)
50+
}
51+
4752
fn corner_to_corner_astar(c: &mut Criterion) {
4853
c.bench_function("fill-corner_to_corner_astar", |b| {
4954
b.iter(|| {
@@ -162,6 +167,19 @@ fn no_path_bfs(c: &mut Criterion) {
162167
});
163168
}
164169

170+
// We have to restrict the grid to a very small one, as the resources
171+
// needed to run dfs over all the path combination augment very quickly.
172+
fn no_path_dfs_restricted(c: &mut Criterion) {
173+
c.bench_function("fill-no_path_dfs", |b| {
174+
b.iter(|| {
175+
assert_eq!(
176+
dfs(Pt::new(2, 3), |p| successors_limit(p, 4), |_| false),
177+
None
178+
)
179+
});
180+
});
181+
}
182+
165183
fn no_path_dijkstra(c: &mut Criterion) {
166184
c.bench_function("fill-no_path_dijkstra", |b| {
167185
b.iter(|| {
@@ -207,6 +225,7 @@ criterion_group!(
207225
corner_to_corner_iddfs,
208226
no_path_astar,
209227
no_path_bfs,
228+
no_path_dfs_restricted,
210229
no_path_dijkstra,
211230
no_path_fringe,
212231
);

benches/algos.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ fn no_path_bfs(c: &mut Criterion) {
161161
});
162162
}
163163

164+
fn no_path_dfs(c: &mut Criterion) {
165+
c.bench_function("no_path_dfs", |b| {
166+
b.iter(|| assert_eq!(bfs(&Pt::new(2, 3), successors, |_| false), None));
167+
});
168+
}
169+
164170
fn no_path_dijkstra(c: &mut Criterion) {
165171
c.bench_function("no_path_dijkstra", |b| {
166172
b.iter(|| {
@@ -255,6 +261,7 @@ criterion_group!(
255261
corner_to_corner_iddfs,
256262
no_path_astar,
257263
no_path_bfs,
264+
no_path_dfs,
258265
no_path_dijkstra,
259266
no_path_fringe,
260267
bench_separate_components,

0 commit comments

Comments
 (0)