Skip to content

Commit

Permalink
bench: Use a OnceCell to avoid recompiling the en_US dictionary
Browse files Browse the repository at this point in the history
Now that the Dictionary is Sync (see the parent commit), we can use
brunch's default benches macro to improve the bench output.
  • Loading branch information
the-mikedavis committed Aug 26, 2023
1 parent 0ac6818 commit ca5c504
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
7 changes: 7 additions & 0 deletions bench/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ rust-version = "1.66"
[dependencies]
spellbook = { path = "../" }
brunch = "0.5"
once_cell = "1"
59 changes: 27 additions & 32 deletions bench/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
use std::hint::black_box;

use brunch::Bench;
use once_cell::sync::OnceCell;
use spellbook::Dictionary;

const EN_US_DIC: &str = include_str!("../../vendor/en_US/en_US.dic");
const EN_US_AFF: &str = include_str!("../../vendor/en_US/en_US.aff");

const SAMPLES: u32 = 500_000;

fn main() {
let dict = Dictionary::compile(EN_US_AFF, EN_US_DIC).unwrap();

eprintln!("Starting benchmarks...");
eprintln!();
let now = std::time::Instant::now();
brunch::benches!(
inline:

// Compilation
Bench::new("Compile en_US")
.run(|| Dictionary::compile(black_box(EN_US_AFF), black_box(EN_US_DIC))),
Bench::spacer(),

// Checking
Bench::new("In-dictionary word (\"drink\")")
.with_samples(SAMPLES)
.run(|| dict.check(black_box("drink"))),
Bench::new("Word with a suffix (\"drinkable\")")
.with_samples(SAMPLES)
.run(|| dict.check(black_box("drinkable"))),
Bench::new("Multi-affix (\"undrinkable\")")
.with_samples(SAMPLES)
.run(|| dict.check(black_box("undrinkable"))),
Bench::new("Incorrect prefix (\"undrink\")")
.with_samples(SAMPLES)
.run(|| dict.check(black_box("undrink"))),
Bench::new("Breaks (\"light-weight-like\")")
.with_samples(SAMPLES)
.run(|| dict.check(black_box("light-weight-like"))),
);
eprintln!("Finished in {:.1}s", now.elapsed().as_secs_f64());
fn en_us() -> &'static Dictionary {
static EN_US: OnceCell<Dictionary> = OnceCell::new();
EN_US.get_or_init(|| Dictionary::compile(EN_US_AFF, EN_US_DIC).unwrap())
}

brunch::benches!(
// Compilation
Bench::new("Compile en_US")
.run(|| Dictionary::compile(black_box(EN_US_AFF), black_box(EN_US_DIC))),
Bench::spacer(),
// Checking
Bench::new("In-dictionary word (\"drink\")")
.with_samples(SAMPLES)
.run_seeded_with(en_us, |dict| dict.check(black_box("drink"))),
Bench::new("Word with a suffix (\"drinkable\")")
.with_samples(SAMPLES)
.run_seeded_with(en_us, |dict| dict.check(black_box("drinkable"))),
Bench::new("Multi-affix (\"undrinkable\")")
.with_samples(SAMPLES)
.run_seeded_with(en_us, |dict| dict.check(black_box("undrinkable"))),
Bench::new("Incorrect prefix (\"undrink\")")
.with_samples(SAMPLES)
.run_seeded_with(en_us, |dict| dict.check(black_box("undrink"))),
Bench::new("Breaks (\"light-weight-like\")")
.with_samples(SAMPLES)
.run_seeded_with(en_us, |dict| dict.check(black_box("light-weight-like"))),
);

0 comments on commit ca5c504

Please sign in to comment.