| 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 |
use detone::IterDecomposeVietnamese; |
| 7 |
|
| 8 |
use icu_normalizer::properties::{CanonicalComposition, CanonicalDecomposition, Decomposed}; |
| 9 |
use icu_normalizer::ComposingNormalizer; |
| 10 |
|
| 11 |
struct BenchDataContent { |
| 12 |
pub file_name: String, |
| 13 |
pub pairs: Vec<(char, char)>, |
| 14 |
} |
| 15 |
|
| 16 |
fn strip_headers(content: &str) -> String { |
| 17 |
content |
| 18 |
.lines() |
| 19 |
.filter(|&s| !s.starts_with('#')) |
| 20 |
.map(|s| s.to_owned()) |
| 21 |
.collect::<Vec<String>>() |
| 22 |
.join("\n") |
| 23 |
} |
| 24 |
|
| 25 |
fn normalizer_bench_data() -> [BenchDataContent; 16] { |
| 26 |
let nfc_normalizer: ComposingNormalizer = ComposingNormalizer::new_nfc(); |
| 27 |
|
| 28 |
return [ |
| 29 |
BenchDataContent { |
| 30 |
file_name: "TestNames_Latin".to_owned(), |
| 31 |
pairs: decompose_data( |
| 32 |
&nfc_normalizer |
| 33 |
.normalize(&strip_headers(include_str!("./data/TestNames_Latin.txt"))), |
| 34 |
), |
| 35 |
}, |
| 36 |
BenchDataContent { |
| 37 |
file_name: "TestNames_Japanese_h".to_owned(), |
| 38 |
pairs: decompose_data(&nfc_normalizer.normalize(&strip_headers(include_str!( |
| 39 |
"./data/TestNames_Japanese_h.txt" |
| 40 |
)))), |
| 41 |
}, |
| 42 |
BenchDataContent { |
| 43 |
file_name: "TestNames_Japanese_k".to_owned(), |
| 44 |
pairs: decompose_data(&nfc_normalizer.normalize(&strip_headers(include_str!( |
| 45 |
"./data/TestNames_Japanese_k.txt" |
| 46 |
)))), |
| 47 |
}, |
| 48 |
BenchDataContent { |
| 49 |
file_name: "TestNames_Korean".to_owned(), |
| 50 |
pairs: decompose_data( |
| 51 |
&nfc_normalizer |
| 52 |
.normalize(&strip_headers(include_str!("./data/TestNames_Korean.txt"))), |
| 53 |
), |
| 54 |
}, |
| 55 |
BenchDataContent { |
| 56 |
file_name: "TestRandomWordsUDHR_ar".to_owned(), |
| 57 |
#[cfg(debug_assertions)] |
| 58 |
pairs: Vec::new(), |
| 59 |
#[cfg(not(debug_assertions))] |
| 60 |
pairs: decompose_data(&nfc_normalizer.normalize(&strip_headers(include_str!( |
| 61 |
"./data/TestRandomWordsUDHR_ar.txt" |
| 62 |
)))), |
| 63 |
}, |
| 64 |
BenchDataContent { |
| 65 |
file_name: "TestRandomWordsUDHR_de".to_owned(), |
| 66 |
pairs: decompose_data(&nfc_normalizer.normalize(&strip_headers(include_str!( |
| 67 |
"./data/TestRandomWordsUDHR_de.txt" |
| 68 |
)))), |
| 69 |
}, |
| 70 |
BenchDataContent { |
| 71 |
file_name: "TestRandomWordsUDHR_el".to_owned(), |
| 72 |
pairs: decompose_data(&nfc_normalizer.normalize(&strip_headers(include_str!( |
| 73 |
"./data/TestRandomWordsUDHR_el.txt" |
| 74 |
)))), |
| 75 |
}, |
| 76 |
BenchDataContent { |
| 77 |
file_name: "TestRandomWordsUDHR_es".to_owned(), |
| 78 |
pairs: decompose_data(&nfc_normalizer.normalize(&strip_headers(include_str!( |
| 79 |
"./data/TestRandomWordsUDHR_es.txt" |
| 80 |
)))), |
| 81 |
}, |
| 82 |
BenchDataContent { |
| 83 |
file_name: "TestRandomWordsUDHR_fr".to_owned(), |
| 84 |
pairs: decompose_data(&nfc_normalizer.normalize(&strip_headers(include_str!( |
| 85 |
"./data/TestRandomWordsUDHR_fr.txt" |
| 86 |
)))), |
| 87 |
}, |
| 88 |
BenchDataContent { |
| 89 |
file_name: "TestRandomWordsUDHR_he".to_owned(), |
| 90 |
pairs: decompose_data(&nfc_normalizer.normalize(&strip_headers(include_str!( |
| 91 |
"./data/TestRandomWordsUDHR_he.txt" |
| 92 |
)))), |
| 93 |
}, |
| 94 |
BenchDataContent { |
| 95 |
file_name: "TestRandomWordsUDHR_pl".to_owned(), |
| 96 |
pairs: decompose_data(&nfc_normalizer.normalize(&strip_headers(include_str!( |
| 97 |
"./data/TestRandomWordsUDHR_pl.txt" |
| 98 |
)))), |
| 99 |
}, |
| 100 |
BenchDataContent { |
| 101 |
file_name: "TestRandomWordsUDHR_ru".to_owned(), |
| 102 |
pairs: decompose_data(&nfc_normalizer.normalize(&strip_headers(include_str!( |
| 103 |
"./data/TestRandomWordsUDHR_ru.txt" |
| 104 |
)))), |
| 105 |
}, |
| 106 |
BenchDataContent { |
| 107 |
file_name: "TestRandomWordsUDHR_th".to_owned(), |
| 108 |
#[cfg(debug_assertions)] |
| 109 |
pairs: Vec::new(), |
| 110 |
#[cfg(not(debug_assertions))] |
| 111 |
pairs: decompose_data(&nfc_normalizer.normalize(&strip_headers(include_str!( |
| 112 |
"./data/TestRandomWordsUDHR_th.txt" |
| 113 |
)))), |
| 114 |
}, |
| 115 |
BenchDataContent { |
| 116 |
file_name: "TestRandomWordsUDHR_tr".to_owned(), |
| 117 |
pairs: decompose_data(&nfc_normalizer.normalize(&strip_headers(include_str!( |
| 118 |
"./data/TestRandomWordsUDHR_tr.txt" |
| 119 |
)))), |
| 120 |
}, |
| 121 |
BenchDataContent { |
| 122 |
file_name: "udhr_vie".to_owned(), |
| 123 |
pairs: decompose_data( |
| 124 |
&nfc_normalizer.normalize(&strip_headers(include_str!("data/wotw.txt"))), |
| 125 |
), |
| 126 |
}, |
| 127 |
BenchDataContent { |
| 128 |
file_name: "udhr_vie_detone".to_owned(), |
| 129 |
pairs: { |
| 130 |
let result: Vec<(char, char)> = nfc_normalizer |
| 131 |
.normalize(&strip_headers(include_str!("data/wotw.txt"))) |
| 132 |
.chars() |
| 133 |
.filter_map(|c| { |
| 134 |
let mut iter = std::iter::once(c).decompose_vietnamese_tones(true); |
| 135 |
if let Some(base) = iter.next() { |
| 136 |
iter.next().map(|tone| (base, tone)) |
| 137 |
} else { |
| 138 |
None |
| 139 |
} |
| 140 |
}) |
| 141 |
.collect(); |
| 142 |
assert!(!result.is_empty()); |
| 143 |
result |
| 144 |
}, |
| 145 |
}, |
| 146 |
]; |
| 147 |
} |
| 148 |
|
| 149 |
fn function_under_bench( |
| 150 |
canonical_composer: &CanonicalComposition, |
| 151 |
composable_points: &[(char, char)], |
| 152 |
) { |
| 153 |
for pair in composable_points.iter() { |
| 154 |
canonical_composer.compose(pair.0, pair.1); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
pub fn criterion_benchmark(criterion: &mut Criterion) { |
| 159 |
let group_name = "canonical_composition"; |
| 160 |
let mut group = criterion.benchmark_group(group_name); |
| 161 |
|
| 162 |
let composer = CanonicalComposition::new(); |
| 163 |
|
| 164 |
for bench_data_content in black_box(normalizer_bench_data()) { |
| 165 |
group.bench_function( |
| 166 |
BenchmarkId::from_parameter(format!("from_nfc_{}", bench_data_content.file_name)), |
| 167 |
|bencher| bencher.iter(|| function_under_bench(&composer, &bench_data_content.pairs)), |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
group.finish(); |
| 172 |
} |
| 173 |
|
| 174 |
fn decompose_data(nfc: &str) -> Vec<(char, char)> { |
| 175 |
let decomposer = CanonicalDecomposition::new(); |
| 176 |
nfc.chars() |
| 177 |
.map(|c| decomposer.decompose(c)) |
| 178 |
.filter_map(|decomposed| { |
| 179 |
if let Decomposed::Expansion(a, b) = decomposed { |
| 180 |
Some((a, b)) |
| 181 |
} else { |
| 182 |
None |
| 183 |
} |
| 184 |
}) |
| 185 |
.collect() |
| 186 |
} |