Skip to content

Commit f165f49

Browse files
committed
Slight perf improvement on char::to_ascii_lowercase
1 parent 9a9477f commit f165f49

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

library/core/benches/char/methods.rs

+10
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,13 @@ fn bench_to_digit_radix_var(b: &mut Bencher) {
3535
.min()
3636
})
3737
}
38+
39+
#[bench]
40+
fn bench_to_ascii_uppercase(b: &mut Bencher) {
41+
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_ascii_uppercase()).min())
42+
}
43+
44+
#[bench]
45+
fn bench_to_ascii_lowercase(b: &mut Bencher) {
46+
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_ascii_lowercase()).min())
47+
}

library/core/src/char/methods.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,8 @@ impl char {
10901090
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
10911091
#[inline]
10921092
pub fn to_ascii_uppercase(&self) -> char {
1093-
if self.is_ascii() { (*self as u8).to_ascii_uppercase() as char } else { *self }
1093+
// 6th bit dictates ascii case.
1094+
if self.is_ascii_lowercase() { ((*self as u8) & !0b10_0000u8) as char } else { *self }
10941095
}
10951096

10961097
/// Makes a copy of the value in its ASCII lower case equivalent.
@@ -1118,7 +1119,8 @@ impl char {
11181119
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
11191120
#[inline]
11201121
pub fn to_ascii_lowercase(&self) -> char {
1121-
if self.is_ascii() { (*self as u8).to_ascii_lowercase() as char } else { *self }
1122+
// 6th bit dictates ascii case.
1123+
if self.is_ascii_uppercase() { ((*self as u8) | 0b10_0000u8) as char } else { *self }
11221124
}
11231125

11241126
/// Checks that two values are an ASCII case-insensitive match.

0 commit comments

Comments
 (0)