diff options
| author | Giles Cope <gilescope@gmail.com> | 2021-02-06 19:14:13 +0000 | 
|---|---|---|
| committer | Giles Cope <gilescope@gmail.com> | 2021-02-06 19:56:43 +0000 | 
| commit | f165f49d228d2582d2dbfd588c2729cfc9585eb0 (patch) | |
| tree | 8b66f6e762778f935c76a176b92b41cb8863c3c5 | |
| parent | 9a9477fada5baf69d693e717d6df902e411a73d6 (diff) | |
| download | rust-f165f49d228d2582d2dbfd588c2729cfc9585eb0.tar.gz rust-f165f49d228d2582d2dbfd588c2729cfc9585eb0.zip  | |
Slight perf improvement on char::to_ascii_lowercase
| -rw-r--r-- | library/core/benches/char/methods.rs | 10 | ||||
| -rw-r--r-- | library/core/src/char/methods.rs | 6 | 
2 files changed, 14 insertions, 2 deletions
diff --git a/library/core/benches/char/methods.rs b/library/core/benches/char/methods.rs index a9a08a4d762..de4b63030fa 100644 --- a/library/core/benches/char/methods.rs +++ b/library/core/benches/char/methods.rs @@ -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()) +} diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index 2baea7842a7..4c28d9cd673 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -1090,7 +1090,8 @@ 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 } + // 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 @@ 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 } + // 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.  | 
