Debian Codegraph @

third_party/rust/mapped_hyph/benches/bench.rs

raw
1 // Any copyright to the test code below is dedicated to the Public Domain.
2 // http://creativecommons.org/publicdomain/zero/1.0/
3
4 use criterion::black_box;
5 use criterion::criterion_group;
6 use criterion::criterion_main;
7 use criterion::BenchmarkId;
8 use criterion::Criterion;
9
10 use mapped_hyph::Hyphenator;
11 use std::fs;
12
13 const SAMPLE_SIZE: usize = 300;
14 const DIC_PATH: &str = "hyph_en_US.hyf";
15
16 fn bench_construct(c: &mut Criterion) {
17 c.bench_function("construct", |b| {
18 b.iter(|| {
19 let dic = unsafe { mapped_hyph::load_file(DIC_PATH) }
20 .expect(&format!("failed to load dictionary {}", DIC_PATH));
21 let _ = Hyphenator::new(black_box(&*dic));
22 })
23 });
24 }
25
26 fn bench_find_hyphen_values(c: &mut Criterion) {
27 // XXX: Should we copy this file to the crate to ensure reproducability?
28 let data = fs::read_to_string("/usr/share/dict/words").expect("File reading failed.");
29 let words: Vec<&str> = data.lines().take(SAMPLE_SIZE).collect();
30
31 let dic = unsafe { mapped_hyph::load_file(DIC_PATH) }
32 .expect(&format!("failed to load dictionary {}", DIC_PATH));
33 let hyph = Hyphenator::new(&*dic);
34
35 c.bench_with_input(
36 BenchmarkId::new("bench_word", SAMPLE_SIZE),
37 &words,
38 |b, words| {
39 b.iter(|| {
40 let mut values: Vec<u8> = vec![0; 1000];
41 for w in words {
42 hyph.find_hyphen_values(&w, &mut values);
43 }
44 });
45 },
46 );
47 }
48
49 criterion_group!(benches, bench_construct, bench_find_hyphen_values,);
50 criterion_main!(benches);