Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions components/properties/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ workspace = true

[lints]
workspace = true

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
criterion = { workspace = true }

[lib]
bench = false # This option is required for Benchmark CI

[[bench]]
name = "properties_bench"
harness = false
64 changes: 64 additions & 0 deletions components/properties/benches/properties_bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use criterion::{black_box, criterion_group, criterion_main, Criterion};

use icu_properties::props::{Alphabetic, GeneralCategory, Script};
use icu_properties::{CodePointMapData, CodePointSetData};

const SAMPLE_STRING_MIXED: &str = "Hello, 世界! 🎃 ЗАГАЛЬНА";

fn one_hundred_code_points(sample_str: &str) -> String {
sample_str.chars().cycle().take(100).collect()
}

fn set_benchmarks(c: &mut Criterion) {
let s = one_hundred_code_points(SAMPLE_STRING_MIXED);
let alpha = CodePointSetData::new::<Alphabetic>();

c.bench_function("icu_properties/set/contains", |b| {
b.iter(|| {
black_box(&s)
.chars()
.filter(|ch| black_box(&alpha).contains(*ch))
.count()
})
});

c.bench_function("icu_properties/set/iter_ranges", |b| {
b.iter(|| black_box(&alpha).iter_ranges().count())
});
}

fn map_benchmarks(c: &mut Criterion) {
let s = one_hundred_code_points(SAMPLE_STRING_MIXED);

let gc = CodePointMapData::<GeneralCategory>::new();
c.bench_function("icu_properties/map/get/general_category", |b| {
b.iter(|| {
black_box(&s).chars().for_each(|ch| {
black_box(black_box(&gc).get(ch));
})
})
});

let script = CodePointMapData::<Script>::new();
c.bench_function("icu_properties/map/get/script", |b| {
b.iter(|| {
black_box(&s).chars().for_each(|ch| {
black_box(black_box(&script).get(ch));
})
})
});

c.bench_function("icu_properties/map/get_set_for_value", |b| {
b.iter(|| {
let set = black_box(&gc).get_set_for_value(GeneralCategory::UppercaseLetter);
set.as_borrowed().contains('A')
})
});
}

criterion_group!(benches, set_benchmarks, map_benchmarks);
criterion_main!(benches);