| 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::measurement::WallTime; |
| 6 |
use criterion::{ |
| 7 |
BatchSize, BenchmarkGroup, BenchmarkId, Criterion, black_box, criterion_group, criterion_main, |
| 8 |
}; |
| 9 |
|
| 10 |
use icu::collator::{options::*, *}; |
| 11 |
use icu::locale::locale; |
| 12 |
use icu_locale_core::Locale; |
| 13 |
|
| 14 |
// Split the baseline and collator benches into distinct `#inline(never)` functions to avoid |
| 15 |
// collator layout changes from affecting the baseline! |
| 16 |
|
| 17 |
#[inline(never)] |
| 18 |
fn baseline_bench(group: &mut BenchmarkGroup<'_, WallTime>, file_name: &&str, elements: &[&str]) { |
| 19 |
// baseline performance, locale-unaware code point sort done by Rust (0 for ordering in the html report) |
| 20 |
group.bench_function( |
| 21 |
BenchmarkId::new(format!("{file_name}/0_rust_sort"), "default"), |
| 22 |
|bencher| { |
| 23 |
bencher.iter_batched( |
| 24 |
|| black_box(elements.to_vec()), |
| 25 |
|mut lines| lines.sort_unstable(), |
| 26 |
BatchSize::SmallInput, |
| 27 |
) |
| 28 |
}, |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
#[inline(never)] |
| 33 |
fn collator_bench( |
| 34 |
group: &mut BenchmarkGroup<'_, WallTime>, |
| 35 |
file_name: &&str, |
| 36 |
elements: &[&str], |
| 37 |
index: usize, |
| 38 |
strength: Strength, |
| 39 |
locale_under_bench: &Locale, |
| 40 |
) { |
| 41 |
let mut options = CollatorOptions::default(); |
| 42 |
options.strength = Some(strength); |
| 43 |
let collator = |
| 44 |
Collator::try_new(CollatorPreferences::from(locale_under_bench), options).unwrap(); |
| 45 |
// ICU4X collator performance, sort is locale-aware |
| 46 |
group.bench_function( |
| 47 |
BenchmarkId::new( |
| 48 |
format!("{file_name}/1_icu4x_sort"), |
| 49 |
format!("{index}_{strength:?}"), |
| 50 |
), |
| 51 |
|bencher| { |
| 52 |
bencher.iter_batched( |
| 53 |
|| black_box(elements.to_vec()), |
| 54 |
|mut lines| lines.sort_unstable_by(|left, right| collator.compare(left, right)), |
| 55 |
BatchSize::SmallInput, |
| 56 |
) |
| 57 |
}, |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
#[inline(never)] |
| 62 |
fn sort_key_bench( |
| 63 |
group: &mut BenchmarkGroup<'_, WallTime>, |
| 64 |
file_name: &&str, |
| 65 |
elements: &[&str], |
| 66 |
index: usize, |
| 67 |
strength: Strength, |
| 68 |
case_level: bool, |
| 69 |
locale_under_bench: &Locale, |
| 70 |
) { |
| 71 |
let mut options = CollatorOptions::default(); |
| 72 |
options.strength = Some(strength); |
| 73 |
if case_level { |
| 74 |
options.case_level = Some(CaseLevel::On); |
| 75 |
} |
| 76 |
let collator = |
| 77 |
Collator::try_new(CollatorPreferences::from(locale_under_bench), options).unwrap(); |
| 78 |
|
| 79 |
let suffix = if case_level { "_caseLevel" } else { "" }; |
| 80 |
|
| 81 |
group.bench_function( |
| 82 |
BenchmarkId::new( |
| 83 |
format!("{file_name}/2_icu4x_sort_key"), |
| 84 |
format!("{index}_{strength:?}{suffix}"), |
| 85 |
), |
| 86 |
|bencher| { |
| 87 |
bencher.iter(|| { |
| 88 |
for &s in black_box(elements) { |
| 89 |
let mut k = Vec::new(); |
| 90 |
let Ok(()) = collator.write_sort_key_to(black_box(s), &mut k); |
| 91 |
black_box(&k); |
| 92 |
} |
| 93 |
}) |
| 94 |
}, |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
pub fn collator_with_locale(criterion: &mut Criterion) { |
| 99 |
// Load file content in reverse order vector. |
| 100 |
let content_photos: (&str, Vec<&str>) = ( |
| 101 |
"TestFilenames_Photos", |
| 102 |
include_str!("data/TestFilenames_Photos.txt") |
| 103 |
.lines() |
| 104 |
.filter(|&s| !s.starts_with('#')) |
| 105 |
.rev() |
| 106 |
.collect::<Vec<&str>>(), |
| 107 |
); |
| 108 |
let content_latin: (&str, Vec<&str>) = ( |
| 109 |
"TestNames_Latin", |
| 110 |
include_str!("data/TestNames_Latin.txt") |
| 111 |
.lines() |
| 112 |
.filter(|&s| !s.starts_with('#')) |
| 113 |
.rev() |
| 114 |
.collect::<Vec<&str>>(), |
| 115 |
); |
| 116 |
let content_asian: (&str, Vec<&str>) = ( |
| 117 |
"TestNames_Asian", |
| 118 |
include_str!("data/TestNames_Asian.txt") |
| 119 |
.lines() |
| 120 |
.filter(|&s| !s.starts_with('#')) |
| 121 |
.rev() |
| 122 |
.collect(), |
| 123 |
); |
| 124 |
let content_russian: (&str, Vec<&str>) = ( |
| 125 |
"TestNames_Russian", |
| 126 |
include_str!("data/TestNames_Russian.txt") |
| 127 |
.lines() |
| 128 |
.filter(|&s| !s.starts_with('#')) |
| 129 |
.rev() |
| 130 |
.collect(), |
| 131 |
); |
| 132 |
let content_chinese: (&str, Vec<&str>) = ( |
| 133 |
"TestNames_Chinese", |
| 134 |
include_str!("data/TestNames_Chinese.txt") |
| 135 |
.lines() |
| 136 |
.filter(|&s| !s.starts_with('#')) |
| 137 |
.rev() |
| 138 |
.collect(), |
| 139 |
); |
| 140 |
let content_jp_h: (&str, Vec<&str>) = ( |
| 141 |
"TestNames_Japanese_h", |
| 142 |
include_str!("data/TestNames_Japanese_h.txt") |
| 143 |
.lines() |
| 144 |
.filter(|&s| !s.starts_with('#')) |
| 145 |
.rev() |
| 146 |
.collect::<Vec<&str>>(), |
| 147 |
); |
| 148 |
let content_jp_k: (&str, Vec<&str>) = ( |
| 149 |
"TestNames_Japanese_k", |
| 150 |
include_str!("data/TestNames_Japanese_k.txt") |
| 151 |
.lines() |
| 152 |
.filter(|&s| !s.starts_with('#')) |
| 153 |
.rev() |
| 154 |
.collect::<Vec<&str>>(), |
| 155 |
); |
| 156 |
let content_korean: (&str, Vec<&str>) = ( |
| 157 |
"TestNames_Korean", |
| 158 |
include_str!("data/TestNames_Korean.txt") |
| 159 |
.lines() |
| 160 |
.filter(|&s| !s.starts_with('#')) |
| 161 |
.rev() |
| 162 |
.collect::<Vec<&str>>(), |
| 163 |
); |
| 164 |
let content_swedish: (&str, Vec<&str>) = ( |
| 165 |
"TestNames_Swedish", |
| 166 |
include_str!("data/TestNames_Swedish.txt") |
| 167 |
.lines() |
| 168 |
.filter(|&s| !s.starts_with('#')) |
| 169 |
.rev() |
| 170 |
.collect::<Vec<&str>>(), |
| 171 |
); |
| 172 |
let content_thai: (&str, Vec<&str>) = ( |
| 173 |
"TestNames_Thai", |
| 174 |
include_str!("data/TestNames_Thai.txt") |
| 175 |
.lines() |
| 176 |
.filter(|&s| !s.starts_with('#')) |
| 177 |
.rev() |
| 178 |
.collect::<Vec<&str>>(), |
| 179 |
); |
| 180 |
let content_polish: (&str, Vec<&str>) = ( |
| 181 |
"TestNames_Polish", |
| 182 |
include_str!("data/TestNames_Polish.txt") |
| 183 |
.lines() |
| 184 |
.filter(|&s| !s.starts_with('#')) |
| 185 |
.rev() |
| 186 |
.collect::<Vec<&str>>(), |
| 187 |
); |
| 188 |
|
| 189 |
// hsivonen@ : All five strengths are benched. |
| 190 |
// The default is tertiary, so it makes sense to bench that. |
| 191 |
// |
| 192 |
// Also, it's particularly interesting to bench quaternary |
| 193 |
// and identical with Japanese due to the performance remarks ICU4C docs make about Japanese. |
| 194 |
// |
| 195 |
// In particular, to get full Japanese standard behavior, you need the identical strength. |
| 196 |
// Furthermore, CLDR used to default to quaternary for Japanese but now defaults to tertiary |
| 197 |
// as for every other language for performance reasons. |
| 198 |
let all_strength = [ |
| 199 |
Strength::Primary, |
| 200 |
Strength::Secondary, |
| 201 |
Strength::Tertiary, |
| 202 |
Strength::Quaternary, |
| 203 |
Strength::Identical, |
| 204 |
]; |
| 205 |
let performance_parameters = [ |
| 206 |
( |
| 207 |
locale!("en-US"), |
| 208 |
vec![&content_latin, &content_photos], |
| 209 |
&all_strength, |
| 210 |
), |
| 211 |
( |
| 212 |
locale!("da-DK"), |
| 213 |
vec![&content_latin, &content_photos], |
| 214 |
&all_strength, |
| 215 |
), |
| 216 |
(locale!("fr-CA"), vec![&content_latin], &all_strength), |
| 217 |
( |
| 218 |
locale!("ja-JP"), |
| 219 |
vec![&content_latin, &content_jp_h, &content_jp_k, &content_asian], |
| 220 |
&all_strength, |
| 221 |
), |
| 222 |
( |
| 223 |
locale!("zh-u-co-pinyin"), |
| 224 |
vec![&content_latin, &content_chinese], |
| 225 |
&all_strength, |
| 226 |
), // zh_CN |
| 227 |
( |
| 228 |
locale!("zh-u-co-stroke"), |
| 229 |
vec![&content_latin, &content_chinese], |
| 230 |
&all_strength, |
| 231 |
), // zh_TW |
| 232 |
( |
| 233 |
locale!("ru-RU"), |
| 234 |
vec![&content_latin, &content_russian], |
| 235 |
&all_strength, |
| 236 |
), |
| 237 |
( |
| 238 |
locale!("sv"), |
| 239 |
vec![&content_latin, &content_swedish], |
| 240 |
&all_strength, |
| 241 |
), |
| 242 |
( |
| 243 |
locale!("th"), |
| 244 |
vec![&content_latin, &content_thai], |
| 245 |
&all_strength, |
| 246 |
), |
| 247 |
( |
| 248 |
locale!("ko-KR"), |
| 249 |
vec![&content_latin, &content_korean], |
| 250 |
&all_strength, |
| 251 |
), |
| 252 |
( |
| 253 |
locale!("pl"), |
| 254 |
vec![&content_latin, &content_polish], |
| 255 |
&all_strength, |
| 256 |
), |
| 257 |
]; |
| 258 |
for perf_parameter in performance_parameters { |
| 259 |
let (locale_under_bench, files_under_bench, benched_strength) = perf_parameter; |
| 260 |
|
| 261 |
let mut group = criterion.benchmark_group(locale_under_bench.to_string()); |
| 262 |
|
| 263 |
for content_under_bench in files_under_bench { |
| 264 |
let (file_name, elements) = black_box(content_under_bench); |
| 265 |
baseline_bench(&mut group, file_name, elements); |
| 266 |
|
| 267 |
// index to keep order of strength in the html report |
| 268 |
for (index, strength) in benched_strength.iter().enumerate() { |
| 269 |
collator_bench( |
| 270 |
&mut group, |
| 271 |
file_name, |
| 272 |
elements, |
| 273 |
index, |
| 274 |
*strength, |
| 275 |
&locale_under_bench, |
| 276 |
); |
| 277 |
} |
| 278 |
|
| 279 |
for (index, strength) in [Strength::Primary, Strength::Tertiary] |
| 280 |
.into_iter() |
| 281 |
.enumerate() |
| 282 |
{ |
| 283 |
sort_key_bench( |
| 284 |
&mut group, |
| 285 |
file_name, |
| 286 |
elements, |
| 287 |
index, |
| 288 |
strength, |
| 289 |
false, |
| 290 |
&locale_under_bench, |
| 291 |
); |
| 292 |
} |
| 293 |
sort_key_bench( |
| 294 |
&mut group, |
| 295 |
file_name, |
| 296 |
elements, |
| 297 |
2, |
| 298 |
Strength::Tertiary, |
| 299 |
true, |
| 300 |
&locale_under_bench, |
| 301 |
); |
| 302 |
} |
| 303 |
// TODO: Bench content_photos with numeric mode on. |
| 304 |
group.finish(); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
criterion_group!( |
| 309 |
name = benches; |
| 310 |
config = Criterion::default(); |
| 311 |
targets = collator_with_locale |
| 312 |
); |
| 313 |
|
| 314 |
criterion_main!(benches); |