From 47872c19ba571f1f7e2f154505f902a84de1cff7 Mon Sep 17 00:00:00 2001 From: Pavel Ivanov Date: Fri, 1 Mar 2024 21:33:53 +0100 Subject: [PATCH] fix: fixed wildcard and string matching benchmarks (#140) --- Cargo.lock | 69 +++++++++++++++++++++++++++++++++++++- Cargo.toml | 13 ++++++-- benches/string.rs | 50 +++++++++++++++++++++++++++ benches/wildcard.rs | 78 ------------------------------------------- benches/wildflower.rs | 72 +++++++++++++++++++++++++++++++++++++++ benches/wildmatch.rs | 72 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 273 insertions(+), 81 deletions(-) create mode 100644 benches/string.rs delete mode 100644 benches/wildcard.rs create mode 100644 benches/wildflower.rs create mode 100644 benches/wildmatch.rs diff --git a/Cargo.lock b/Cargo.lock index 2a82c594..cc8cbf91 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -738,7 +738,7 @@ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" [[package]] name = "hl" -version = "0.25.3-alpha.5" +version = "0.25.3" dependencies = [ "atoi", "bincode", @@ -786,6 +786,7 @@ dependencies = [ "snap", "stats_alloc", "thiserror", + "wildflower", "wildmatch", "winapi", "wyhash", @@ -1555,6 +1556,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "synstructure" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.50", +] + [[package]] name = "terminal_size" version = "0.3.0" @@ -1760,6 +1772,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "wildflower" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4138ac28620aa128f691374fdcd36fe5cfc51346f22c51be13887df1146e156e" +dependencies = [ + "stable_deref_trait", + "yoke", +] + [[package]] name = "wildmatch" version = "2.3.0" @@ -1964,3 +1986,48 @@ checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" dependencies = [ "linked-hash-map", ] + +[[package]] +name = "yoke" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65e71b2e4f287f467794c671e2b8f8a5f3716b3c829079a1c44740148eff07e4" +dependencies = [ + "serde", + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e6936f0cce458098a201c245a11bef556c6a0181129c7034d10d76d1ec3a2b8" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.50", + "synstructure", +] + +[[package]] +name = "zerofrom" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "655b0814c5c0b19ade497851070c640773304939a6c0fd5f5fb43da0696d05b7" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6a647510471d372f2e6c2e6b7219e44d8c574d24fdc11c610a61455782f18c3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.50", + "synstructure", +] diff --git a/Cargo.toml b/Cargo.toml index cc4e0276..9127921e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ categories = ["command-line-utilities"] description = "Utility for viewing json-formatted log files." keywords = ["cli", "human", "log"] name = "hl" -version = "0.25.3-alpha.5" +version = "0.25.3" edition = "2021" build = "build.rs" @@ -71,6 +71,7 @@ criterion = "0" stats_alloc = "0" regex = "1" wildmatch = "2" +wildflower = "0" [profile.release] debug = false @@ -91,7 +92,11 @@ name = "ts-format" harness = false [[bench]] -name = "wildcard" +name = "wildmatch" +harness = false + +[[bench]] +name = "wildflower" harness = false [[bench]] @@ -102,6 +107,10 @@ harness = false name = "parse-and-format" harness = false +[[bench]] +name = "string" +harness = false + [[bench]] name = "json" harness = false diff --git a/benches/string.rs b/benches/string.rs new file mode 100644 index 00000000..6fa8e22f --- /dev/null +++ b/benches/string.rs @@ -0,0 +1,50 @@ +// std imports +use std::alloc::System; + +// third-party imports +use criterion::{criterion_group, criterion_main, Criterion}; +use stats_alloc::{StatsAlloc, INSTRUMENTED_SYSTEM}; +use std::hint::black_box; + +#[global_allocator] +static GLOBAL: &StatsAlloc = &INSTRUMENTED_SYSTEM; + +fn benchmark(c: &mut Criterion) { + let mut c = c.benchmark_group("string"); + let prefix = String::from("_"); + + c.bench_function("string-short-match", |b| { + let what = String::from("_TEST"); + b.iter(|| { + assert_eq!(black_box(&what).starts_with(black_box(&prefix)), true); + }); + }); + c.bench_function("string-long-match", |b| { + let what = String::from("_TEST_SOME_VERY_VERY_LONG_NAME"); + b.iter(|| { + assert_eq!(black_box(&what).starts_with(black_box(&prefix)), true); + }); + }); + c.bench_function("string-short-non-match", |b| { + let what = String::from("TEST"); + b.iter(|| { + assert_eq!(black_box(&what).starts_with(black_box(&prefix)), false); + }); + }); + c.bench_function("string-long-non-match", |b| { + let what = String::from("TEST_SOME_VERY_VERY_LONG_NAME"); + b.iter(|| { + assert_eq!(black_box(&what).starts_with(black_box(&prefix)), false); + }); + }); + c.bench_function("string-long-prefix-match", |b| { + let prefix = String::from("TEST_SOME_VERY_VERY_LONG_PREFIX_"); + let what = String::from("TEST_SOME_VERY_VERY_LONG_PREFIX_AND_SOMEWHAT"); + b.iter(|| { + assert_eq!(black_box(&what).starts_with(black_box(&prefix)), true); + }); + }); +} + +criterion_group!(benches, benchmark); +criterion_main!(benches); diff --git a/benches/wildcard.rs b/benches/wildcard.rs deleted file mode 100644 index f67c128d..00000000 --- a/benches/wildcard.rs +++ /dev/null @@ -1,78 +0,0 @@ -// std imports -use std::alloc::System; - -// third-party imports -use criterion::{criterion_group, criterion_main, Criterion}; -use stats_alloc::{Region, StatsAlloc, INSTRUMENTED_SYSTEM}; -use wildmatch::WildMatch; - -#[global_allocator] -static GLOBAL: &StatsAlloc = &INSTRUMENTED_SYSTEM; - -fn benchmark(c: &mut Criterion) { - let mut c = c.benchmark_group("wildcard"); - let pattern = WildMatch::new(r"_*"); - let prefix = String::from("_"); - - let mut c1 = None; - let mut n1 = 0; - c.bench_function("wild-short-match", |b| { - let reg = Region::new(&GLOBAL); - b.iter(|| { - assert_eq!(pattern.matches("_TEST"), true); - n1 += 1; - }); - c1 = Some(reg.change()); - }); - println!("allocations at 1 ({:?} iterations): {:#?}", n1, c1); - - let mut c2 = None; - let mut n2 = 0; - c.bench_function("wild-long-match", |b| { - let reg = Region::new(&GLOBAL); - b.iter(|| { - assert_eq!(pattern.matches("_TEST_SOME_VERY_VERY_LONG_NAME"), true); - n2 += 1; - }); - c2 = Some(reg.change()); - }); - println!("allocations at 2 ({:?} iterations): {:#?}", n2, c2); - - c.bench_function("wild-short-non-match", |b| { - b.iter(|| { - assert_eq!(pattern.matches("TEST"), false); - }); - }); - c.bench_function("wild-long-non-match", |b| { - b.iter(|| { - assert_eq!(pattern.matches("TEST_SOME_VERY_VERY_LONG_NAME"), false); - }); - }); - c.bench_function("compare-short-match", |b| { - let what = String::from("_TEST"); - b.iter(|| { - assert_eq!(what.starts_with(&prefix), true); - }); - }); - c.bench_function("compare-long-match", |b| { - let what = String::from("_TEST_SOME_VERY_VERY_LONG_NAME"); - b.iter(|| { - assert_eq!(what.starts_with(&prefix), true); - }); - }); - c.bench_function("compare-short-non-match", |b| { - let what = String::from("TEST"); - b.iter(|| { - assert_eq!(what.starts_with(&prefix), false); - }); - }); - c.bench_function("compare-long-non-match", |b| { - let what = String::from("TEST_SOME_VERY_VERY_LONG_NAME"); - b.iter(|| { - assert_eq!(what.starts_with(&prefix), false); - }); - }); -} - -criterion_group!(benches, benchmark); -criterion_main!(benches); diff --git a/benches/wildflower.rs b/benches/wildflower.rs new file mode 100644 index 00000000..d61c5b5b --- /dev/null +++ b/benches/wildflower.rs @@ -0,0 +1,72 @@ +// std imports +use std::alloc::System; + +// third-party imports +use criterion::{criterion_group, criterion_main, Criterion}; +use stats_alloc::{Region, StatsAlloc, INSTRUMENTED_SYSTEM}; +use std::hint::black_box; +use wildflower::Pattern; + +#[global_allocator] +static GLOBAL: &StatsAlloc = &INSTRUMENTED_SYSTEM; + +fn benchmark(c: &mut Criterion) { + let mut c = c.benchmark_group("wildflower"); + let pattern = Pattern::new(r"_*"); + + let mut c1 = None; + let mut n1 = 0; + c.bench_function("wildflower-short-match", |b| { + let what = "_TEST"; + let reg = Region::new(&GLOBAL); + b.iter(|| { + assert_eq!(black_box(&pattern).matches(black_box(&what)), true); + n1 += 1; + }); + c1 = Some(reg.change()); + }); + println!("allocations at 1 ({:?} iterations): {:#?}", n1, c1); + + let mut c2 = None; + let mut n2 = 0; + c.bench_function("wildflower-long-match", |b| { + let what = "_TEST_SOME_VERY_VERY_LONG_NAME"; + let reg = Region::new(&GLOBAL); + b.iter(|| { + assert_eq!(black_box(&pattern).matches(black_box(&what)), true); + n2 += 1; + }); + c2 = Some(reg.change()); + }); + println!("allocations at 2 ({:?} iterations): {:#?}", n2, c2); + + let mut c2 = None; + let mut n2 = 0; + c.bench_function("wildflower-long-prefix-match", |b| { + let pattern = Pattern::new(r"SOME_VERY_VERY_LONG_PREFIX_*"); + let what = "SOME_VERY_VERY_LONG_PREFIX_AND_SOMEWHAT"; + let reg = Region::new(&GLOBAL); + b.iter(|| { + assert_eq!(black_box(&pattern).matches(black_box(&what)), true); + n2 += 1; + }); + c2 = Some(reg.change()); + }); + println!("allocations at 2 ({:?} iterations): {:#?}", n2, c2); + + c.bench_function("wildflower-short-non-match", |b| { + let what = "TEST"; + b.iter(|| { + assert_eq!(black_box(&pattern).matches(black_box(&what)), false); + }); + }); + c.bench_function("wildflower-long-non-match", |b| { + let what = "TEST_SOME_VERY_VERY_LONG_NAME"; + b.iter(|| { + assert_eq!(black_box(&pattern).matches(black_box(&what)), false); + }); + }); +} + +criterion_group!(benches, benchmark); +criterion_main!(benches); diff --git a/benches/wildmatch.rs b/benches/wildmatch.rs new file mode 100644 index 00000000..0c7f31c4 --- /dev/null +++ b/benches/wildmatch.rs @@ -0,0 +1,72 @@ +// std imports +use std::alloc::System; + +// third-party imports +use criterion::{criterion_group, criterion_main, Criterion}; +use stats_alloc::{Region, StatsAlloc, INSTRUMENTED_SYSTEM}; +use std::hint::black_box; +use wildmatch::WildMatch; + +#[global_allocator] +static GLOBAL: &StatsAlloc = &INSTRUMENTED_SYSTEM; + +fn benchmark(c: &mut Criterion) { + let mut c = c.benchmark_group("wildmatch"); + let pattern = WildMatch::new(r"_*"); + + let mut c1 = None; + let mut n1 = 0; + c.bench_function("wildmatch-short-match", |b| { + let what = "_TEST"; + let reg = Region::new(&GLOBAL); + b.iter(|| { + assert_eq!(black_box(&pattern).matches(black_box(&what)), true); + n1 += 1; + }); + c1 = Some(reg.change()); + }); + println!("allocations at 1 ({:?} iterations): {:#?}", n1, c1); + + let mut c2 = None; + let mut n2 = 0; + c.bench_function("wildmatch-long-match", |b| { + let what = "_TEST_SOME_VERY_VERY_LONG_NAME"; + let reg = Region::new(&GLOBAL); + b.iter(|| { + assert_eq!(black_box(&pattern).matches(black_box(&what)), true); + n2 += 1; + }); + c2 = Some(reg.change()); + }); + println!("allocations at 2 ({:?} iterations): {:#?}", n2, c2); + + let mut c2 = None; + let mut n2 = 0; + c.bench_function("wildmatch-long-prefix-match", |b| { + let pattern = WildMatch::new(r"SOME_VERY_VERY_LONG_PREFIX_*"); + let what = "SOME_VERY_VERY_LONG_PREFIX_AND_SOMEWHAT"; + let reg = Region::new(&GLOBAL); + b.iter(|| { + assert_eq!(black_box(&pattern).matches(black_box(&what)), true); + n2 += 1; + }); + c2 = Some(reg.change()); + }); + println!("allocations at 2 ({:?} iterations): {:#?}", n2, c2); + + c.bench_function("wildmatch-short-non-match", |b| { + let what = "TEST"; + b.iter(|| { + assert_eq!(black_box(&pattern).matches(black_box(&what)), false); + }); + }); + c.bench_function("wildmatch-long-non-match", |b| { + let what = "TEST_SOME_VERY_VERY_LONG_NAME"; + b.iter(|| { + assert_eq!(black_box(&pattern).matches(black_box(&what)), false); + }); + }); +} + +criterion_group!(benches, benchmark); +criterion_main!(benches);