-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57d38ce
commit 378718b
Showing
14 changed files
with
219 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,95 @@ | ||
use criterion::criterion_group; | ||
use criterion::criterion_main; | ||
use criterion::Criterion; | ||
use criterion::Fun; | ||
|
||
use tinystr::{TinyStr4, TinyStr8}; | ||
use unic_langid_impl::LanguageIdentifier; | ||
|
||
fn language_identifier_from_str_bench(c: &mut Criterion) { | ||
let strings = &[ | ||
"en-US", | ||
"en-GB", | ||
"es-AR", | ||
"it", | ||
"zh-Hans-CN", | ||
"de-AT", | ||
"pl", | ||
"fr-FR", | ||
"de-AT", | ||
"sr-Cyrl-SR", | ||
"nb-NO", | ||
"fr-FR", | ||
"mk", | ||
"uk", | ||
]; | ||
c.bench_function("language_identifier_from_str", move |b| { | ||
b.iter(|| { | ||
for s in strings { | ||
let _: Result<LanguageIdentifier, _> = s.parse(); | ||
} | ||
}) | ||
}); | ||
} | ||
static STRINGS: &[&str] = &[ | ||
"en-US", | ||
"en-GB", | ||
"es-AR", | ||
"it", | ||
"zh-Hans-CN", | ||
"de-AT", | ||
"pl", | ||
"fr-FR", | ||
"de-AT", | ||
"sr-Cyrl-SR", | ||
"nb-NO", | ||
"fr-FR", | ||
"mk", | ||
"uk", | ||
]; | ||
|
||
fn language_identifier_from_parts_bench(c: &mut Criterion) { | ||
let entries: Vec<(Option<&str>, Option<&str>, Option<&str>, Option<&[&&str]>)> = vec![ | ||
(Some("en"), None, Some("US"), None), | ||
(Some("en"), None, Some("GB"), None), | ||
(Some("es"), None, Some("AR"), None), | ||
(Some("it"), None, None, None), | ||
(Some("zh"), Some("Hans"), Some("CN"), None), | ||
(Some("de"), None, Some("AT"), None), | ||
(Some("pl"), None, None, None), | ||
(Some("fr"), None, Some("FR"), None), | ||
(Some("de"), None, Some("AT"), None), | ||
(Some("sr"), Some("Cyrl"), Some("SR"), None), | ||
(Some("nb"), None, Some("NO"), None), | ||
(Some("fr"), None, Some("FR"), None), | ||
(Some("mk"), None, None, None), | ||
(Some("uk"), None, None, None), | ||
]; | ||
c.bench_function("language_identifier_from_parts", move |b| { | ||
b.iter(|| { | ||
for (language, region, script, variants) in &entries { | ||
let _ = LanguageIdentifier::from_parts( | ||
language.as_ref(), | ||
region.as_ref(), | ||
script.as_ref(), | ||
*variants, | ||
); | ||
} | ||
}) | ||
}); | ||
fn language_identifier_construct_bench(c: &mut Criterion) { | ||
let langids: Vec<LanguageIdentifier> = STRINGS | ||
.iter() | ||
.map(|s| -> LanguageIdentifier { s.parse().unwrap() }) | ||
.collect(); | ||
|
||
let entries2: Vec<(Option<&str>, Option<&str>, Option<&str>, Option<&[&str]>)> = vec![ | ||
(Some("en"), None, Some("US"), None), | ||
(Some("en"), None, Some("GB"), None), | ||
(Some("es"), None, Some("AR"), None), | ||
(Some("it"), None, None, None), | ||
(Some("zh"), Some("Hans"), Some("CN"), None), | ||
(Some("de"), None, Some("AT"), None), | ||
(Some("pl"), None, None, None), | ||
(Some("fr"), None, Some("FR"), None), | ||
(Some("de"), None, Some("AT"), None), | ||
(Some("sr"), Some("Cyrl"), Some("SR"), None), | ||
(Some("nb"), None, Some("NO"), None), | ||
(Some("fr"), None, Some("FR"), None), | ||
(Some("mk"), None, None, None), | ||
(Some("uk"), None, None, None), | ||
let funcs = vec![ | ||
Fun::new("from_str", |b, _| { | ||
b.iter(|| { | ||
for s in STRINGS { | ||
let _: Result<LanguageIdentifier, _> = s.parse(); | ||
} | ||
}) | ||
}), | ||
Fun::new("from_parts", |b, langids: &Vec<LanguageIdentifier>| { | ||
let entries: Vec<(Option<&str>, Option<&str>, Option<&str>, Vec<&str>)> = langids | ||
.iter() | ||
.map(|langid| { | ||
let lang = Some(langid.get_language()).and_then(|s| { | ||
if s == "und" { | ||
None | ||
} else { | ||
Some(s) | ||
} | ||
}); | ||
( | ||
lang, | ||
langid.get_script(), | ||
langid.get_region(), | ||
langid.get_variants(), | ||
) | ||
}) | ||
.collect(); | ||
b.iter(|| { | ||
for (language, script, region, variants) in &entries { | ||
let _ = LanguageIdentifier::from_parts(*language, *script, *region, variants); | ||
} | ||
}) | ||
}), | ||
Fun::new( | ||
"from_parts_unchecked", | ||
|b, langids: &Vec<LanguageIdentifier>| { | ||
let entries = langids | ||
.iter() | ||
.map(|langid| langid.clone().to_raw_parts()) | ||
.collect::<Vec<_>>(); | ||
b.iter(|| { | ||
for (language, script, region, variants) in &entries { | ||
let _ = unsafe { | ||
LanguageIdentifier::from_raw_parts_unchecked( | ||
language.map(|l| TinyStr8::new_unchecked(l)), | ||
script.map(|s| TinyStr4::new_unchecked(s)), | ||
region.map(|r| TinyStr4::new_unchecked(r)), | ||
variants | ||
.into_iter() | ||
.map(|v| TinyStr8::new_unchecked(*v)) | ||
.collect(), | ||
) | ||
}; | ||
} | ||
}) | ||
}, | ||
), | ||
]; | ||
c.bench_function("language_identifier_from_parts_unchecked", move |b| { | ||
b.iter(|| { | ||
for (language, region, script, variants) in &entries2 { | ||
let _ = LanguageIdentifier::from_parts_unchecked( | ||
*language, *region, *script, *variants, | ||
); | ||
} | ||
}) | ||
}); | ||
|
||
c.bench_functions("language_identifier_construct", funcs, langids); | ||
} | ||
|
||
criterion_group!( | ||
benches, | ||
language_identifier_from_str_bench, | ||
language_identifier_from_parts_bench,, | ||
); | ||
criterion_group!(benches, language_identifier_construct_bench,); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.