From e9ffaa579e4cec50d246b0dd9e4027aed29713ef Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Tue, 26 May 2020 23:38:24 -0400 Subject: [PATCH] use ad hoc parser for small integers this results in about a 3% speed up for small integer and float parsing --- src/value/parse.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/value/parse.rs b/src/value/parse.rs index 7c49e53..96f347a 100644 --- a/src/value/parse.rs +++ b/src/value/parse.rs @@ -431,6 +431,17 @@ fn single_value>( }) } +/// Parse numbers from strings *known* to contain +/// only the characters 0-9 +fn parse_i64(s: String) -> i64 { + let mut output = 0i64; + for b in s.as_bytes() { + output *= 10; + output += i64::from(b - b'0'); + } + output +} + impl Value { pub fn from_tokens>( toks: &mut PeekMoreIterator, @@ -735,7 +746,7 @@ impl Value { let n = if val.dec_len == 0 { if val.num.len() <= 18 && val.times_ten.is_empty() { - let n = Rational64::new_raw(val.num.parse::().unwrap(), 1); + let n = Rational64::new_raw(parse_i64(val.num), 1); return Some(Ok(IntermediateValue::Value(Value::Dimension( Number::new_machine(n), unit, @@ -746,7 +757,7 @@ impl Value { } else { if val.num.len() <= 18 && val.times_ten.is_empty() { let n = - Rational64::new(val.num.parse::().unwrap(), pow(10, val.dec_len)); + Rational64::new(parse_i64(val.num), pow(10, val.dec_len)); return Some(Ok(IntermediateValue::Value(Value::Dimension( Number::new_machine(n), unit,