From 737521c21f5dc7fcbf50a09687f502fdb6adcec9 Mon Sep 17 00:00:00 2001 From: Noratrieb <48135649+Noratrieb@users.noreply.github.com> Date: Sun, 10 Nov 2024 20:35:25 +0100 Subject: [PATCH] `#[inline]` integer parsing functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 { input.parse() } ``` --- core/src/num/mod.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/src/num/mod.rs b/core/src/num/mod.rs index f4930ca5c7dbd..e18c81f770777 100644 --- a/core/src/num/mod.rs +++ b/core/src/num/mod.rs @@ -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 { <$t>::from_str_radix(src, 10) } @@ -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; @@ -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),