| 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::properties::CanonicalDecompositionBorrowed; |
| 8 |
use icu_normalizer::{ComposingNormalizerBorrowed, DecomposingNormalizerBorrowed}; |
| 9 |
|
| 10 |
struct BenchDataContent { |
| 11 |
pub file_name: String, |
| 12 |
pub nfc: String, |
| 13 |
pub nfd: String, |
| 14 |
pub nfkc: String, |
| 15 |
pub nfkd: String, |
| 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; 15] { |
| 28 |
let nfc_normalizer = ComposingNormalizerBorrowed::new_nfc(); |
| 29 |
let nfd_normalizer = DecomposingNormalizerBorrowed::new_nfd(); |
| 30 |
let nfkc_normalizer = ComposingNormalizerBorrowed::new_nfkc(); |
| 31 |
let nfkd_normalizer = DecomposingNormalizerBorrowed::new_nfkd(); |
| 32 |
|
| 33 |
let content_latin: (&str, &str) = ( |
| 34 |
"TestNames_Latin", |
| 35 |
&strip_headers(include_str!("./data/TestNames_Latin.txt")), |
| 36 |
); |
| 37 |
let content_jp_h: (&str, &str) = ( |
| 38 |
"TestNames_Japanese_h", |
| 39 |
&strip_headers(include_str!("./data/TestNames_Japanese_h.txt")), |
| 40 |
); |
| 41 |
let content_jp_k: (&str, &str) = ( |
| 42 |
"TestNames_Japanese_k", |
| 43 |
&strip_headers(include_str!("./data/TestNames_Japanese_k.txt")), |
| 44 |
); |
| 45 |
let content_korean: (&str, &str) = ( |
| 46 |
"TestNames_Korean", |
| 47 |
&strip_headers(include_str!("./data/TestNames_Korean.txt")), |
| 48 |
); |
| 49 |
let content_random_words_ar: (&str, &str) = ( |
| 50 |
"TestRandomWordsUDHR_ar", |
| 51 |
&strip_headers(include_str!("./data/TestRandomWordsUDHR_ar.txt")), |
| 52 |
); |
| 53 |
let content_random_words_de: (&str, &str) = ( |
| 54 |
"TestRandomWordsUDHR_de", |
| 55 |
&strip_headers(include_str!("./data/TestRandomWordsUDHR_de.txt")), |
| 56 |
); |
| 57 |
let content_random_words_el: (&str, &str) = ( |
| 58 |
"TestRandomWordsUDHR_el", |
| 59 |
&strip_headers(include_str!("./data/TestRandomWordsUDHR_el.txt")), |
| 60 |
); |
| 61 |
let content_random_words_es: (&str, &str) = ( |
| 62 |
"TestRandomWordsUDHR_es", |
| 63 |
&strip_headers(include_str!("./data/TestRandomWordsUDHR_es.txt")), |
| 64 |
); |
| 65 |
let content_random_words_fr: (&str, &str) = ( |
| 66 |
"TestRandomWordsUDHR_fr", |
| 67 |
&strip_headers(include_str!("./data/TestRandomWordsUDHR_fr.txt")), |
| 68 |
); |
| 69 |
let content_random_words_he: (&str, &str) = ( |
| 70 |
"TestRandomWordsUDHR_he", |
| 71 |
&strip_headers(include_str!("./data/TestRandomWordsUDHR_he.txt")), |
| 72 |
); |
| 73 |
let content_random_words_pl: (&str, &str) = ( |
| 74 |
"TestRandomWordsUDHR_pl", |
| 75 |
&strip_headers(include_str!("./data/TestRandomWordsUDHR_pl.txt")), |
| 76 |
); |
| 77 |
let content_random_words_ru: (&str, &str) = ( |
| 78 |
"TestRandomWordsUDHR_ru", |
| 79 |
&strip_headers(include_str!("./data/TestRandomWordsUDHR_ru.txt")), |
| 80 |
); |
| 81 |
let content_random_words_th: (&str, &str) = ( |
| 82 |
"TestRandomWordsUDHR_th", |
| 83 |
&strip_headers(include_str!("./data/TestRandomWordsUDHR_th.txt")), |
| 84 |
); |
| 85 |
let content_random_words_tr: (&str, &str) = ( |
| 86 |
"TestRandomWordsUDHR_tr", |
| 87 |
&strip_headers(include_str!("./data/TestRandomWordsUDHR_tr.txt")), |
| 88 |
); |
| 89 |
let content_viet: (&str, &str) = ("udhr_vie", &strip_headers(include_str!("data/wotw.txt"))); |
| 90 |
|
| 91 |
[ |
| 92 |
content_latin, |
| 93 |
content_viet, |
| 94 |
content_jp_k, |
| 95 |
content_jp_h, |
| 96 |
content_korean, |
| 97 |
content_random_words_ru, |
| 98 |
content_random_words_ar, |
| 99 |
content_random_words_el, |
| 100 |
content_random_words_es, |
| 101 |
content_random_words_fr, |
| 102 |
content_random_words_tr, |
| 103 |
content_random_words_th, |
| 104 |
content_random_words_pl, |
| 105 |
content_random_words_he, |
| 106 |
content_random_words_de, |
| 107 |
] |
| 108 |
.map(|(file_name, raw_content)| BenchDataContent { |
| 109 |
file_name: file_name.to_owned(), |
| 110 |
nfc: nfc_normalizer.normalize(raw_content).to_string(), |
| 111 |
nfd: nfd_normalizer.normalize(raw_content).to_string(), |
| 112 |
nfkc: nfkc_normalizer.normalize(raw_content).to_string(), |
| 113 |
nfkd: nfkd_normalizer.normalize(raw_content).to_string(), |
| 114 |
}) |
| 115 |
} |
| 116 |
|
| 117 |
#[cfg(debug_assertions)] |
| 118 |
fn function_under_bench( |
| 119 |
_canonical_decomposer: &CanonicalDecompositionBorrowed, |
| 120 |
_decomposable_points: &str, |
| 121 |
) { |
| 122 |
// using debug assertion fails some test. |
| 123 |
// "cargo test --bench bench" will pass |
| 124 |
// "cargo bench" will work as expected, because the profile doesn't include debug assertions. |
| 125 |
} |
| 126 |
|
| 127 |
#[cfg(not(debug_assertions))] |
| 128 |
fn function_under_bench( |
| 129 |
canonical_decomposer: &CanonicalDecompositionBorrowed, |
| 130 |
decomposable_points: &str, |
| 131 |
) { |
| 132 |
decomposable_points.chars().for_each(|point| { |
| 133 |
canonical_decomposer.decompose(point); |
| 134 |
}); |
| 135 |
} |
| 136 |
|
| 137 |
pub fn criterion_benchmark(criterion: &mut Criterion) { |
| 138 |
let group_name = "canonical_decomposition"; |
| 139 |
let mut group = criterion.benchmark_group(group_name); |
| 140 |
|
| 141 |
let decomposer = CanonicalDecompositionBorrowed::new(); |
| 142 |
|
| 143 |
for bench_data_content in black_box(normalizer_bench_data()) { |
| 144 |
group.bench_function( |
| 145 |
BenchmarkId::from_parameter(format!("from_nfc_{}", bench_data_content.file_name)), |
| 146 |
|bencher| bencher.iter(|| function_under_bench(&decomposer, &bench_data_content.nfc)), |
| 147 |
); |
| 148 |
group.bench_function( |
| 149 |
BenchmarkId::from_parameter(format!("from_nfd_{}", bench_data_content.file_name)), |
| 150 |
|bencher| bencher.iter(|| function_under_bench(&decomposer, &bench_data_content.nfd)), |
| 151 |
); |
| 152 |
group.bench_function( |
| 153 |
BenchmarkId::from_parameter(format!("from_nfkc_{}", bench_data_content.file_name)), |
| 154 |
|bencher| bencher.iter(|| function_under_bench(&decomposer, &bench_data_content.nfkc)), |
| 155 |
); |
| 156 |
group.bench_function( |
| 157 |
BenchmarkId::from_parameter(format!("from_nfkd_{}", bench_data_content.file_name)), |
| 158 |
|bencher| bencher.iter(|| function_under_bench(&decomposer, &bench_data_content.nfkd)), |
| 159 |
); |
| 160 |
} |
| 161 |
group.finish(); |
| 162 |
} |