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