From 1491b70b1bda67786cb114771814a4beaf16d1f0 Mon Sep 17 00:00:00 2001 From: Amos Wenger Date: Sat, 30 Nov 2024 16:29:35 +0100 Subject: [PATCH] bye bigint --- Cargo.lock | 21 ------ merde_json/Cargo.toml | 5 +- merde_json/src/error.rs | 4 - merde_json/src/jiter_lite/number_decoder.rs | 84 ++------------------- 4 files changed, 6 insertions(+), 108 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fab8232..8a3c439 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -356,8 +356,6 @@ dependencies = [ "lexical-parse-float", "merde_core", "merde_loggingserializer", - "num-bigint", - "num-traits", "ryu", "tokio", ] @@ -407,31 +405,12 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "num-bigint" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" -dependencies = [ - "num-integer", - "num-traits", -] - [[package]] name = "num-conv" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" -[[package]] -name = "num-integer" -version = "0.1.46" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" -dependencies = [ - "num-traits", -] - [[package]] name = "num-traits" version = "0.2.19" diff --git a/merde_json/Cargo.toml b/merde_json/Cargo.toml index 56abf35..6fca765 100644 --- a/merde_json/Cargo.toml +++ b/merde_json/Cargo.toml @@ -14,15 +14,12 @@ categories = ["encoding", "parser-implementations"] itoa = "1.0.11" lexical-parse-float = { version = "0.8.5", features = ["format"] } merde_core = { version = "8.1.1", path = "../merde_core" } -num-bigint = { version = "0.4.6", optional = true } -num-traits = { version = "0.2.19", optional = true } ryu = "1.0.18" tokio = { version = "1", optional = true, features = ["io-util"] } [features] default = [] -full = ["num-bigint", "tokio"] -num-bigint = ["dep:num-bigint", "dep:num-traits"] +full = ["tokio"] tokio = ["dep:tokio"] [dev-dependencies] diff --git a/merde_json/src/error.rs b/merde_json/src/error.rs index 6f511a4..b2c2251 100644 --- a/merde_json/src/error.rs +++ b/merde_json/src/error.rs @@ -19,9 +19,6 @@ pub enum JsonFieldType { /// The JSON value fits in an `i64`. Int, - /// The JSON value no longer fits in an `i64`. - BigInt, - /// The JSON value has decimal places. Float, @@ -144,7 +141,6 @@ impl JsonFieldType { JsonValue::Null => JsonFieldType::Null, JsonValue::Bool(_) => JsonFieldType::Bool, JsonValue::Int(_) => JsonFieldType::Int, - JsonValue::BigInt(_) => JsonFieldType::BigInt, JsonValue::Float(_) => JsonFieldType::Float, JsonValue::Str(_) => JsonFieldType::String, JsonValue::Array(_) => JsonFieldType::Array, diff --git a/merde_json/src/jiter_lite/number_decoder.rs b/merde_json/src/jiter_lite/number_decoder.rs index b780ef0..7c8149d 100644 --- a/merde_json/src/jiter_lite/number_decoder.rs +++ b/merde_json/src/jiter_lite/number_decoder.rs @@ -1,8 +1,3 @@ -#[cfg(feature = "num-bigint")] -use num_bigint::BigInt; -#[cfg(feature = "num-bigint")] -use num_traits::cast::ToPrimitive; - use std::ops::Range; use lexical_parse_float::{ @@ -26,16 +21,12 @@ pub trait AbstractNumberDecoder { #[derive(Debug, Clone, PartialEq)] pub enum NumberInt { Int(i64), - #[cfg(feature = "num-bigint")] - BigInt(BigInt), } impl From for f64 { fn from(num: NumberInt) -> Self { match num { NumberInt::Int(int) => int as f64, - #[cfg(feature = "num-bigint")] - NumberInt::BigInt(big_int) => big_int.to_f64().unwrap_or(f64::NAN), } } } @@ -220,7 +211,6 @@ pub(crate) enum IntParse { impl IntParse { pub(crate) fn parse(data: &[u8], mut index: usize, first: u8) -> JsonResult<(Self, usize)> { - let start = index; let positive = match first { b'N' => return Ok((Self::FloatNaN, index)), b'-' => false, @@ -254,7 +244,7 @@ impl IntParse { index += 1; let (chunk, new_index) = IntChunk::parse_small(data, index, first_value); - let ongoing: u64 = match chunk { + match chunk { IntChunk::Ongoing(value) => value, IntChunk::Done(value) => { let mut value_i64 = value as i64; @@ -266,72 +256,8 @@ impl IntParse { IntChunk::Float => return Ok((Self::Float, new_index)), }; - // number is too big for i64, we need to use a BigInt, - // or error out if num-bigint is not enabled - - #[cfg(not(feature = "num-bigint"))] - { - // silence unused variable warning - let _ = (ongoing, start); - json_err!(NumberOutOfRange, index) - } - - #[cfg(feature = "num-bigint")] - { - #[cfg(target_arch = "aarch64")] - // in aarch64 we use a 128 bit registers - 16 bytes - const ONGOING_CHUNK_MULTIPLIER: u64 = 10u64.pow(16); - #[cfg(not(target_arch = "aarch64"))] - // decode_int_chunk_fallback - we parse 18 bytes when the number is ongoing - const ONGOING_CHUNK_MULTIPLIER: u64 = 10u64.pow(18); - - const POW_10: [u64; 18] = [ - 10u64.pow(0), - 10u64.pow(1), - 10u64.pow(2), - 10u64.pow(3), - 10u64.pow(4), - 10u64.pow(5), - 10u64.pow(6), - 10u64.pow(7), - 10u64.pow(8), - 10u64.pow(9), - 10u64.pow(10), - 10u64.pow(11), - 10u64.pow(12), - 10u64.pow(13), - 10u64.pow(14), - 10u64.pow(15), - 10u64.pow(16), - 10u64.pow(17), - ]; - - let mut big_value: BigInt = ongoing.into(); - index = new_index; - - loop { - let (chunk, new_index) = IntChunk::parse_big(data, index); - if (new_index - start) > 4300 { - return json_err!(NumberOutOfRange, start + 4301); - } - match chunk { - IntChunk::Ongoing(value) => { - big_value *= ONGOING_CHUNK_MULTIPLIER; - big_value += value; - index = new_index; - } - IntChunk::Done(value) => { - big_value *= POW_10[new_index - index]; - big_value += value; - if !positive { - big_value = -big_value; - } - return Ok((Self::Int(NumberInt::BigInt(big_value)), new_index)); - } - IntChunk::Float => return Ok((Self::Float, new_index)), - } - } - } + // number is too big for i64 + json_err!(NumberOutOfRange, index) } } @@ -412,10 +338,10 @@ pub(crate) static INT_CHAR_MAP: [bool; 256] = { ] }; +// in some cfg configurations, fields are never read +#[allow(dead_code)] pub struct NumberRange { pub range: Range, - // in some cfg configurations, this field is never read. - #[allow(dead_code)] pub is_int: bool, }