Skip to content

Commit

Permalink
Rust like lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
zbraniecki committed Jun 15, 2020
1 parent 22166f4 commit b1fa8b6
Show file tree
Hide file tree
Showing 14 changed files with 1,682 additions and 220 deletions.
6 changes: 5 additions & 1 deletion components/pluralrules/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ include = [
]

[dependencies]
icu-locale = { path = "../locale" }
icu-locale = { path = "../locale", features=["serde"] }
serde = { version = "1.0", optional = true, features = ["derive"] }
serde_json = {version = "1.0", optional = true }

[dev-dependencies]
criterion = "0.3"
cldr_pluralrules_parser = "2.0.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = {version = "1.0" }

[[bench]]
name = "pluralrules"
Expand Down
121 changes: 44 additions & 77 deletions components/pluralrules/benches/pluralrules.rs
Original file line number Diff line number Diff line change
@@ -1,115 +1,82 @@
use criterion::black_box;
use criterion::criterion_group;
use criterion::criterion_main;
use criterion::BenchmarkId;
use criterion::Criterion;
use std::convert::TryInto;

use cldr_pluralrules_parser::{parse_plural_condition, parse_plural_rule};
use icu_locale::LanguageIdentifier;
use icu_pluralrules::operands::PluralOperands;
use icu_pluralrules::rules::lexer::Lexer;
use icu_pluralrules::rules::parser::Parser;
use icu_pluralrules::rules::resolver::matches;
use icu_pluralrules::{PluralCategory, PluralRuleType, PluralRules};

// let langs = &["uk", "de", "sk", "ar", "fr", "it", "en", "cs", "es", "zh"];
// const SAMPLES: &[isize] = &[
// 1, 2, 3, 4, 5, 25, 134, 910293019, 12, 1412, -12, 15, 2931, 31231, 3123, 13231, 91, 0, 231,
// -2, -45, 33, 728, 2, 291, 24, 479, 291, 778, 919, 93,
// ];
const SAMPLES: &[isize] = &[
1, 2, 3, 4, 5, 25, 134, 910293019, 12, 1412, -12, 15, 2931, 31231, 3123, 13231, 91, 0, 231, -2,
-45, 33, 728, 2, 291, 24, 479, 291, 778, 919, 93,
];

const STRINGS: &[&str] = &[
"i = 1",
"n % 10 = 1 and n % 100 != 11",
"n % 10 = 2..4 and n % 100 != 12..14",
"n = 0,1 or i = 0 and f = 1",
const PL_DATA: &[(PluralCategory, &'static str)] = &[
(PluralCategory::One, "i = 1 and v = 0"),
(PluralCategory::Few, "v = 0 and i % 10 = 2..4 and i % 100 != 12..14"),
(PluralCategory::Many, "v = 0 and i != 1 and i % 10 = 0..1 or v = 0 and i % 10 = 5..9 or v = 0 and i % 100 = 12..14"),
(PluralCategory::Other, ""),
];

fn plural_rules(c: &mut Criterion) {
c.bench_function("lex", |b| {
b.iter(|| {
for s in STRINGS {
for (_, s) in PL_DATA {
let lexer = Lexer::new(black_box(s.as_bytes()));
let _ = lexer.collect::<Vec<_>>();
let _ = lexer.count();
}
})
});
c.bench_function("parse", |b| {
b.iter(|| {
for s in STRINGS {
for (_, s) in PL_DATA {
let parser = Parser::new(black_box(s.as_bytes()));
let _ = parser.parse();
}
})
});
c.bench_function("parse_old", |b| {
b.iter(|| {
for s in STRINGS {
let _ = parse_plural_rule(black_box(s)).expect("Parsing succeeded");
for (_, s) in PL_DATA {
let _ = parse_plural_condition(black_box(s)).expect("Parsing succeeded");
}
})
});
c.bench_function("matches", |b| {
c.bench_function("select", |b| {
let loc: LanguageIdentifier = "pl".parse().unwrap();
let pr = PluralRules::try_new(loc, PluralRuleType::Cardinal).unwrap();
b.iter(|| {
for s in STRINGS {
let parser = Parser::new(black_box(s.as_bytes()));
let ast = parser.parse().unwrap();
let _ = matches(&ast, &1u64.into());
for s in SAMPLES {
let op: PluralOperands = (*s).try_into().unwrap();
let _ = pr.select(op);
}
})
});
// let langs = &["uk", "de", "sk", "ar", "fr", "it", "en", "cs", "es", "zh"];
// let langs: Vec<LanguageIdentifier> = langs
// .iter()
// .map(|l| l.parse().expect("Parsing failed"))
// .collect();
//
// c.bench_with_input(
// BenchmarkId::new("construct", langs.len()),
// &langs,
// |b, langs| {
// b.iter(|| {
// for lang in langs {
// PluralRules::create(lang.clone(), PluralRuleType::ORDINAL).unwrap();
// PluralRules::create(lang.clone(), PluralRuleType::CARDINAL).unwrap();
// }
// });
// },
// );
//
// let samples = &[
// 1, 2, 3, 4, 5, 25, 134, 910293019, 12, 1412, -12, 15, 2931, 31231, 3123, 13231, 91, 0, 231,
// -2, -45, 33, 728, 2, 291, 24, 479, 291, 778, 919, 93,
// ];
//
// let langid_pl = langid!("pl");
// let ipr = PluralRules::create(langid_pl.clone(), PluralRuleType::CARDINAL).unwrap();
//
// c.bench_with_input(
// BenchmarkId::new("select", samples.len()),
// samples,
// |b, samples| {
// b.iter(|| {
// for value in samples {
// ipr.select(*value).unwrap();
// }
// });
// },
// );
//
// c.bench_function("total", |b| {
// b.iter(|| {
// let ipr = PluralRules::create(langid_pl.clone(), PluralRuleType::CARDINAL).unwrap();
// ipr.select(1).unwrap();
// ipr.select(2).unwrap();
// ipr.select(3).unwrap();
// ipr.select(4).unwrap();
// ipr.select(5).unwrap();
// ipr.select(25).unwrap();
// ipr.select(134).unwrap();
// ipr.select(5090).unwrap();
// ipr.select(910293019).unwrap();
// ipr.select(5.2).unwrap();
// ipr.select(-0.2).unwrap();
// ipr.select("12.06").unwrap();
// })
// });

let langs = &["uk", "de", "sk", "ar", "fr", "it", "en", "cs", "es", "zh"];
let langs: Vec<LanguageIdentifier> = langs
.iter()
.map(|l| l.parse().expect("Parsing failed"))
.collect();

c.bench_with_input(
BenchmarkId::new("construct", langs.len()),
&langs,
|b, langs| {
b.iter(|| {
for lang in langs {
PluralRules::try_new(lang.clone(), PluralRuleType::Ordinal).unwrap();
PluralRules::try_new(lang.clone(), PluralRuleType::Cardinal).unwrap();
}
});
},
);
}

criterion_group!(benches, plural_rules,);
Expand Down
Loading

0 comments on commit b1fa8b6

Please sign in to comment.