From 1e849fad44a7590b6d05e9505b649847ed2e7ace Mon Sep 17 00:00:00 2001 From: aumetra Date: Thu, 7 Dec 2023 21:27:41 +0100 Subject: [PATCH] Use a faster crate for ASCII representation parsing (#441) * Use `lexical-parse-integer` to parse ASCII integers quicker * Use `atoi_radix10` --- Cargo.lock | 11 ++++------- lib/masto-id-convert/Cargo.toml | 2 +- lib/masto-id-convert/src/lib.rs | 15 ++++++++------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 099c9f322..e9cef61fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -463,13 +463,10 @@ dependencies = [ ] [[package]] -name = "atoi" -version = "2.0.0" +name = "atoi_radix10" +version = "0.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f28d99ec8bfea296261ca1af174f24225171fea9664ba9003cbebee704810528" -dependencies = [ - "num-traits 0.2.17", -] +checksum = "b6970a22a33d6a8f862aac371bac48505a1bfaa230ecb268c7b86fa4ac6e7121" [[package]] name = "atomic" @@ -3742,7 +3739,7 @@ dependencies = [ name = "masto-id-convert" version = "0.0.1-pre.4" dependencies = [ - "atoi", + "atoi_radix10", "criterion", "nanorand", "time", diff --git a/lib/masto-id-convert/Cargo.toml b/lib/masto-id-convert/Cargo.toml index 303204184..da5b6c068 100644 --- a/lib/masto-id-convert/Cargo.toml +++ b/lib/masto-id-convert/Cargo.toml @@ -9,7 +9,7 @@ name = "process" harness = false [dependencies] -atoi = { version = "2.0.0", default-features = false } +atoi_radix10 = "0.0.1" nanorand = { version = "0.7.0", default-features = false, features = [ "wyrand", ] } diff --git a/lib/masto-id-convert/src/lib.rs b/lib/masto-id-convert/src/lib.rs index aae3b1889..db5f7707d 100644 --- a/lib/masto-id-convert/src/lib.rs +++ b/lib/masto-id-convert/src/lib.rs @@ -2,7 +2,6 @@ #![forbid(missing_docs)] #![cfg_attr(not(feature = "std"), no_std)] -use atoi::FromRadix10; use core::fmt; use nanorand::{Rng, WyRand}; use uuid::Uuid; @@ -11,7 +10,7 @@ use uuid::Uuid; #[derive(Debug)] pub enum Error { /// Number parsing error - NumberParse, + NumberParse(atoi_radix10::ParseIntErrorPublic), } impl fmt::Display for Error { @@ -20,6 +19,12 @@ impl fmt::Display for Error { } } +impl From for Error { + fn from(value: atoi_radix10::ParseIntErrorPublic) -> Self { + Self::NumberParse(value) + } +} + #[cfg(feature = "std")] impl std::error::Error for Error {} @@ -55,10 +60,6 @@ pub fn process(masto_id: T) -> Result where T: AsRef<[u8]>, { - let (result, index) = u64::from_radix_10(masto_id.as_ref()); - if index == 0 { - return Err(Error::NumberParse); - } - + let result = atoi_radix10::parse(masto_id.as_ref())?; Ok(process_u64(result)) }