Skip to content

Increase the #[inline] opportunities - 15-40% performance improvements #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 2, 2021
Merged
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ no_std = [] # This is a no-op, preserved for backward compatibility only.

[dev-dependencies]
quickcheck = "0.7"
bencher = "0.1"
criterion = "0.3"

[[bench]]
name = "graphemes"
Expand Down
59 changes: 29 additions & 30 deletions benches/graphemes.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,54 @@
#[macro_use]
extern crate bencher;
extern crate unicode_segmentation;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use unicode_segmentation;

use bencher::Bencher;
use unicode_segmentation::UnicodeSegmentation;
use std::fs;
use unicode_segmentation::UnicodeSegmentation;

fn graphemes(bench: &mut Bencher, path: &str) {
fn graphemes(c: &mut Criterion, lang: &str, path: &str) {
let text = fs::read_to_string(path).unwrap();
bench.iter(|| {
for g in UnicodeSegmentation::graphemes(&*text, true) {
bencher::black_box(g);
}
});

bench.bytes = text.len() as u64;
c.bench_function(&format!("graphemes_{}",lang), |bench| {
bench.iter(|| {
for g in UnicodeSegmentation::graphemes(black_box(&*text), true) {
black_box(g);
}
})
});
}

fn graphemes_arabic(bench: &mut Bencher) {
graphemes(bench, "benches/texts/arabic.txt");
fn graphemes_arabic(c: &mut Criterion) {
graphemes(c, "arabic" ,"benches/texts/arabic.txt");
}

fn graphemes_english(bench: &mut Bencher) {
graphemes(bench, "benches/texts/english.txt");
fn graphemes_english(c: &mut Criterion) {
graphemes(c, "english" ,"benches/texts/english.txt");
}

fn graphemes_hindi(bench: &mut Bencher) {
graphemes(bench, "benches/texts/hindi.txt");
fn graphemes_hindi(c: &mut Criterion) {
graphemes(c, "hindi" ,"benches/texts/hindi.txt");
}

fn graphemes_japanese(bench: &mut Bencher) {
graphemes(bench, "benches/texts/japanese.txt");
fn graphemes_japanese(c: &mut Criterion) {
graphemes(c, "japanese" ,"benches/texts/japanese.txt");
}

fn graphemes_korean(bench: &mut Bencher) {
graphemes(bench, "benches/texts/korean.txt");
fn graphemes_korean(c: &mut Criterion) {
graphemes(c, "korean" ,"benches/texts/korean.txt");
}

fn graphemes_mandarin(bench: &mut Bencher) {
graphemes(bench, "benches/texts/mandarin.txt");
fn graphemes_mandarin(c: &mut Criterion) {
graphemes(c, "mandarin" ,"benches/texts/mandarin.txt");
}

fn graphemes_russian(bench: &mut Bencher) {
graphemes(bench, "benches/texts/russian.txt");
fn graphemes_russian(c: &mut Criterion) {
graphemes(c, "russian" ,"benches/texts/russian.txt");
}

fn graphemes_source_code(bench: &mut Bencher) {
graphemes(bench, "benches/texts/source_code.txt");
fn graphemes_source_code(c: &mut Criterion) {
graphemes(c, "source_code","benches/texts/source_code.txt");
}

benchmark_group!(
criterion_group!(
benches,
graphemes_arabic,
graphemes_english,
Expand All @@ -61,4 +60,4 @@ benchmark_group!(
graphemes_source_code,
);

benchmark_main!(benches);
criterion_main!(benches);
8 changes: 8 additions & 0 deletions src/grapheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ enum PairResult {
Emoji, // a break if preceded by emoji base and (Extend)*
}

#[inline]
fn check_pair(before: GraphemeCat, after: GraphemeCat) -> PairResult {
use crate::tables::grapheme::GraphemeCat::*;
use self::PairResult::*;
Expand Down Expand Up @@ -407,6 +408,7 @@ impl GraphemeCursor {
}
}

#[inline]
fn decide(&mut self, is_break: bool) {
self.state = if is_break {
GraphemeState::Break
Expand All @@ -415,11 +417,13 @@ impl GraphemeCursor {
};
}

#[inline]
fn decision(&mut self, is_break: bool) -> Result<bool, GraphemeIncomplete> {
self.decide(is_break);
Ok(is_break)
}

#[inline]
fn is_boundary_result(&self) -> Result<bool, GraphemeIncomplete> {
if self.state == GraphemeState::Break {
Ok(true)
Expand All @@ -432,6 +436,7 @@ impl GraphemeCursor {
}
}

#[inline]
fn handle_regional(&mut self, chunk: &str, chunk_start: usize) {
use crate::tables::grapheme as gr;
let mut ris_count = self.ris_count.unwrap_or(0);
Expand All @@ -452,6 +457,7 @@ impl GraphemeCursor {
self.state = GraphemeState::Regional;
}

#[inline]
fn handle_emoji(&mut self, chunk: &str, chunk_start: usize) {
use crate::tables::grapheme as gr;
let mut iter = chunk.chars().rev();
Expand Down Expand Up @@ -482,6 +488,7 @@ impl GraphemeCursor {
self.state = GraphemeState::Emoji;
}

#[inline]
Copy link

Choose a reason for hiding this comment

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

this should probably go below the docs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. I thought I saw this pattern somewhere else in the crate and decided to follow. Will submit a follow-up PR that moves all of them below the docs.

Copy link
Member

Choose a reason for hiding this comment

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

sounds good!

/// Determine whether the current cursor location is a grapheme cluster boundary.
/// Only a part of the string need be supplied. If `chunk_start` is nonzero or
/// the length of `chunk` is not equal to `len` on creation, then this method
Expand Down Expand Up @@ -563,6 +570,7 @@ impl GraphemeCursor {
}
}

#[inline]
Copy link

Choose a reason for hiding this comment

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

same

/// Find the next boundary after the current cursor position. Only a part of
/// the string need be supplied. If the chunk is incomplete, then this
/// method might return `GraphemeIncomplete::PreContext` or
Expand Down