]> git.lizzy.rs Git - rust.git/commitdiff
Slight perf improvement on char::to_ascii_lowercase
authorGiles Cope <gilescope@gmail.com>
Sat, 6 Feb 2021 19:14:13 +0000 (19:14 +0000)
committerGiles Cope <gilescope@gmail.com>
Sat, 6 Feb 2021 19:56:43 +0000 (19:56 +0000)
library/core/benches/char/methods.rs
library/core/src/char/methods.rs

index a9a08a4d76200139d342476ee39262854784a0e3..de4b63030fa7c5432becf49037fb200895f86653 100644 (file)
@@ -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())
+}
index 2baea7842a796ed12293d701b8e872f66d0d49d7..4c28d9cd673af56f336fbc8144b17aa978af4f4f 100644 (file)
@@ -1090,7 +1090,8 @@ pub const fn is_ascii(&self) -> bool {
     #[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 }
+        // 6th bit dictates ascii case.
+        if self.is_ascii_lowercase() { ((*self as u8) & !0b10_0000u8) as char } else { *self }
     }
 
     /// Makes a copy of the value in its ASCII lower case equivalent.
@@ -1118,7 +1119,8 @@ pub fn to_ascii_uppercase(&self) -> 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 }
+        // 6th bit dictates ascii case.
+        if self.is_ascii_uppercase() { ((*self as u8) | 0b10_0000u8) as char } else { *self }
     }
 
     /// Checks that two values are an ASCII case-insensitive match.