| 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::{Criterion, black_box, criterion_group, criterion_main}; |
| 6 |
use icu_locale::LocaleCanonicalizer; |
| 7 |
use icu_locale::LocaleExpander; |
| 8 |
use icu_locale_core::Locale; |
| 9 |
|
| 10 |
fn canonicalize_bench(c: &mut Criterion) { |
| 11 |
let lc = LocaleCanonicalizer::new_common(); |
| 12 |
|
| 13 |
let mut group = c.benchmark_group("uncanonicalized"); |
| 14 |
|
| 15 |
let data: Vec<String> = |
| 16 |
serde_json::from_str(include_str!("fixtures/uncanonicalized-locales.json")) |
| 17 |
.expect("Failed to read a fixture"); |
| 18 |
let locales: Vec<Locale> = data.iter().map(|s| s.parse().unwrap()).collect(); |
| 19 |
|
| 20 |
group.bench_function("clone", |b| { |
| 21 |
b.iter(|| { |
| 22 |
for locale in &locales { |
| 23 |
let _ = black_box(locale).clone(); |
| 24 |
} |
| 25 |
}) |
| 26 |
}); |
| 27 |
|
| 28 |
group.bench_function("canonicalize", |b| { |
| 29 |
b.iter(|| { |
| 30 |
for locale in &locales { |
| 31 |
let mut locale = black_box(locale).clone(); |
| 32 |
lc.canonicalize(&mut locale); |
| 33 |
} |
| 34 |
}) |
| 35 |
}); |
| 36 |
|
| 37 |
group.finish(); |
| 38 |
} |
| 39 |
|
| 40 |
fn canonicalize_noop_bench(c: &mut Criterion) { |
| 41 |
let lc = LocaleCanonicalizer::new_common(); |
| 42 |
|
| 43 |
let mut group = c.benchmark_group("canonicalized"); |
| 44 |
|
| 45 |
// None of these locales require canonicalization, so this measures the cost of calling |
| 46 |
// the canonicalizer on locales that will not be modified. |
| 47 |
let data: Vec<String> = serde_json::from_str(include_str!("fixtures/locales.json")) |
| 48 |
.expect("Failed to read a fixture"); |
| 49 |
let locales: Vec<Locale> = data.iter().map(|s| s.parse().unwrap()).collect(); |
| 50 |
|
| 51 |
group.bench_function("clone", |b| { |
| 52 |
b.iter(|| { |
| 53 |
for locale in &locales { |
| 54 |
let _ = black_box(locale).clone(); |
| 55 |
} |
| 56 |
}) |
| 57 |
}); |
| 58 |
|
| 59 |
group.bench_function("canonicalize", |b| { |
| 60 |
b.iter(|| { |
| 61 |
for locale in &locales { |
| 62 |
let mut locale = black_box(locale).clone(); |
| 63 |
lc.canonicalize(&mut locale); |
| 64 |
} |
| 65 |
}) |
| 66 |
}); |
| 67 |
|
| 68 |
group.finish(); |
| 69 |
} |
| 70 |
|
| 71 |
fn maximize_bench(c: &mut Criterion) { |
| 72 |
let lc = LocaleExpander::new_common(); |
| 73 |
|
| 74 |
let mut group = c.benchmark_group("likelysubtags"); |
| 75 |
|
| 76 |
let data: Vec<String> = serde_json::from_str(include_str!("fixtures/locales.json")) |
| 77 |
.expect("Failed to read a fixture"); |
| 78 |
let locales: Vec<Locale> = data.iter().map(|s| s.parse().unwrap()).collect(); |
| 79 |
|
| 80 |
group.bench_function("maximize", |b| { |
| 81 |
b.iter(|| { |
| 82 |
for locale in &locales { |
| 83 |
let mut locale = locale.clone(); |
| 84 |
lc.maximize(black_box(&mut locale.id)); |
| 85 |
} |
| 86 |
}) |
| 87 |
}); |
| 88 |
|
| 89 |
group.bench_function("minimize", |b| { |
| 90 |
b.iter(|| { |
| 91 |
for locale in &locales { |
| 92 |
let mut locale = locale.clone(); |
| 93 |
lc.minimize(black_box(&mut locale.id)); |
| 94 |
} |
| 95 |
}) |
| 96 |
}); |
| 97 |
|
| 98 |
group.finish(); |
| 99 |
} |
| 100 |
|
| 101 |
criterion_group!( |
| 102 |
benches, |
| 103 |
canonicalize_bench, |
| 104 |
canonicalize_noop_bench, |
| 105 |
maximize_bench |
| 106 |
); |
| 107 |
criterion_main!(benches); |