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

Slight perf improvement on char::to_ascii_lowercase #81837

Merged
merged 5 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 4 additions & 2 deletions library/core/benches/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ macro_rules! benches {
use test::black_box;
use test::Bencher;

const ASCII_CASE_MASK: u8 = 0b0010_0000;
gilescope marked this conversation as resolved.
Show resolved Hide resolved

benches! {
fn case00_alloc_only(_bytes: &mut [u8]) {}

Expand Down Expand Up @@ -204,7 +206,7 @@ benches! {
}
}
for byte in bytes {
*byte &= !((is_ascii_lowercase(*byte) as u8) << 5)
*byte &= !((is_ascii_lowercase(*byte) as u8) * ASCII_CASE_MASK)
}
}

Expand All @@ -216,7 +218,7 @@ benches! {
}
}
for byte in bytes {
*byte -= (is_ascii_lowercase(*byte) as u8) << 5
*byte -= (is_ascii_lowercase(*byte) as u8) * ASCII_CASE_MASK
}
}

Expand Down
10 changes: 10 additions & 0 deletions library/core/benches/char/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@ fn bench_to_digit_radix_var(b: &mut Bencher) {
.min()
})
}

#[bench]
fn bench_to_ascii_uppercase(b: &mut Bencher) {
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_ascii_uppercase()).min())
}

#[bench]
fn bench_to_ascii_lowercase(b: &mut Bencher) {
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_ascii_lowercase()).min())
}
6 changes: 3 additions & 3 deletions library/core/src/char/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::slice;
use crate::str::from_utf8_unchecked_mut;
use crate::unicode::printable::is_printable;
use crate::unicode::{self, conversions};
use crate::unicode::{self, conversions, ASCII_CASE_MASK};

use super::*;

Expand Down Expand Up @@ -1090,7 +1090,7 @@ impl char {
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[inline]
pub fn to_ascii_uppercase(&self) -> char {
if self.is_ascii() { (*self as u8).to_ascii_uppercase() as char } else { *self }
if self.is_ascii_lowercase() { ((*self as u8) & !ASCII_CASE_MASK) as char } else { *self }
gilescope marked this conversation as resolved.
Show resolved Hide resolved
}

/// Makes a copy of the value in its ASCII lower case equivalent.
Expand Down Expand Up @@ -1118,7 +1118,7 @@ impl char {
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[inline]
pub fn to_ascii_lowercase(&self) -> char {
if self.is_ascii() { (*self as u8).to_ascii_lowercase() as char } else { *self }
if self.is_ascii_uppercase() { ((*self as u8) | ASCII_CASE_MASK) as char } else { *self }
gilescope marked this conversation as resolved.
Show resolved Hide resolved
}

/// Checks that two values are an ASCII case-insensitive match.
Expand Down
5 changes: 3 additions & 2 deletions library/core/src/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use crate::intrinsics;
use crate::mem;
use crate::str::FromStr;
use crate::unicode::ASCII_CASE_MASK;

// Used because the `?` operator is not allowed in a const context.
macro_rules! try_opt {
Expand Down Expand Up @@ -195,7 +196,7 @@ impl u8 {
#[inline]
pub fn to_ascii_uppercase(&self) -> u8 {
// Unset the fifth bit if this is a lowercase letter
*self & !((self.is_ascii_lowercase() as u8) << 5)
*self & !((self.is_ascii_lowercase() as u8) * ASCII_CASE_MASK)
}

/// Makes a copy of the value in its ASCII lower case equivalent.
Expand All @@ -218,7 +219,7 @@ impl u8 {
#[inline]
pub fn to_ascii_lowercase(&self) -> u8 {
// Set the fifth bit if this is an uppercase letter
*self | ((self.is_ascii_uppercase() as u8) << 5)
*self | (self.is_ascii_uppercase() as u8 * ASCII_CASE_MASK)
}

/// Checks that two values are an ASCII case-insensitive match.
Expand Down
3 changes: 3 additions & 0 deletions library/core/src/unicode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ mod unicode_data;
#[stable(feature = "unicode_version", since = "1.45.0")]
pub const UNICODE_VERSION: (u8, u8, u8) = unicode_data::UNICODE_VERSION;

/// If 6th bit set ascii is upper case.
pub(crate) const ASCII_CASE_MASK: u8 = 0b0010_0000;

// For use in liballoc, not re-exported in libstd.
pub use unicode_data::{
case_ignorable::lookup as Case_Ignorable, cased::lookup as Cased, conversions,
Expand Down