Skip to content

Add segmenter comparison benchmarks#8141

Open
Xuanwo wants to merge 3 commits into
unicode-org:mainfrom
Xuanwo:xuanwo/icu-segmenter-benchmarks
Open

Add segmenter comparison benchmarks#8141
Xuanwo wants to merge 3 commits into
unicode-org:mainfrom
Xuanwo:xuanwo/icu-segmenter-benchmarks

Conversation

@Xuanwo

@Xuanwo Xuanwo commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Changelog

No changelog entry is required: this PR only changes benchmark code.

Summary

This PR publishes segmenter comparison benchmarks without changing segmenter behavior or public APIs.

The benchmark matrix covers word and line segmentation across segment_str, segment_utf8, and segment_utf16. It includes English, Thai, Japanese, Han, Thai+Japanese, and Thai+Han inputs so follow-up optimization PRs can separately validate dictionary and complex-language traversal changes.

By default, the benchmark compiles and runs the published segmenter implementation. With the unstable feature enabled, it also registers matching neo benchmarks. The comparison matrix includes auto, lstm, and dictionary constructor variants, plus line-break CSS option cases for English.

The benchmarks consume iterators with a checksum instead of collecting breakpoints, so result collection allocation is not part of the measured path.

How to run

Compile the default published-segmenter benchmark:

cargo bench -p icu_segmenter --bench bench --no-run

Compile the published-vs-neo comparison benchmark:

cargo bench -p icu_segmenter --features unstable --bench bench --no-run

Run the comparison benchmark:

cargo bench -p icu_segmenter --features unstable --bench bench

For a quick smoke run while iterating:

cargo bench -p icu_segmenter --features unstable --bench bench -- --sample-size 10 --warm-up-time 0.01 --measurement-time 0.01

Follow-up PRs for segmenter optimizations can use the stable Criterion names under Segmenter/{line,word}/{segment_str,segment_utf8,segment_utf16}/{published,neo}_{auto,lstm,dictionary}/{case} and Segmenter/line/segment_{latin1,str,utf8,utf16}/{published,neo}_dictionary_css/english to compare improvements independently.

Comment thread components/segmenter/benches/bench.rs Outdated
const TEXT_JAPANESE: &str =
"こんにちは世界こんにちは世界こんにちは世界こんにちは世界こんにちは世界こんにちは世界";
const TEXT_HAN: &str = "中文分词需要词典中文分词需要词典中文分词需要词典中文分词需要词典";
const TEXT_THAI_JAPANESE: &str = "ภาษาไทยこんにちは世界ภาษาไทยこんにちは世界ภาษาไทยこんにちは世界";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

complex mixes are good, but also add mixed complex/non-complex text

Comment thread components/segmenter/benches/bench.rs Outdated
});

group.bench_function("En CSS", |b| {
group.bench_function("published_dictionary_css/english", |b| {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the new names are nicer, but please don't rename benchmarks, it will break our graphs

Comment thread components/segmenter/benches/bench.rs Outdated
const TEST_STR_TH: &str =
"ภาษาไทยภาษาไทย ภาษาไทยภาษาไทย ภาษาไทยภาษาไทย ภาษาไทยภาษาไทย ภาษาไทยภาษาไทย ภาษาไทยภาษาไทย";
const TEXT_ENGLISH: &str = "Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.";
const TEXT_THAI: &str =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you probably want longer texts to really notice allocations. like a whole wikipedia article or something.

you can check text files into tests/testdata and then load them here

Comment thread components/segmenter/benches/bench.rs Outdated
const TEST_STR_EN: &str = "Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.";
const TEST_STR_TH: &str =
"ภาษาไทยภาษาไทย ภาษาไทยภาษาไทย ภาษาไทยภาษาไทย ภาษาไทยภาษาไทย ภาษาไทยภาษาไทย ภาษาไทยภาษาไทย";
const TEXT_ENGLISH: &str = "Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please try to reduce diffs. there's no reason for renaming this constant or removing the comment

Comment thread components/segmenter/benches/bench.rs Outdated

group.bench_function("published_dictionary/english", |b| {
b.iter(|| {
consume_breakpoints(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the reason for replacing count by consume_breakpoints?

@Xuanwo Xuanwo marked this pull request as ready for review July 3, 2026 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants