Skip to content

Commit

Permalink
perf: reduce cloning operations
Browse files Browse the repository at this point in the history
  • Loading branch information
cijiugechu committed Aug 30, 2023
1 parent f401e32 commit 598e355
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@ serde = "1.0.126"

[dev-dependencies]
pretty_assertions = "0.6.1"
serde_derive = "1.0.115"
serde_json = "1.0.57"
serde_derive = "1.0.115"
serde_json = "1.0.57"
criterion = { version = "0.5.1", features = ["html_reports"] }

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

pub fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("range", |b| {
b.iter(|| {
let range = Range::parse(black_box(">=1.2.3-rc.4")).unwrap();
let version = Version::parse(black_box("1.2.3")).unwrap();

let _r = range.satisfies(black_box(&version));
})
});
}

criterion_group!(bench, criterion_benchmark);
criterion_main!(bench);
12 changes: 6 additions & 6 deletions src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,11 @@ enum Predicate {
}

impl Predicate {
fn flip(&self) -> Self {
fn flip(self) -> Self {
use Predicate::*;
match self {
Excluding(v) => Including(v.clone()),
Including(v) => Excluding(v.clone()),
Excluding(v) => Including(v),
Including(v) => Excluding(v),
Unbounded => Unbounded,
}
}
Expand All @@ -239,12 +239,12 @@ impl Bound {
Bound::Lower(Predicate::Unbounded)
}

fn predicate(&self) -> Predicate {
fn predicate(self) -> Predicate {
use Bound::*;

match self {
Lower(p) => p.clone(),
Upper(p) => p.clone(),
Lower(p) => p,
Upper(p) => p,
}
}
}
Expand Down

0 comments on commit 598e355

Please sign in to comment.