Debian Codegraph @

helix-view/benches/word_index.rs

raw symbols SCIP
1 //! Benchmarks for the word index.
2
3 use std::hint::black_box;
4
5 use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
6 use helix_core::Rope;
7 use helix_view::handlers::word_index::bench;
8
9 /// See `texts/README`. These texts are used in unicode-segmentation's benchmarks.
10 const CORPORA: &[(&str, &str)] = &[
11 ("arabic", include_str!("texts/arabic.txt")),
12 ("english", include_str!("texts/english.txt")),
13 ("hindi", include_str!("texts/hindi.txt")),
14 ("japanese", include_str!("texts/japanese.txt")),
15 ("korean", include_str!("texts/korean.txt")),
16 ("mandarin", include_str!("texts/mandarin.txt")),
17 ("russian", include_str!("texts/russian.txt")),
18 ("source_code", include_str!("texts/source_code.txt")),
19 ];
20
21 /// Just extracting words from a rope (i.e. the hot path).
22 fn words(c: &mut Criterion) {
23 let mut group = c.benchmark_group("words");
24 for (name, text) in CORPORA {
25 let rope = Rope::from_str(text);
26 group.throughput(Throughput::Bytes(text.len() as u64));
27 group.bench_with_input(BenchmarkId::from_parameter(name), &rope, |b, rope| {
28 b.iter(|| {
29 let mut count = 0usize;
30 for word in bench::words(black_box(rope.slice(..))) {
31 black_box(word);
32 count += 1;
33 }
34 count
35 });
36 });
37 }
38 group.finish();
39 }
40
41 /// End-to-end extraction plus hashing / locking / inserting.
42 fn add_document(c: &mut Criterion) {
43 let mut group = c.benchmark_group("add_document");
44 for (name, text) in CORPORA {
45 let rope = Rope::from_str(text);
46 group.throughput(Throughput::Bytes(text.len() as u64));
47 group.bench_with_input(BenchmarkId::from_parameter(name), &rope, |b, rope| {
48 b.iter(|| {
49 let index = bench::WordIndex::default();
50 bench::add_document(&index, black_box(rope));
51 index
52 });
53 });
54 }
55 group.finish();
56 }
57
58 criterion_group!(benches, words, add_document);
59 criterion_main!(benches);