| 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 icu_locid::{langid, subtags::language, subtags::region, LanguageIdentifier}; |
| 6 |
use writeable::Writeable; |
| 7 |
|
| 8 |
const LIDS: &[LanguageIdentifier] = &[ |
| 9 |
langid!("en"), |
| 10 |
langid!("pl"), |
| 11 |
langid!("fr-CA"), |
| 12 |
langid!("zh-Hans"), |
| 13 |
langid!("en-US"), |
| 14 |
langid!("en-Latn-US"), |
| 15 |
langid!("sr-Cyrl-BA"), |
| 16 |
]; |
| 17 |
|
| 18 |
const LIDS_STR: &[&str] = &[ |
| 19 |
"en", |
| 20 |
"pl", |
| 21 |
"fr-CA", |
| 22 |
"zh-Hans", |
| 23 |
"en-US", |
| 24 |
"en-Latn-US", |
| 25 |
"sr-Cyrl-BA", |
| 26 |
]; |
| 27 |
|
| 28 |
fn bench_langid_constr() { |
| 29 |
// Tests the instructions required to construct a LID from an str. |
| 30 |
|
| 31 |
let _: Vec<LanguageIdentifier> = LIDS_STR |
| 32 |
.iter() |
| 33 |
.map(|l| l.parse().expect("Failed to parse")) |
| 34 |
.collect(); |
| 35 |
} |
| 36 |
|
| 37 |
fn bench_langid_compare_components() { |
| 38 |
// Tests the cost of comparing LID components. |
| 39 |
|
| 40 |
let result = LIDS |
| 41 |
.iter() |
| 42 |
.filter(|l| l.language == language!("en") && l.region == Some(region!("US"))) |
| 43 |
.count(); |
| 44 |
|
| 45 |
assert_eq!(result, 2); |
| 46 |
} |
| 47 |
|
| 48 |
fn bench_langid_compare_components_str() { |
| 49 |
// Tests the cost of comparing LID components to str. |
| 50 |
|
| 51 |
let result = LIDS |
| 52 |
.iter() |
| 53 |
.filter(|l| { |
| 54 |
l.language == language!("en") && l.region.map(|r| r == region!("US")).unwrap_or(false) |
| 55 |
}) |
| 56 |
.count(); |
| 57 |
|
| 58 |
assert_eq!(result, 2); |
| 59 |
} |
| 60 |
|
| 61 |
fn bench_langid_strict_cmp() { |
| 62 |
// Tests the cost of comparing a langid against byte strings. |
| 63 |
use core::cmp::Ordering; |
| 64 |
|
| 65 |
let lid = langid!("en_us"); |
| 66 |
|
| 67 |
let result = LIDS_STR |
| 68 |
.iter() |
| 69 |
.filter(|s| lid.strict_cmp(s.as_bytes()) == Ordering::Equal) |
| 70 |
.count(); |
| 71 |
|
| 72 |
assert_eq!(result, 1); |
| 73 |
} |
| 74 |
|
| 75 |
fn bench_langid_matching() { |
| 76 |
// Tests matching a LID against other LIDs. |
| 77 |
|
| 78 |
let lid = langid!("en_us"); |
| 79 |
|
| 80 |
let count = LIDS.iter().filter(|l| lid == **l).count(); |
| 81 |
assert_eq!(count, 1); |
| 82 |
} |
| 83 |
|
| 84 |
fn bench_langid_matching_str() { |
| 85 |
// Tests matching a LID against list of str. |
| 86 |
|
| 87 |
let lid = langid!("en_us"); |
| 88 |
|
| 89 |
let count = LIDS_STR.iter().filter(|&l| lid.normalizing_eq(l)).count(); |
| 90 |
assert_eq!(count, 1); |
| 91 |
} |
| 92 |
|
| 93 |
fn bench_langid_serialize() { |
| 94 |
// Tests serialization of LIDs. |
| 95 |
|
| 96 |
let _: Vec<String> = LIDS.iter().map(|l| l.to_string()).collect(); |
| 97 |
} |
| 98 |
|
| 99 |
fn bench_langid_serialize_writeable() { |
| 100 |
// Tests serialization of LIDs. |
| 101 |
|
| 102 |
let _: Vec<_> = LIDS.iter().map(|l| l.write_to_string()).collect(); |
| 103 |
} |
| 104 |
|
| 105 |
fn bench_langid_canonicalize() { |
| 106 |
// Tests canonicalization of strings. |
| 107 |
|
| 108 |
let _: Vec<String> = LIDS_STR |
| 109 |
.iter() |
| 110 |
.map(|l| LanguageIdentifier::canonicalize(l).expect("Canonicalization failed")) |
| 111 |
.collect(); |
| 112 |
} |
| 113 |
|
| 114 |
iai::main!( |
| 115 |
bench_langid_constr, |
| 116 |
bench_langid_compare_components, |
| 117 |
bench_langid_compare_components_str, |
| 118 |
bench_langid_strict_cmp, |
| 119 |
bench_langid_matching, |
| 120 |
bench_langid_matching_str, |
| 121 |
bench_langid_serialize, |
| 122 |
bench_langid_serialize_writeable, |
| 123 |
bench_langid_canonicalize, |
| 124 |
); |