-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bench: Use a OnceCell to avoid recompiling the en_US dictionary
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
1 parent
0ac6818
commit ca5c504
Showing
3 changed files
with
35 additions
and
32 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ rust-version = "1.66" | |
[dependencies] | ||
spellbook = { path = "../" } | ||
brunch = "0.5" | ||
once_cell = "1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))), | ||
); |