Skip to content

Commit

Permalink
Use a faster crate for ASCII representation parsing (#441)
Browse files Browse the repository at this point in the history
* Use `lexical-parse-integer` to parse ASCII integers quicker

* Use `atoi_radix10`
  • Loading branch information
aumetra authored Dec 7, 2023
1 parent 491429c commit 1e849fa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
11 changes: 4 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/masto-id-convert/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
] }
Expand Down
15 changes: 8 additions & 7 deletions lib/masto-id-convert/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -20,6 +19,12 @@ impl fmt::Display for Error {
}
}

impl From<atoi_radix10::ParseIntErrorPublic> for Error {
fn from(value: atoi_radix10::ParseIntErrorPublic) -> Self {
Self::NumberParse(value)
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

Expand Down Expand Up @@ -55,10 +60,6 @@ pub fn process<T>(masto_id: T) -> Result<Uuid, Error>
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))
}

0 comments on commit 1e849fa

Please sign in to comment.