Debian Codegraph @

debian/missing-sources/icu_normalizer/benches/decomposing_normalizer_nfkd.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, BenchmarkId, Criterion};
6
7 use icu_normalizer::{ComposingNormalizerBorrowed, DecomposingNormalizerBorrowed};
8
9 struct BenchDataContent {
10 pub file_name: String,
11 pub nfc: String,
12 pub nfd: String,
13 pub nfkc: String,
14 pub nfkd: String,
15 pub nfc_u16: Vec<u16>,
16 pub nfd_u16: Vec<u16>,
17 pub nfkc_u16: Vec<u16>,
18 pub nfkd_u16: Vec<u16>,
19 }
20
21 fn strip_headers(content: &str) -> String {
22 content
23 .lines()
24 .filter(|&s| !s.starts_with('#'))
25 .map(|s| s.to_owned())
26 .collect::<Vec<String>>()
27 .join("\n")
28 }
29
30 fn normalizer_bench_data() -> [BenchDataContent; 15] {
31 let nfc_normalizer = ComposingNormalizerBorrowed::new_nfc();
32 let nfd_normalizer = DecomposingNormalizerBorrowed::new_nfd();
33 let nfkc_normalizer = ComposingNormalizerBorrowed::new_nfkc();
34 let nfkd_normalizer = DecomposingNormalizerBorrowed::new_nfkd();
35
36 let content_latin: (&str, &str) = (
37 "TestNames_Latin",
38 &strip_headers(include_str!("./data/TestNames_Latin.txt")),
39 );
40 let content_jp_h: (&str, &str) = (
41 "TestNames_Japanese_h",
42 &strip_headers(include_str!("./data/TestNames_Japanese_h.txt")),
43 );
44 let content_jp_k: (&str, &str) = (
45 "TestNames_Japanese_k",
46 &strip_headers(include_str!("./data/TestNames_Japanese_k.txt")),
47 );
48 let content_korean: (&str, &str) = (
49 "TestNames_Korean",
50 &strip_headers(include_str!("./data/TestNames_Korean.txt")),
51 );
52 let content_random_words_ar: (&str, &str) = (
53 "TestRandomWordsUDHR_ar",
54 &strip_headers(include_str!("./data/TestRandomWordsUDHR_ar.txt")),
55 );
56 let content_random_words_de: (&str, &str) = (
57 "TestRandomWordsUDHR_de",
58 &strip_headers(include_str!("./data/TestRandomWordsUDHR_de.txt")),
59 );
60 let content_random_words_el: (&str, &str) = (
61 "TestRandomWordsUDHR_el",
62 &strip_headers(include_str!("./data/TestRandomWordsUDHR_el.txt")),
63 );
64 let content_random_words_es: (&str, &str) = (
65 "TestRandomWordsUDHR_es",
66 &strip_headers(include_str!("./data/TestRandomWordsUDHR_es.txt")),
67 );
68 let content_random_words_fr: (&str, &str) = (
69 "TestRandomWordsUDHR_fr",
70 &strip_headers(include_str!("./data/TestRandomWordsUDHR_fr.txt")),
71 );
72 let content_random_words_he: (&str, &str) = (
73 "TestRandomWordsUDHR_he",
74 &strip_headers(include_str!("./data/TestRandomWordsUDHR_he.txt")),
75 );
76 let content_random_words_pl: (&str, &str) = (
77 "TestRandomWordsUDHR_pl",
78 &strip_headers(include_str!("./data/TestRandomWordsUDHR_pl.txt")),
79 );
80 let content_random_words_ru: (&str, &str) = (
81 "TestRandomWordsUDHR_ru",
82 &strip_headers(include_str!("./data/TestRandomWordsUDHR_ru.txt")),
83 );
84 let content_random_words_th: (&str, &str) = (
85 "TestRandomWordsUDHR_th",
86 &strip_headers(include_str!("./data/TestRandomWordsUDHR_th.txt")),
87 );
88 let content_random_words_tr: (&str, &str) = (
89 "TestRandomWordsUDHR_tr",
90 &strip_headers(include_str!("./data/TestRandomWordsUDHR_tr.txt")),
91 );
92 let content_viet: (&str, &str) = ("wotw", &strip_headers(include_str!("./data/wotw.txt")));
93
94 [
95 content_latin,
96 content_viet,
97 content_jp_k,
98 content_jp_h,
99 content_korean,
100 content_random_words_ru,
101 content_random_words_ar,
102 content_random_words_el,
103 content_random_words_es,
104 content_random_words_fr,
105 content_random_words_tr,
106 content_random_words_th,
107 content_random_words_pl,
108 content_random_words_he,
109 content_random_words_de,
110 ]
111 .map(|(file_name, raw_content)| {
112 let nfc = &nfc_normalizer.normalize(raw_content);
113 let nfd = &nfd_normalizer.normalize(raw_content);
114 let nfkc = &nfkc_normalizer.normalize(raw_content);
115 let nfkd = &nfkd_normalizer.normalize(raw_content);
116 BenchDataContent {
117 file_name: file_name.to_owned(),
118 nfc: nfc.to_string(),
119 nfd: nfd.to_string(),
120 nfkc: nfkc.to_string(),
121 nfkd: nfkd.to_string(),
122 nfc_u16: nfc.encode_utf16().collect(),
123 nfd_u16: nfd.encode_utf16().collect(),
124 nfkc_u16: nfkc.encode_utf16().collect(),
125 nfkd_u16: nfkd.encode_utf16().collect(),
126 }
127 })
128 }
129
130 fn function_under_bench(normalizer: &DecomposingNormalizerBorrowed, text: &str) {
131 normalizer.normalize(text);
132 }
133
134 fn function_under_bench_u16(normalizer: &DecomposingNormalizerBorrowed, text: &[u16]) {
135 normalizer.normalize_utf16(text);
136 }
137
138 pub fn criterion_benchmark(criterion: &mut Criterion) {
139 let group_name = "decomposing_normalizer_nfkd";
140
141 let normalizer_under_bench = DecomposingNormalizerBorrowed::new_nfkd();
142
143 let mut group = criterion.benchmark_group(group_name);
144 for bench_data_content in black_box(normalizer_bench_data()) {
145 group.bench_function(
146 BenchmarkId::from_parameter(format!("from_nfc_{}", bench_data_content.file_name)),
147 |bencher| {
148 bencher
149 .iter(|| function_under_bench(&normalizer_under_bench, &bench_data_content.nfc))
150 },
151 );
152 group.bench_function(
153 BenchmarkId::from_parameter(format!("from_nfd_{}", bench_data_content.file_name)),
154 |bencher| {
155 bencher
156 .iter(|| function_under_bench(&normalizer_under_bench, &bench_data_content.nfd))
157 },
158 );
159 group.bench_function(
160 BenchmarkId::from_parameter(format!("from_nfkc_{}", bench_data_content.file_name)),
161 |bencher| {
162 bencher.iter(|| {
163 function_under_bench(&normalizer_under_bench, &bench_data_content.nfkc)
164 })
165 },
166 );
167 group.bench_function(
168 BenchmarkId::from_parameter(format!("from_nfkd_{}", bench_data_content.file_name)),
169 |bencher| {
170 bencher.iter(|| {
171 function_under_bench(&normalizer_under_bench, &bench_data_content.nfkd)
172 })
173 },
174 );
175
176 // UTF 16
177 group.bench_function(
178 BenchmarkId::from_parameter(format!("from_nfc_{}_u16", bench_data_content.file_name)),
179 |bencher| {
180 bencher.iter(|| {
181 function_under_bench_u16(&normalizer_under_bench, &bench_data_content.nfc_u16)
182 })
183 },
184 );
185 group.bench_function(
186 BenchmarkId::from_parameter(format!("from_nfd_{}_u16", bench_data_content.file_name)),
187 |bencher| {
188 bencher.iter(|| {
189 function_under_bench_u16(&normalizer_under_bench, &bench_data_content.nfd_u16)
190 })
191 },
192 );
193 group.bench_function(
194 BenchmarkId::from_parameter(format!("from_nfkc_{}_u16", bench_data_content.file_name)),
195 |bencher| {
196 bencher.iter(|| {
197 function_under_bench_u16(&normalizer_under_bench, &bench_data_content.nfkc_u16)
198 })
199 },
200 );
201 group.bench_function(
202 BenchmarkId::from_parameter(format!("from_nfkd_{}_u16", bench_data_content.file_name)),
203 |bencher| {
204 bencher.iter(|| {
205 function_under_bench_u16(&normalizer_under_bench, &bench_data_content.nfkd_u16)
206 })
207 },
208 );
209 }
210 group.finish();
211 }