Skip to content

Commit

Permalink
Auto merge of #81358 - mcastorina:to-upper-lower-speed, r=joshtriplett
Browse files Browse the repository at this point in the history
Add a check for ASCII characters in to_upper and to_lower

This extra check has better performance. See discussion here:
https://internals.rust-lang.org/t/to-upper-speed/13896

Thanks to `@gilescope` for helping discover and test this.
  • Loading branch information
bors committed Mar 17, 2021
2 parents 2c74903 + 229fdf8 commit 0ce0fed
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
30 changes: 30 additions & 0 deletions library/core/benches/char/methods.rs
Expand Up @@ -45,3 +45,33 @@ fn bench_to_ascii_uppercase(b: &mut Bencher) {
fn bench_to_ascii_lowercase(b: &mut Bencher) {
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_ascii_lowercase()).min())
}

#[bench]
fn bench_ascii_mix_to_uppercase(b: &mut Bencher) {
b.iter(|| (0..=255).cycle().take(10_000).map(|b| char::from(b).to_uppercase()).count())
}

#[bench]
fn bench_ascii_mix_to_lowercase(b: &mut Bencher) {
b.iter(|| (0..=255).cycle().take(10_000).map(|b| char::from(b).to_lowercase()).count())
}

#[bench]
fn bench_ascii_char_to_uppercase(b: &mut Bencher) {
b.iter(|| (0..=127).cycle().take(10_000).map(|b| char::from(b).to_uppercase()).count())
}

#[bench]
fn bench_ascii_char_to_lowercase(b: &mut Bencher) {
b.iter(|| (0..=127).cycle().take(10_000).map(|b| char::from(b).to_lowercase()).count())
}

#[bench]
fn bench_non_ascii_char_to_uppercase(b: &mut Bencher) {
b.iter(|| (128..=255).cycle().take(10_000).map(|b| char::from(b).to_uppercase()).count())
}

#[bench]
fn bench_non_ascii_char_to_lowercase(b: &mut Bencher) {
b.iter(|| (128..=255).cycle().take(10_000).map(|b| char::from(b).to_lowercase()).count())
}
20 changes: 14 additions & 6 deletions library/core/src/unicode/unicode_data.rs
Expand Up @@ -549,16 +549,24 @@ pub mod white_space {
#[rustfmt::skip]
pub mod conversions {
pub fn to_lower(c: char) -> [char; 3] {
match bsearch_case_table(c, LOWERCASE_TABLE) {
None => [c, '\0', '\0'],
Some(index) => LOWERCASE_TABLE[index].1,
if c.is_ascii() {
[(c as u8).to_ascii_lowercase() as char, '\0', '\0']
} else {
match bsearch_case_table(c, LOWERCASE_TABLE) {
None => [c, '\0', '\0'],
Some(index) => LOWERCASE_TABLE[index].1,
}
}
}

pub fn to_upper(c: char) -> [char; 3] {
match bsearch_case_table(c, UPPERCASE_TABLE) {
None => [c, '\0', '\0'],
Some(index) => UPPERCASE_TABLE[index].1,
if c.is_ascii() {
[(c as u8).to_ascii_uppercase() as char, '\0', '\0']
} else {
match bsearch_case_table(c, UPPERCASE_TABLE) {
None => [c, '\0', '\0'],
Some(index) => UPPERCASE_TABLE[index].1,
}
}
}

Expand Down

0 comments on commit 0ce0fed

Please sign in to comment.