Debian Codegraph @

debian/missing-sources/icu_normalizer/benches/composing_normalizer_nfc.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: &ComposingNormalizerBorrowed, text: &str) {
131 normalizer.normalize(text);
132 }
133
134 fn function_under_bench_utf16(normalizer: &ComposingNormalizerBorrowed, text: &[u16]) {
135 normalizer.normalize_utf16(text);
136 }
137
138 pub fn criterion_benchmark(criterion: &mut Criterion) {
139 let group_name = "composing_normalizer_nfc";
140
141 let normalizer_under_bench = ComposingNormalizerBorrowed::new_nfc();
142
143 let mut group = criterion.benchmark_group(group_name);
144
145 for bench_data_content in black_box(normalizer_bench_data()) {
146 group.bench_function(
147 BenchmarkId::from_parameter(format!("from_nfc_{}", bench_data_content.file_name)),
148 |bencher| {
149 bencher
150 .iter(|| function_under_bench(&normalizer_under_bench, &bench_data_content.nfc))
151 },
152 );
153 group.bench_function(
154 BenchmarkId::from_parameter(format!("from_nfd_{}", bench_data_content.file_name)),
155 |bencher| {
156 bencher
157 .iter(|| function_under_bench(&normalizer_under_bench, &bench_data_content.nfd))
158 },
159 );
160 group.bench_function(
161 BenchmarkId::from_parameter(format!("from_nfkc_{}", bench_data_content.file_name)),
162 |bencher| {
163 bencher.iter(|| {
164 function_under_bench(&normalizer_under_bench, &bench_data_content.nfkc)
165 })
166 },
167 );
168 group.bench_function(
169 BenchmarkId::from_parameter(format!("from_nfkd_{}", bench_data_content.file_name)),
170 |bencher| {
171 bencher.iter(|| {
172 function_under_bench(&normalizer_under_bench, &bench_data_content.nfkd)
173 })
174 },
175 );
176
177 // UTF_16
178 group.bench_function(
179 BenchmarkId::from_parameter(format!(
180 "from_nfc_{}_utf_16",
181 bench_data_content.file_name
182 )),
183 |bencher| {
184 bencher.iter(|| {
185 function_under_bench_utf16(&normalizer_under_bench, &bench_data_content.nfc_u16)
186 })
187 },
188 );
189 group.bench_function(
190 BenchmarkId::from_parameter(format!(
191 "from_nfd_{}_utf_16",
192 bench_data_content.file_name
193 )),
194 |bencher| {
195 bencher.iter(|| {
196 function_under_bench_utf16(&normalizer_under_bench, &bench_data_content.nfd_u16)
197 })
198 },
199 );
200 group.bench_function(
201 BenchmarkId::from_parameter(format!(
202 "from_nfkc_{}_utf_16",
203 bench_data_content.file_name
204 )),
205 |bencher| {
206 bencher.iter(|| {
207 function_under_bench_utf16(
208 &normalizer_under_bench,
209 &bench_data_content.nfkc_u16,
210 )
211 })
212 },
213 );
214 group.bench_function(
215 BenchmarkId::from_parameter(format!(
216 "from_nfkd_{}_utf_16",
217 bench_data_content.file_name
218 )),
219 |bencher| {
220 bencher.iter(|| {
221 function_under_bench_utf16(
222 &normalizer_under_bench,
223 &bench_data_content.nfkd_u16,
224 )
225 })
226 },
227 );
228 }
229 group.finish();
230 }