Skip to content

Commit

Permalink
#[inline] integer parsing functions
Browse files Browse the repository at this point in the history
This improves the performance of `str::parse` into integers.

Before:
```
compare          fastest       │ slowest       │ median        │ mean          │ samples │ iters
╰─ std                         │               │               │               │         │
   ├─ 328920585  10.23 ns      │ 24.8 ns       │ 10.34 ns      │ 10.48 ns      │ 100     │ 25600
   ├─ 3255       8.551 ns      │ 8.59 ns       │ 8.551 ns      │ 8.56 ns       │ 100     │ 25600
   ╰─ 5          7.847 ns      │ 7.887 ns      │ 7.847 ns      │ 7.853 ns      │ 100     │ 25600
```

After:
```
compare          fastest       │ slowest       │ median        │ mean          │ samples │ iters
╰─ std                         │               │               │               │         │
   ├─ 328920585  8.316 ns      │ 23.7 ns       │ 8.355 ns      │ 8.491 ns      │ 100     │ 25600
   ├─ 3255       4.566 ns      │ 4.588 ns      │ 4.586 ns      │ 4.576 ns      │ 100     │ 51200
   ╰─ 5          2.877 ns      │ 3.697 ns      │ 2.896 ns      │ 2.945 ns      │ 100     │ 102400
```

Benchmark:
```rust
fn std(input: &str) -> Result<u64, ParseIntError> {
    input.parse()
}
```
  • Loading branch information
Noratrieb committed Nov 10, 2024
1 parent 538f5b4 commit 737521c
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/src/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,7 @@ macro_rules! from_str_radix_int_impl {
#[stable(feature = "rust1", since = "1.0.0")]
impl FromStr for $t {
type Err = ParseIntError;
#[inline]
fn from_str(src: &str) -> Result<Self, ParseIntError> {
<$t>::from_str_radix(src, 10)
}
Expand Down Expand Up @@ -1505,6 +1506,7 @@ macro_rules! from_str_radix {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_int_from_str", since = "1.82.0")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32) -> Result<$int_ty, ParseIntError> {
use self::IntErrorKind::*;
use self::ParseIntError as PIE;
Expand Down Expand Up @@ -1649,6 +1651,7 @@ macro_rules! from_str_radix_size_impl {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_int_from_str", since = "1.82.0")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32) -> Result<$size, ParseIntError> {
match <$t>::from_str_radix(src, radix) {
Ok(x) => Ok(x as $size),
Expand Down

0 comments on commit 737521c

Please sign in to comment.