Debian Codegraph @

third_party/rust/icu_collections/benches/codepointtrie.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 use criterion::{black_box, criterion_group, criterion_main, Criterion};
6
7 use icu_collections::codepointtrie::CodePointTrie;
8
9 #[path = "tries/mod.rs"]
10 mod tries;
11
12 #[cfg(feature = "bench")]
13 mod sample_str_lng {
14 // "eng" is all ASCII
15 pub const ENG: &str = "Universal Declaration of Human Rights";
16 // "pcd" has ASCII mixed with low-BMP code points
17 pub const PCD: &str = "Dèclaråcion dès dreûts d' l'ome po tos lès payîs dè monde";
18 // "ukr" uses low-BMP code points (less than U+1000)
19 pub const UKR: &str = "ЗАГАЛЬНА ДЕКЛАРАЦІЯ ПРАВ ЛЮДИНІ";
20 // "yue" uses high-BMP code points (greater than U+1000), so it benefits from fast mode
21 pub const YUE: &str = "世界人权宣言";
22 // "ccp" exercises supplementary code points
23 pub const CCP: &str = "𑄟𑄚𑄬𑄭𑄃𑄇𑄴𑄇𑄥𑄧𑄁𑄢𑄴 𑄝𑄬𑄇𑄴𑄅𑄚𑄮𑄢𑄴 𑄟𑄧𑄚𑄳𑄢𑄧𑄧𑄇𑄉𑄮𑄌𑄴";
24 }
25
26 const SAMPLE_STRING_MIXED: &str = "Dèclaråcion ЗАГАЛЬНА 世界人权宣言 𑄟𑄚𑄬𑄭𑄃𑄇𑄴𑄇𑄥𑄧𑄁𑄢𑄴";
27
28 /// A function that returns 100 code points in the desired language
29 fn one_hundred_code_points(sample_str: &str) -> String {
30 sample_str.chars().cycle().take(100).collect()
31 }
32
33 fn get_trie_small() -> CodePointTrie<'static, u8> {
34 CodePointTrie::try_new(
35 tries::gc_small::HEADER,
36 tries::gc_small::INDEX,
37 tries::gc_small::DATA,
38 )
39 .unwrap()
40 }
41
42 #[cfg(feature = "bench")]
43 fn get_trie_fast() -> CodePointTrie<'static, u8> {
44 CodePointTrie::try_new(
45 tries::gc_fast::HEADER,
46 tries::gc_fast::INDEX,
47 tries::gc_fast::DATA,
48 )
49 .unwrap()
50 }
51
52 fn overview_bench(c: &mut Criterion) {
53 let s = one_hundred_code_points(SAMPLE_STRING_MIXED);
54 let cpt_small = get_trie_small();
55
56 c.bench_function("cpt/overview", |b| {
57 b.iter(|| {
58 black_box(&s)
59 .chars()
60 .map(|c| black_box(&cpt_small).get32(c as u32))
61 .reduce(|a, b| a.wrapping_add(b))
62 });
63 });
64
65 #[cfg(feature = "bench")]
66 c.bench_function("cpt/get_range", |b| {
67 b.iter(|| {
68 black_box(&s)
69 .chars()
70 .map(|c| black_box(&cpt_small).get_range(c as u32).unwrap())
71 .fold(0u32, |acc, ele| {
72 acc.wrapping_add(ele.range.end() - ele.range.start() + ele.value as u32)
73 })
74 });
75 });
76
77 #[cfg(feature = "bench")]
78 {
79 let cpt_fast = get_trie_fast();
80 lang_bench(c, &cpt_small, "small/eng", sample_str_lng::ENG);
81 lang_bench(c, &cpt_small, "small/pcd", sample_str_lng::PCD);
82 lang_bench(c, &cpt_small, "small/ukr", sample_str_lng::UKR);
83 lang_bench(c, &cpt_small, "small/yue", sample_str_lng::YUE);
84 lang_bench(c, &cpt_small, "small/ccp", sample_str_lng::CCP);
85 lang_bench(c, &cpt_fast, "fast/eng", sample_str_lng::ENG);
86 lang_bench(c, &cpt_fast, "fast/pcd", sample_str_lng::PCD);
87 lang_bench(c, &cpt_fast, "fast/ukr", sample_str_lng::UKR);
88 lang_bench(c, &cpt_fast, "fast/yue", sample_str_lng::YUE);
89 lang_bench(c, &cpt_fast, "fast/ccp", sample_str_lng::CCP);
90 }
91 }
92
93 #[cfg(feature = "bench")]
94 fn lang_bench(c: &mut Criterion, cpt: &CodePointTrie<u8>, lid: &str, sample_str: &str) {
95 let bench_name = format!("cpt/get/{lid}");
96 let s = one_hundred_code_points(sample_str);
97
98 c.bench_function(&bench_name, |b| {
99 b.iter(|| {
100 black_box(&s)
101 .chars()
102 .map(|c| black_box(&cpt).get32(c as u32))
103 .reduce(|a, b| a.wrapping_add(b))
104 });
105 });
106 }
107
108 criterion_group!(benches, overview_bench,);
109 criterion_main!(benches);