Skip to content

Commit

Permalink
upgrade to smallvec 2 to fix invariant lifetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Sep 9, 2024
1 parent dd25fd0 commit 6c45ab8
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion crates/jiter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repository = {workspace = true}
num-bigint = "0.4.4"
num-traits = "0.2.16"
ahash = "0.8.0"
smallvec = "1.11.0"
smallvec = "2.0.0-alpha.7"
pyo3 = { workspace = true, optional = true, features = ["num-bigint"] }
lexical-parse-float = { version = "0.8.5", features = ["format"] }
bitvec = "1.0.1"
Expand Down
4 changes: 2 additions & 2 deletions crates/jiter/src/lazy_index_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use smallvec::SmallVec;

/// Like [IndexMap](https://docs.rs/indexmap/latest/indexmap/) but only builds the lookup map when it's needed.
pub struct LazyIndexMap<K, V> {
vec: SmallVec<[(K, V); 8]>,
vec: SmallVec<(K, V), 8>,
map: OnceLock<AHashMap<K, usize>>,
last_find: AtomicUsize,
}
Expand Down Expand Up @@ -150,7 +150,7 @@ impl<K: PartialEq, V: PartialEq> PartialEq for LazyIndexMap<K, V> {
}

struct IterUnique<'a, K, V> {
vec: &'a SmallVec<[(K, V); 8]>,
vec: &'a SmallVec<(K, V), 8>,
map: &'a AHashMap<K, usize>,
index: usize,
}
Expand Down
4 changes: 2 additions & 2 deletions crates/jiter/src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl<'j, StringCache: StringMaybeCache, KeyCheck: MaybeKeyCheck, ParseNumber: Ma
Ok(None) | Err(_) => return Ok(PyList::empty_bound(py).into_any()),
};

let mut vec: SmallVec<[Bound<'_, PyAny>; 8]> = SmallVec::with_capacity(8);
let mut vec: SmallVec<Bound<'_, PyAny>, 8> = SmallVec::with_capacity(8);
if let Err(e) = self._parse_array(py, peek_first, &mut vec) {
if !self._allow_partial_err(&e) {
return Err(e);
Expand All @@ -174,7 +174,7 @@ impl<'j, StringCache: StringMaybeCache, KeyCheck: MaybeKeyCheck, ParseNumber: Ma
&mut self,
py: Python<'py>,
peek_first: Peek,
vec: &mut SmallVec<[Bound<'py, PyAny>; 8]>,
vec: &mut SmallVec<Bound<'py, PyAny>, 8>,
) -> JsonResult<()> {
let v = self._check_take_value(py, peek_first)?;
vec.push(v);
Expand Down
8 changes: 5 additions & 3 deletions crates/jiter/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub enum JsonValue<'s> {
Object(JsonObject<'s>),
}

pub type JsonArray<'s> = Arc<SmallVec<[JsonValue<'s>; 8]>>;
pub type JsonArray<'s> = Arc<SmallVec<JsonValue<'s>, 8>>;
pub type JsonObject<'s> = Arc<LazyIndexMap<Cow<'s, str>, JsonValue<'s>>>;

#[cfg(feature = "python")]
Expand Down Expand Up @@ -81,7 +81,9 @@ fn value_static(v: JsonValue<'_>) -> JsonValue<'static> {
JsonValue::BigInt(b) => JsonValue::BigInt(b),
JsonValue::Float(f) => JsonValue::Float(f),
JsonValue::Str(s) => JsonValue::Str(s.into_owned().into()),
JsonValue::Array(v) => JsonValue::Array(Arc::new(v.iter().map(JsonValue::to_static).collect::<SmallVec<_>>())),
JsonValue::Array(v) => {
JsonValue::Array(Arc::new(v.iter().map(JsonValue::to_static).collect::<SmallVec<_, 8>>()))
}
JsonValue::Object(o) => JsonValue::Object(Arc::new(o.to_static())),
}
}
Expand Down Expand Up @@ -173,7 +175,7 @@ fn take_value<'j, 's>(
}
Peek::Array => {
// we could do something clever about guessing the size of the array
let mut array: SmallVec<[JsonValue<'s>; 8]> = SmallVec::new();
let mut array: SmallVec<JsonValue<'s>, 8> = SmallVec::new();
if let Some(peek_first) = parser.array_first()? {
check_recursion!(recursion_limit, parser.index,
let v = take_value(peek_first, parser, tape, recursion_limit, allow_inf_nan, create_cow)?;
Expand Down
9 changes: 9 additions & 0 deletions crates/jiter/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1653,3 +1653,12 @@ fn test_unicode_roundtrip() {
assert_eq!(cow, "中文");
assert!(matches!(cow, Cow::Owned(_)));
}

#[test]
fn test_invariant_lifetimes() {
let v1 = JsonValue::Str("foobar".into());

let s = "foobar".to_string();
let v2 = JsonValue::Str(s.as_str().into());
assert_eq!(v1, v2);
}

0 comments on commit 6c45ab8

Please sign in to comment.