Skip to content

Commit 272ef60

Browse files
authored
perf: use hashmap in owned object to optimize search performance (#156)
1 parent f166dc7 commit 272ef60

10 files changed

Lines changed: 281 additions & 218 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ version = "0.4.0-rc4"
1515
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1616

1717
[dependencies]
18+
ahash = "0.8"
1819
bumpalo = "3.13"
1920
bytes = "1.9"
2021
cfg-if = "1.0"

benchmarks/benches/value_operator.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,12 @@ fn bench_object_insert(c: &mut Criterion) {
177177
b.iter_batched(
178178
|| (),
179179
|_| {
180-
let mut val = sonic_rs::Object::new();
180+
let mut val = sonic_rs::Object::with_capacity(100);
181181
for i in 0..100 {
182182
let mut obj = sonic_rs::json!({"a":{"b":{"c":{"d":{}}}}});
183-
for j in 0..100 {
183+
*obj["a"]["b"]["c"]["d"].as_object_mut().unwrap() =
184+
sonic_rs::Object::with_capacity(1000);
185+
for j in 0..1000 {
184186
obj["a"]["b"]["c"]["d"]
185187
.as_object_mut()
186188
.unwrap()
@@ -200,7 +202,7 @@ fn bench_object_insert(c: &mut Criterion) {
200202
let mut val = serde_json::Map::new();
201203
for i in 0..100 {
202204
let mut obj = serde_json::json!({"a":{"b":{"c":{"d":{}}}}});
203-
for j in 0..100 {
205+
for j in 0..1000 {
204206
obj["a"]["b"]["c"]["d"]
205207
.as_object_mut()
206208
.unwrap()

src/index.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ macro_rules! impl_str_index {
170170
if !v.is_object() {
171171
return None;
172172
}
173-
v.get_key_mut(*self).map(|v| v.0)
173+
v.get_key_mut(*self)
174174
}
175175

176176
#[inline]
@@ -184,11 +184,11 @@ macro_rules! impl_str_index {
184184
obj.as_object_mut()
185185
.expect(&format!("cannot access key in non-object value {:?}", typ))
186186
.0
187-
.get_key_mut(*self).map_or_else(|| {
187+
.get_key_mut(*self).unwrap_or_else(|| {
188188
let o = unsafe { dormant_obj.reborrow() };
189-
let inserted = o.append_pair((Into::<Value>::into((*self)), Value::new_null()));
190-
&mut inserted.1
191-
}, |v| v.0)
189+
let inserted = o.insert(&self, Value::new_null());
190+
inserted
191+
})
192192
}
193193

194194
#[inline]

src/lazyvalue/value.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,19 +272,11 @@ impl<'a> JsonValueTrait for LazyValue<'a> {
272272
}
273273

274274
fn as_number(&self) -> Option<Number> {
275-
if let Ok(num) = from_str(self.as_raw_str()) {
276-
Some(num)
277-
} else {
278-
None
279-
}
275+
from_str(self.as_raw_str()).ok()
280276
}
281277

282278
fn as_raw_number(&self) -> Option<RawNumber> {
283-
if let Ok(num) = from_str(self.as_raw_str()) {
284-
Some(num)
285-
} else {
286-
None
287-
}
279+
from_str(self.as_raw_str()).ok()
288280
}
289281

290282
fn as_str(&self) -> Option<&str> {

src/value/from.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,10 +563,10 @@ mod test {
563563
let x: Value = map.iter().collect();
564564
assert_eq!(x, json!({"sonic_rs": 40, "json": 2}));
565565

566-
let v = std::iter::repeat(6).take(3);
566+
let v = std::iter::repeat_n(6, 3);
567567
let x1: Vec<_> = v.collect();
568568
dbg!(x1);
569-
let v = std::iter::repeat(6).take(3);
569+
let v = std::iter::repeat_n(6, 3);
570570
let x: Value = v.collect();
571571
assert_eq!(x, json!([6, 6, 6]));
572572

src/value/macros.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,7 @@ macro_rules! json_internal {
226226
// Insert the current entry followed by trailing comma.
227227
(@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
228228
let key: &str = ($($key)+).as_ref();
229-
let pair = ($crate::Value::copy_str(key), $value);
230-
let _ = $object.append_pair(pair);
229+
let _ = $object.insert(key, $value);
231230
json_internal!(@object $object () ($($rest)*) ($($rest)*));
232231
};
233232

@@ -239,8 +238,7 @@ macro_rules! json_internal {
239238
// Insert the last entry without trailing comma.
240239
(@object $object:ident [$($key:tt)+] ($value:expr)) => {
241240
let key: &str = ($($key)+).as_ref();
242-
let pair = ($crate::Value::copy_str(key), $value);
243-
let _ = $object.append_pair(pair);
241+
let _ = $object.insert(key, $value);
244242
};
245243

246244
// Next value is `null`.

0 commit comments

Comments
 (0)