Skip to content

Commit

Permalink
Add bench for backtracking
Browse files Browse the repository at this point in the history
  • Loading branch information
x-hgg-x committed Nov 10, 2024
1 parent 1f3baca commit c6db4bc
Show file tree
Hide file tree
Showing 8 changed files with 49,243 additions and 4 deletions.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ include = ["Cargo.toml", "LICENSE", "README.md", "src/**", "tests/**", "examples

[dependencies]
indexmap = "2.6.0"
log = "0.4.22" # for debug logs in tests
# for debug logs in tests
log = "0.4.22"
priority-queue = "2.1.1"
rustc-hash = ">=1.0.0, <3.0.0"
serde = { version = "1.0", features = ["derive"], optional = true }
Expand All @@ -42,6 +43,10 @@ version-ranges = { version = "0.1.0", path = "version-ranges", features = ["prop
[features]
serde = ["dep:serde", "version-ranges/serde"]

[[bench]]
name = "backtracking"
harness = false

[[bench]]
name = "large_case"
harness = false
Expand Down
56 changes: 56 additions & 0 deletions benches/backtracking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: MPL-2.0

use std::collections::BTreeMap;
use std::time::Duration;

use criterion::*;
use pubgrub::OfflineDependencyProvider;
use version_ranges::Ranges;

type Deps = BTreeMap<u32, BTreeMap<u8, BTreeMap<u32, Vec<(u8, u8)>>>>;

fn bench(b: &mut Bencher, data: &str) {
let dependencies = ron::de::from_str::<Deps>(data).unwrap();

let mut dependency_provider = OfflineDependencyProvider::<u32, Ranges<u8>>::new();

for (&p, vmap) in &dependencies {
for (&v, dmap) in vmap {
let deps_iter = dmap.iter().map(|(&d, intervals)| {
let mut r = Ranges::empty();
for &(start, end) in intervals {
r = r.r#union(&Ranges::from_range_bounds(start..=end));
}
(d, r)
});
dependency_provider.add_dependencies(p, v, deps_iter);
}
}

if let Some((p, v)) = dependencies
.first_key_value()
.and_then(|(&p, vmap)| vmap.first_key_value().map(|(&v, _)| (p, v)))
{
b.iter(|| {
let _ = pubgrub::resolve(&dependency_provider, p, v);
});
}
}

fn bench_group(c: &mut Criterion) {
let mut group = c.benchmark_group("bench");
group.measurement_time(Duration::from_secs(20));

for case in ["small", "medium", "large"] {
let name = format!("backtracking_{case}");
let data = std::fs::read_to_string(format!("test-examples/{name}.ron")).unwrap();
group.bench_function(name, |b| {
bench(b, &data);
});
}

group.finish();
}

criterion_group!(benches, bench_group);
criterion_main!(benches);
1 change: 1 addition & 0 deletions benches/large_case.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// SPDX-License-Identifier: MPL-2.0

use std::time::Duration;

use criterion::*;
Expand Down
4 changes: 2 additions & 2 deletions benches/sudoku.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ fn from_board(b: &str) -> Vec<(SudokuPackage, Range<Arc<usize>>)> {
if let Some(val) = val.chars().next().unwrap().to_digit(10) {
out.push((
SudokuPackage::Cell {
row: (row + 1).try_into().unwrap(),
col: (col + 1).try_into().unwrap(),
row: row + 1,
col: col + 1,
},
Range::singleton(val as usize),
));
Expand Down
Loading

0 comments on commit c6db4bc

Please sign in to comment.