From 598e355476e19e96f7dd6dd1582d65b7fdc13221 Mon Sep 17 00:00:00 2001 From: cijiugechu Date: Wed, 30 Aug 2023 16:43:51 +0800 Subject: [PATCH] perf: reduce cloning operations --- Cargo.toml | 9 +++++++-- benches/parser.rs | 16 ++++++++++++++++ src/range.rs | 12 ++++++------ 3 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 benches/parser.rs diff --git a/Cargo.toml b/Cargo.toml index 48f8117..d597197 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/benches/parser.rs b/benches/parser.rs new file mode 100644 index 0000000..9fca22b --- /dev/null +++ b/benches/parser.rs @@ -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); diff --git a/src/range.rs b/src/range.rs index 75e9903..7297922 100644 --- a/src/range.rs +++ b/src/range.rs @@ -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, } } @@ -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, } } }