Skip to content

Commit 4cfcae1

Browse files
authored
Rollup merge of rust-lang#94112 - digama0:patch-3, r=scottmcm
Optimize char_try_from_u32 The optimization was proposed by ```````@falk-hueffner``````` in https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Micro-optimizing.20char.3A.3Afrom_u32/near/272146171, and I simplified it a bit and added an explanation of why the optimization is correct. The generated code is 2 instructions shorter and uses 2 registers instead of 4 on x86.
2 parents aab5597 + 9825070 commit 4cfcae1

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

core/src/char/convert.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use crate::fmt;
66
use crate::mem::transmute;
77
use crate::str::FromStr;
88

9-
use super::MAX;
10-
119
/// Converts a `u32` to a `char`.
1210
///
1311
/// Note that all [`char`]s are valid [`u32`]s, and can be cast to one with
@@ -271,7 +269,20 @@ impl FromStr for char {
271269

272270
#[inline]
273271
const fn char_try_from_u32(i: u32) -> Result<char, CharTryFromError> {
274-
if (i > MAX as u32) || (i >= 0xD800 && i <= 0xDFFF) {
272+
// This is an optimized version of the check
273+
// (i > MAX as u32) || (i >= 0xD800 && i <= 0xDFFF),
274+
// which can also be written as
275+
// i >= 0x110000 || (i >= 0xD800 && i < 0xE000).
276+
//
277+
// The XOR with 0xD800 permutes the ranges such that 0xD800..0xE000 is
278+
// mapped to 0x0000..0x0800, while keeping all the high bits outside 0xFFFF the same.
279+
// In particular, numbers >= 0x110000 stay in this range.
280+
//
281+
// Subtracting 0x800 causes 0x0000..0x0800 to wrap, meaning that a single
282+
// unsigned comparison against 0x110000 - 0x800 will detect both the wrapped
283+
// surrogate range as well as the numbers originally larger than 0x110000.
284+
//
285+
if (i ^ 0xD800).wrapping_sub(0x800) >= 0x110000 - 0x800 {
275286
Err(CharTryFromError(()))
276287
} else {
277288
// SAFETY: checked that it's a legal unicode value

0 commit comments

Comments
 (0)