Skip to content
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

Add a check for ASCII characters in to_upper and to_lower #81358

Merged
merged 3 commits into from
Mar 17, 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
30 changes: 30 additions & 0 deletions library/core/benches/char/methods.rs
Original file line number Diff line number Diff line change
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())

This comment was marked as outdated.

Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to say anything about benchmarks results on mixed and non-ascii text? I expect branch prediction would mean that on pure non-ascii, there is virtually no change(?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would expect so. What do you think of adding two more benchmarks for a total of three?

  • all ASCII characters
  • no ASCII characters
  • 50/50 mixed characters

Copy link
Contributor

Choose a reason for hiding this comment

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

We need to be slightly careful here. The non-ascii benchmark is always one byte unicode is it not? It is non-ascii but above 192 it's going to be indicating another code point so there's going to be pleanty of malformed unicode in here.
It might be slightly better to create a random slice of u8 and then use String::from_utf8_lossy() to make it valid first?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since it's a benchmark I would want to make the "random chars" constant so runs can be compared.

What do you think of this? Playground Each test case can use the same list of chars and filter for what it's testing for (where mixed does no filtering).

Copy link
Contributor

Choose a reason for hiding this comment

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

Random was the wrong choice of words here, sorry! It looks like a fair few other benchmarks / tests don't worry about putting in malformed utf sequences so I think it's fine to define the benchmarks this way for now.

}

#[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
Original file line number Diff line number Diff line change
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