Skip to content

Commit

Permalink
perf: boxing BoundSet (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
cijiugechu authored Nov 5, 2024
1 parent 9bc9f2c commit fb94ab7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
7 changes: 7 additions & 0 deletions benches/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ pub fn criterion_benchmark(c: &mut Criterion) {
let _r = range.satisfies(black_box(&version));
})
});

c.bench_function("min version in range", |b| {
b.iter(|| {
let range = Range::parse(black_box(">=1.2.3-rc.4")).unwrap();
let _version = range.min_version().unwrap();
})
});
}

criterion_group!(bench, criterion_benchmark);
Expand Down
36 changes: 20 additions & 16 deletions src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use crate::{

#[derive(Clone, Debug, Eq, PartialEq, Hash)]
struct BoundSet {
upper: Bound,
lower: Bound,
upper: Box<Bound>,
lower: Box<Bound>,
}

impl BoundSet {
Expand All @@ -36,10 +36,13 @@ impl BoundSet {
None
}
(Lower(Including(v1)), Upper(Including(v2))) if v1 == v2 => Some(Self {
lower: Lower(Including(v1)),
upper: Upper(Including(v2)),
lower: Box::new(Lower(Including(v1))),
upper: Box::new(Upper(Including(v2))),
}),
(lower, upper) if lower < upper => Some(Self {
lower: Box::new(lower),
upper: Box::new(upper),
}),
(lower, upper) if lower < upper => Some(Self { lower, upper }),
_ => None,
}
}
Expand All @@ -63,7 +66,7 @@ impl BoundSet {
use Bound::*;
use Predicate::*;

let lower_bound = match &self.lower {
let lower_bound = match &self.lower.as_ref() {
Lower(Including(lower)) => lower <= version,
Lower(Excluding(lower)) => lower < version,
Lower(Unbounded) => true,
Expand All @@ -73,7 +76,7 @@ impl BoundSet {
),
};

let upper_bound = match &self.upper {
let upper_bound = match &self.upper.as_ref() {
Upper(Including(upper)) => version <= upper,
Upper(Excluding(upper)) => version < upper,
Upper(Unbounded) => true,
Expand All @@ -88,7 +91,7 @@ impl BoundSet {
}

if version.is_prerelease() {
let lower_version = match &self.lower {
let lower_version = match &self.lower.as_ref() {
Lower(Including(v)) => Some(v),
Lower(Excluding(v)) => Some(v),
_ => None,
Expand All @@ -103,7 +106,7 @@ impl BoundSet {
}
}

let upper_version = match &self.upper {
let upper_version = match &self.upper.as_ref() {
Upper(Including(v)) => Some(v),
Upper(Excluding(v)) => Some(v),
_ => None,
Expand Down Expand Up @@ -141,8 +144,8 @@ impl BoundSet {
}

fn intersect(&self, other: &Self) -> Option<Self> {
let lower = std::cmp::max(&self.lower, &other.lower);
let upper = std::cmp::min(&self.upper, &other.upper);
let lower: &Bound = std::cmp::max(&self.lower, &other.lower);
let upper: &Bound = std::cmp::min(&self.upper, &other.upper);

BoundSet::new(lower.clone(), upper.clone())
}
Expand All @@ -157,19 +160,19 @@ impl BoundSet {

if self.lower < overlap.lower && overlap.upper < self.upper {
return Some(vec![
BoundSet::new(self.lower.clone(), Upper(overlap.lower.predicate().flip()))
BoundSet::new(*self.lower.clone(), Upper(overlap.lower.predicate().flip()))
.unwrap(),
BoundSet::new(Lower(overlap.upper.predicate().flip()), self.upper.clone())
BoundSet::new(Lower(overlap.upper.predicate().flip()), *self.upper.clone())
.unwrap(),
]);
}

if self.lower < overlap.lower {
return BoundSet::new(self.lower.clone(), Upper(overlap.lower.predicate().flip()))
return BoundSet::new(*self.lower.clone(), Upper(overlap.lower.predicate().flip()))
.map(|f| vec![f]);
}

BoundSet::new(Lower(overlap.upper.predicate().flip()), self.upper.clone())
BoundSet::new(Lower(overlap.upper.predicate().flip()), *self.upper.clone())
.map(|f| vec![f])
} else {
Some(vec![self.clone()])
Expand All @@ -181,7 +184,7 @@ impl fmt::Display for BoundSet {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use Bound::*;
use Predicate::*;
match (&self.lower, &self.upper) {
match (&self.lower.as_ref(), &self.upper.as_ref()) {
(Lower(Unbounded), Upper(Unbounded)) => write!(f, "*"),
(Lower(Unbounded), Upper(Including(v))) => write!(f, "<={}", v),
(Lower(Unbounded), Upper(Excluding(v))) => write!(f, "<{}", v),
Expand Down Expand Up @@ -502,6 +505,7 @@ impl Range {
*/
pub fn min_version(&self) -> Option<Version> {
if let Some(min_bound) = self.0.iter().map(|range| &range.lower).min() {
let min_bound = min_bound.as_ref();
match min_bound {
Bound::Lower(pred) => match pred {
Predicate::Including(v) => Some(v.clone()),
Expand Down

0 comments on commit fb94ab7

Please sign in to comment.