Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment: remove use of smallvec #38

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ repository = "https://github.com/pydantic/jiter/"
num-bigint = "0.4.4"
num-traits = "0.2.16"
ahash = "0.8.0"
smallvec = "1.11.0"
pyo3 = { version = "0.20.0", features = ["num-bigint"], optional = true }
lexical-core = { version = "0.8.5", features = ["format"] }

Expand Down
7 changes: 3 additions & 4 deletions src/lazy_index_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ use std::slice::Iter as SliceIter;
use std::sync::OnceLock;

use ahash::AHashMap;
use smallvec::SmallVec;

/// Like [IndexMap](https://docs.rs/indexmap/latest/indexmap/) but only builds the lookup map when it's needed.
#[derive(Clone, Default)]
pub struct LazyIndexMap<K, V> {
vec: SmallVec<[(K, V); 8]>,
vec: Vec<(K, V)>,
map: OnceLock<AHashMap<K, usize>>,
}

Expand All @@ -33,7 +32,7 @@ where
{
pub fn new() -> Self {
Self {
vec: SmallVec::new(),
vec: Vec::new(),
map: OnceLock::new(),
}
}
Expand Down Expand Up @@ -102,7 +101,7 @@ impl<K: PartialEq, V: PartialEq> PartialEq for LazyIndexMap<K, V> {
}

struct IterUnique<'a, K, V> {
vec: &'a SmallVec<[(K, V); 8]>,
vec: &'a Vec<(K, V)>,
map: &'a AHashMap<K, usize>,
index: usize,
}
Expand Down
4 changes: 1 addition & 3 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyList, PyString};

use smallvec::SmallVec;

use crate::errors::{json_error, FilePosition, JsonError, DEFAULT_RECURSION_LIMIT};
use crate::number_decoder::{NumberAny, NumberInt};
use crate::parse::{Parser, Peak};
Expand Down Expand Up @@ -80,7 +78,7 @@ impl<'j> PythonParser<'j> {
}
Peak::Array => {
let list = if let Some(peak_first) = self.parser.array_first().map_err(mje)? {
let mut vec: SmallVec<[PyObject; 8]> = SmallVec::with_capacity(8);
let mut vec: Vec<PyObject> = Vec::new();
let v = self._check_take_value(py, peak_first)?;
vec.push(v);
while let Some(peak) = self.parser.array_step().map_err(mje)? {
Expand Down
5 changes: 2 additions & 3 deletions src/value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::sync::Arc;

use num_bigint::BigInt;
use smallvec::SmallVec;

use crate::errors::{FilePosition, JsonError, JsonResult, JsonValueError, DEFAULT_RECURSION_LIMIT};
use crate::lazy_index_map::LazyIndexMap;
Expand All @@ -22,7 +21,7 @@ pub enum JsonValue {
Object(JsonObject),
}

pub type JsonArray = Arc<SmallVec<[JsonValue; 8]>>;
pub type JsonArray = Arc<Vec<JsonValue>>;
pub type JsonObject = Arc<LazyIndexMap<String, JsonValue>>;

#[cfg(feature = "python")]
Expand Down Expand Up @@ -112,7 +111,7 @@ pub(crate) fn take_value(
}
Peak::Array => {
// we could do something clever about guessing the size of the array
let mut array: SmallVec<[JsonValue; 8]> = SmallVec::new();
let mut array: Vec<JsonValue> = Vec::new();
if let Some(peak_first) = parser.array_first()? {
check_recursion!(recursion_limit, parser.index,
let v = take_value(peak_first, parser, tape, recursion_limit, allow_inf_nan)?;
Expand Down
21 changes: 10 additions & 11 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::str::FromStr;
use std::sync::Arc;

use num_bigint::BigInt;
use smallvec::smallvec;

use jiter::{
FilePosition, Jiter, JiterErrorType, JiterResult, JsonErrorType, JsonType, JsonValue, LazyIndexMap, NumberAny,
Expand Down Expand Up @@ -402,11 +401,11 @@ fn nan_disallowed_wrong_type() {
fn value_allow_nan_inf() {
let json = r#"[1, NaN, Infinity, -Infinity]"#;
let value = JsonValue::parse(json.as_bytes(), true).unwrap();
let expected = JsonValue::Array(Arc::new(smallvec![
let expected = JsonValue::Array(Arc::new(vec![
JsonValue::Int(1),
JsonValue::Float(f64::NAN),
JsonValue::Float(f64::INFINITY),
JsonValue::Float(f64::NEG_INFINITY)
JsonValue::Float(f64::NEG_INFINITY),
]));
// compare debug since `f64::NAN != f64::NAN`
assert_eq!(format!("{:?}", value), format!("{:?}", expected));
Expand Down Expand Up @@ -528,10 +527,10 @@ fn json_value_object() {
expected.insert("foo".to_string(), JsonValue::Str("bar".to_string()));
expected.insert(
"spam".to_string(),
JsonValue::Array(Arc::new(smallvec![
JsonValue::Array(Arc::new(vec![
JsonValue::Int(1),
JsonValue::Null,
JsonValue::Bool(true)
JsonValue::Bool(true),
])),
);
assert_eq!(v, JsonValue::Object(Arc::new(expected)));
Expand All @@ -542,10 +541,10 @@ fn json_value_string() {
let json = r#"["foo", "\u00a3", "\""]"#;
let v = JsonValue::parse(json.as_bytes(), false).unwrap();

let expected = JsonValue::Array(Arc::new(smallvec![
let expected = JsonValue::Array(Arc::new(vec![
JsonValue::Str("foo".to_string()),
JsonValue::Str("£".to_string()),
JsonValue::Str("\"".to_string())
JsonValue::Str("\"".to_string()),
]));
assert_eq!(v, expected);
}
Expand All @@ -556,7 +555,7 @@ fn parse_array_3() {
let v = JsonValue::parse(json.as_bytes(), false).unwrap();
assert_eq!(
v,
JsonValue::Array(Arc::new(smallvec![
JsonValue::Array(Arc::new(vec![
JsonValue::Int(1),
JsonValue::Null,
JsonValue::Bool(true)
Expand All @@ -568,7 +567,7 @@ fn parse_array_3() {
fn parse_array_empty() {
let json = r#"[ ]"#;
let v = JsonValue::parse(json.as_bytes(), false).unwrap();
assert_eq!(v, JsonValue::Array(Arc::new(smallvec![])));
assert_eq!(v, JsonValue::Array(Arc::new(vec![])));
}

#[test]
Expand All @@ -585,10 +584,10 @@ fn parse_value_nested() {
let v = JsonValue::parse(json.as_bytes(), false).unwrap();
assert_eq!(
v,
JsonValue::Array(Arc::new(smallvec![
JsonValue::Array(Arc::new(vec![
JsonValue::Int(1),
JsonValue::Int(2),
JsonValue::Array(Arc::new(smallvec![JsonValue::Int(3), JsonValue::Int(4)])),
JsonValue::Array(Arc::new(vec![JsonValue::Int(3), JsonValue::Int(4)])),
JsonValue::Int(5),
JsonValue::Int(6),
]),)
Expand Down