Debian Codegraph @

debian/rust-vendor/icu_plurals/benches/parser.rs

raw
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 mod fixtures;
6
7 use criterion::{Criterion, black_box, criterion_group, criterion_main};
8
9 use icu_provider::prelude::*;
10
11 fn parser(c: &mut Criterion) {
12 use icu_plurals::provider::rules::reference::parse_condition;
13
14 let fixture_data =
15 serde_json::from_str::<fixtures::PluralsFixture>(include_str!("fixtures/plurals.json"))
16 .expect("Failed to read a fixture");
17
18 let mut rules = vec![];
19
20 for langid in fixture_data.langs {
21 let response: DataResponse<icu_plurals::provider::PluralsCardinalV1> =
22 icu_plurals::provider::Baked
23 .load(DataRequest {
24 id: DataIdentifierBorrowed::for_locale(&langid.into()),
25 ..Default::default()
26 })
27 .unwrap();
28 let data = response.payload.get();
29
30 let r = [
31 data.zero.as_ref(),
32 data.one.as_ref(),
33 data.two.as_ref(),
34 data.few.as_ref(),
35 data.many.as_ref(),
36 ];
37
38 for x in r.iter().flatten() {
39 rules.push(x.to_string());
40 }
41 }
42
43 c.bench_function("plurals/parser/overview", |b| {
44 b.iter(|| {
45 for rule in &rules {
46 let _ = parse_condition(black_box(rule.as_bytes()));
47 }
48 })
49 });
50
51 c.bench_function("plurals/parser/lex", |b| {
52 use icu_plurals::provider::rules::reference::Lexer;
53 b.iter(|| {
54 for rule in &rules {
55 let _ = Lexer::new(black_box(rule.as_bytes())).count();
56 }
57 })
58 });
59 }
60
61 criterion_group!(benches, parser,);
62 criterion_main!(benches);