Skip to content

Commit 17f08fe

Browse files
committed
core/char: Speed up to_digit() for radix <= 10
### Before ``` # Run 1 test char::methods::bench_to_digit_radix_10 ... bench: 16,265 ns/iter (+/- 1,774) test char::methods::bench_to_digit_radix_16 ... bench: 13,938 ns/iter (+/- 2,479) test char::methods::bench_to_digit_radix_2 ... bench: 13,090 ns/iter (+/- 524) test char::methods::bench_to_digit_radix_36 ... bench: 14,236 ns/iter (+/- 1,949) # Run 2 test char::methods::bench_to_digit_radix_10 ... bench: 16,176 ns/iter (+/- 1,589) test char::methods::bench_to_digit_radix_16 ... bench: 13,896 ns/iter (+/- 3,140) test char::methods::bench_to_digit_radix_2 ... bench: 13,158 ns/iter (+/- 1,112) test char::methods::bench_to_digit_radix_36 ... bench: 14,206 ns/iter (+/- 1,312) # Run 3 test char::methods::bench_to_digit_radix_10 ... bench: 16,221 ns/iter (+/- 2,423) test char::methods::bench_to_digit_radix_16 ... bench: 14,361 ns/iter (+/- 3,926) test char::methods::bench_to_digit_radix_2 ... bench: 13,097 ns/iter (+/- 671) test char::methods::bench_to_digit_radix_36 ... bench: 14,388 ns/iter (+/- 1,068) ``` ### After ``` # Run 1 test char::methods::bench_to_digit_radix_10 ... bench: 11,521 ns/iter (+/- 552) test char::methods::bench_to_digit_radix_16 ... bench: 12,926 ns/iter (+/- 684) test char::methods::bench_to_digit_radix_2 ... bench: 11,266 ns/iter (+/- 1,085) test char::methods::bench_to_digit_radix_36 ... bench: 14,213 ns/iter (+/- 614) # Run 2 test char::methods::bench_to_digit_radix_10 ... bench: 11,424 ns/iter (+/- 1,042) test char::methods::bench_to_digit_radix_16 ... bench: 12,854 ns/iter (+/- 1,193) test char::methods::bench_to_digit_radix_2 ... bench: 11,193 ns/iter (+/- 716) test char::methods::bench_to_digit_radix_36 ... bench: 14,249 ns/iter (+/- 3,514) # Run 3 test char::methods::bench_to_digit_radix_10 ... bench: 11,469 ns/iter (+/- 685) test char::methods::bench_to_digit_radix_16 ... bench: 12,852 ns/iter (+/- 568) test char::methods::bench_to_digit_radix_2 ... bench: 11,275 ns/iter (+/- 1,356) test char::methods::bench_to_digit_radix_36 ... bench: 14,188 ns/iter (+/- 1,501) ```
1 parent 04aade8 commit 17f08fe

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

Diff for: src/libcore/char/methods.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,27 @@ impl char {
122122
#[inline]
123123
pub fn to_digit(self, radix: u32) -> Option<u32> {
124124
assert!(radix <= 36, "to_digit: radix is too high (maximum 36)");
125-
let val = match self {
126-
'0' ..= '9' => self as u32 - '0' as u32,
127-
'a' ..= 'z' => self as u32 - 'a' as u32 + 10,
128-
'A' ..= 'Z' => self as u32 - 'A' as u32 + 10,
129-
_ => return None,
125+
if radix == 10 {
126+
return match self {
127+
'0' ..= '9' => Some(self as u32 - '0' as u32),
128+
_ => None,
129+
};
130+
}
131+
132+
let val = if radix < 10 {
133+
match self {
134+
'0' ..= '9' => self as u32 - '0' as u32,
135+
_ => return None,
136+
}
137+
} else {
138+
match self {
139+
'0'..='9' => self as u32 - '0' as u32,
140+
'a'..='z' => self as u32 - 'a' as u32 + 10,
141+
'A'..='Z' => self as u32 - 'A' as u32 + 10,
142+
_ => return None,
143+
}
130144
};
145+
131146
if val < radix { Some(val) }
132147
else { None }
133148
}

0 commit comments

Comments
 (0)