Skip to content

Commit

Permalink
Add benchmarks.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuroitaki committed Jan 16, 2025
1 parent 15b951c commit 78d44eb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ name = "utils"
serde = { workspace = true, optional = true, features = ["derive"] }

[dev-dependencies]
criterion = { version = "0.5" }
rstest = "0.12"
rand = { workspace = true }
itertools = "0.11.0"


[[bench]]
name = "range"
harness = false
41 changes: 41 additions & 0 deletions utils/benches/range.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

use utils::range::{RangeSet, Subset};

pub fn criterion_benchmark(c: &mut Criterion) {
let mut rangeset_vec = Vec::new();
for i in 0..10000 {
if i % 2 == 0 {
// [0..1, 2..3, ... , 9998..9999].
rangeset_vec.push(i..i + 1);
}
}
let rangeset = RangeSet::from(rangeset_vec);

c.bench_function("boundary_range_subset_of_rangeset", |b| {
b.iter(|| boundary_range_subset_of_rangeset(black_box(&rangeset)))
});

c.bench_function("rangeset_subset_of_boundary_rangeset", |b| {
b.iter(|| rangeset_subset_of_boundary_rangeset(black_box(&rangeset)))
});
}

// To benchmark the worst case where [range.start] is close to [other.end()], i.e. N
// iterations are needed if there is no boundary short citcuit (N == other.len_ranges()).
fn boundary_range_subset_of_rangeset(other: &RangeSet<u32>) {
let range = 9997..10005;
let _ = range.is_subset(other);
}

// To benchmark the worst case where [rangeset.last().start] is close to [other.end()],
// i.e. N iterations of [is_subset()] check are needed if there is no boundary short
// citcuit (N ~= rangeset.len_ranges()).
#[allow(clippy::single_range_in_vec_init)]
fn rangeset_subset_of_boundary_rangeset(rangeset: &RangeSet<u32>) {
let other = RangeSet::from(vec![0..9998]);
let _ = rangeset.is_subset(&other);
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
2 changes: 2 additions & 0 deletions utils/src/range/subset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ impl<T: Copy + Ord> Subset<RangeSet<T>> for Range<T> {
return false;
}

// Boundary short circuit.
if self.start < other.min().unwrap() || self.end > other.end().unwrap() {
// Check if self's start & end are contained within (or same as) other's.
return false;
Expand Down Expand Up @@ -62,6 +63,7 @@ impl<T: Copy + Ord> Subset<RangeSet<T>> for RangeSet<T> {
return false;
}

// Boundary short circuit.
if self.min().unwrap() < other.min().unwrap() || self.end().unwrap() > other.end().unwrap()
{
// Check if self's start & end are contained within (or same as) other's.
Expand Down

0 comments on commit 78d44eb

Please sign in to comment.