summaryrefslogtreecommitdiffstats
path: root/vendor/unic-langid-impl/benches/likely_subtags.rs
blob: a01666597ba02f3de3a0678743f00c885a643cc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use criterion::black_box;
use criterion::criterion_group;
use criterion::criterion_main;
use criterion::Criterion;

use tinystr::{TinyStr4, TinyStr8};
use unic_langid_impl::LanguageIdentifier;

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",
    "und-PL",
    "und-Latn-AM",
    "ug-Cyrl",
    "sr-ME",
    "mn-Mong",
    "lif-Limb",
    "gan",
    "zh-Hant",
    "yue-Hans",
    "unr",
    "unr-Deva",
    "und-Thai-CN",
    "ug-Cyrl",
    "en-Latn-DE",
    "pl-FR",
    "de-CH",
    "tuq",
    "sr-ME",
    "ng",
    "klx",
    "kk-Arab",
    "en-Cyrl",
    "und-Cyrl-UK",
    "und-Arab",
    "und-Arab-FO",
];

fn maximize_bench(c: &mut Criterion) {
    c.bench_function("maximize", move |b| {
        b.iter(|| {
            let langids: Vec<LanguageIdentifier> = STRINGS
                .iter()
                .map(|s| -> LanguageIdentifier { s.parse().unwrap() })
                .collect();
            for mut s in langids {
                s.maximize();
                let _ = black_box(s.to_string());
            }
        })
    });
}

fn extract_input(s: &str) -> (Option<TinyStr8>, Option<TinyStr4>, Option<TinyStr4>) {
    let chunks: Vec<&str> = s.split("-").collect();
    let mut lang: Option<TinyStr8> = chunks.get(0).map(|s| s.parse().unwrap());
    let mut script: Option<TinyStr4> = chunks.get(1).map(|s| s.parse().unwrap());
    let mut region: Option<TinyStr4> = chunks.get(2).map(|s| s.parse().unwrap());
    if let Some(l) = lang {
        if l.as_str() == "und" {
            lang = None;
        }
    }
    if let Some(s) = script {
        if s.as_str().chars().count() == 2 {
            region = script;
            script = None;
        }
    }
    (lang, script, region)
}

fn raw_maximize_bench(c: &mut Criterion) {
    let entries: Vec<(Option<TinyStr8>, Option<TinyStr4>, Option<TinyStr4>)> =
        STRINGS.iter().map(|s| extract_input(s)).collect();

    c.bench_function("raw_maximize", move |b| {
        b.iter(|| {
            for (lang, script, region) in &entries {
                let _ = unic_langid_impl::likelysubtags::maximize(
                    lang.clone(),
                    script.clone(),
                    region.clone(),
                );
            }
        })
    });
}

criterion_group!(benches, maximize_bench, raw_maximize_bench,);
criterion_main!(benches);