about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorTobias Bieniek <tobias.bieniek@gmail.com>2018-11-13 18:05:46 +0100
committerTobias Bieniek <tobias.bieniek@gmail.com>2018-11-13 22:02:51 +0100
commit17f08fecfd81479e04fc5ea7590cecdb429c7ce3 (patch)
tree5866ba6317742bb703b5a50dbecbcd59d3d100c5 /src
parent04aade83f2785d6ca43049ff89026ed930f792f1 (diff)
downloadrust-17f08fecfd81479e04fc5ea7590cecdb429c7ce3.tar.gz
rust-17f08fecfd81479e04fc5ea7590cecdb429c7ce3.zip
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)
```
Diffstat (limited to 'src')
-rw-r--r--src/libcore/char/methods.rs25
1 files changed, 20 insertions, 5 deletions
diff --git a/src/libcore/char/methods.rs b/src/libcore/char/methods.rs
index fc212aa4f20..46b201f7bbf 100644
--- a/src/libcore/char/methods.rs
+++ b/src/libcore/char/methods.rs
@@ -122,12 +122,27 @@ impl char {
     #[inline]
     pub fn to_digit(self, radix: u32) -> Option<u32> {
         assert!(radix <= 36, "to_digit: radix is too high (maximum 36)");
-        let val = match self {
-          '0' ..= '9' => self as u32 - '0' as u32,
-          'a' ..= 'z' => self as u32 - 'a' as u32 + 10,
-          'A' ..= 'Z' => self as u32 - 'A' as u32 + 10,
-          _ => return None,
+        if radix == 10 {
+            return match self {
+                '0' ..= '9' => Some(self as u32 - '0' as u32),
+                _ => None,
+            };
+        }
+
+        let val = if radix < 10  {
+            match self {
+                '0' ..= '9' => self as u32 - '0' as u32,
+                _ => return None,
+            }
+        } else {
+            match self {
+                '0'..='9' => self as u32 - '0' as u32,
+                'a'..='z' => self as u32 - 'a' as u32 + 10,
+                'A'..='Z' => self as u32 - 'A' as u32 + 10,
+                _ => return None,
+            }
         };
+
         if val < radix { Some(val) }
         else { None }
     }