Skip to content

Commit

Permalink
feat: refactor document and value
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 committed Dec 8, 2023
1 parent d601a7a commit 8b7158d
Show file tree
Hide file tree
Showing 43 changed files with 7,373 additions and 1,386 deletions.
82 changes: 77 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ bumpalo = "3.13"
bytes = "1.4"
thiserror = "1.0"
simdutf8 = "0.1"
parking_lot = "0.12"
static_assertions = "1.1"
libc = "0.2.150"

[dev-dependencies]
jemallocator = "0.5"
Expand Down Expand Up @@ -55,6 +58,10 @@ panic = 'unwind'
incremental = false
overflow-checks = false

[build]
sanitizers = true
debug = true

[[bench]]
name = "deserialize_struct"
harness = false
Expand Down
50 changes: 39 additions & 11 deletions benches/deserialize_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,24 @@ fn serde_from_str(data: &[u8]) {
let _: serde_json::Value = serde_json::from_str(data).unwrap();
}

fn sonic_rs_dom_from_slice(data: &[u8]) {
let _ = sonic_rs::value::dom_from_slice(data).unwrap();
fn sonic_rs_from_slice(data: &[u8]) {
let _: sonic_rs::Value = sonic_rs::from_slice(data).unwrap();
}

fn sonic_rs_dom_from_slice_unchecked(data: &[u8]) {
let _ = unsafe { sonic_rs::value::dom_from_slice_unchecked(data).unwrap() };
fn sonic_rs_from_slice_unchecked(data: &[u8]) {
let _: sonic_rs::Value = unsafe { sonic_rs::from_slice_unchecked(data).unwrap() };
}

fn sonic_rs_skip_one(data: &[u8]) {
unsafe {
let data = from_utf8_unchecked(data);
let empty: &[&str] = &[];
let _ = sonic_rs::get_unchecked(data, empty).unwrap();
}
}

fn sonic_rs_to_serdejson_value(data: &[u8]) {
let _: serde_json::Value = sonic_rs::from_slice(data).unwrap();
}

macro_rules! bench_file {
Expand All @@ -52,7 +64,7 @@ macro_rules! bench_file {
// verify sonic-rs parse
let serde_out: serde_json::Value = serde_json::from_slice(&vec).unwrap();

let value = sonic_rs::value::dom_from_slice(&vec).unwrap();
let value: sonic_rs::Value = sonic_rs::from_slice(&vec).unwrap();
let out = sonic_rs::to_string(&value).unwrap();
let rs_out1: serde_json::Value = serde_json::from_str(&out).unwrap();
assert_eq!(rs_out1, serde_out);
Expand All @@ -63,23 +75,31 @@ macro_rules! bench_file {
group.bench_with_input("sonic_rs_dom::from_slice", &vec, |b, data| {
b.iter_batched(
|| data,
|bytes| sonic_rs_dom_from_slice(&bytes),
|bytes| sonic_rs_from_slice(&bytes),
BatchSize::SmallInput,
)
});

group.bench_with_input("sonic_rs_dom::from_slice_unchecked", &vec, |b, data| {
b.iter_batched(
|| data,
|bytes| sonic_rs_dom_from_slice_unchecked(&bytes),
|bytes| sonic_rs_from_slice_unchecked(&bytes),
BatchSize::SmallInput,
)
});

group.bench_with_input("simd_json::slice_to_borrowed_value", &vec, |b, data| {
group.bench_with_input("sonic_rs::skip_one", &vec, |b, data| {
b.iter_batched(
|| data.clone(),
|mut bytes| simdjson_to_borrowed_value(&mut bytes),
|| data,
|bytes| sonic_rs_skip_one(&bytes),
BatchSize::SmallInput,
)
});

group.bench_with_input("sonic_rs::to_serdejson_value", &vec, |b, data| {
b.iter_batched(
|| data,
|bytes| sonic_rs_to_serdejson_value(&bytes),
BatchSize::SmallInput,
)
});
Expand Down Expand Up @@ -107,6 +127,14 @@ macro_rules! bench_file {
BatchSize::SmallInput,
)
});

group.bench_with_input("simd_json::slice_to_borrowed_value", &vec, |b, data| {
b.iter_batched(
|| data.clone(),
|mut bytes| simdjson_to_borrowed_value(&mut bytes),
BatchSize::SmallInput,
)
});
group.throughput(Throughput::Bytes(vec.len() as u64));
}
};
Expand All @@ -119,5 +147,5 @@ bench_file!(twitter);
bench_file!(github_events);

// criterion_group!(benches, canada, otfcc, citm_catalog, twitter, lottie, github_events, twitterescaped, book, poet, fgo);
criterion_group!(benches, twitter, citm_catalog, canada);
criterion_group!(benches, twitter, canada, citm_catalog);
criterion_main!(benches);
6 changes: 3 additions & 3 deletions benches/serialize_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn serde_to_string(val: &serde_json::Value) {
let _ = serde_json::to_string(val).unwrap();
}

fn sonic_rs_to_string(val: &sonic_rs::value::Document) {
fn sonic_rs_to_string(val: &sonic_rs::Value) {
let _ = sonic_rs::to_string(val).unwrap();
}

Expand Down Expand Up @@ -60,7 +60,7 @@ macro_rules! bench_file {
let serde_out: serde_json::Value = serde_json::from_slice(&data).unwrap();
let expect = serde_json::to_string(&serde_out).unwrap();

let value = sonic_rs::value::dom_from_slice(&data).unwrap();
let value: sonic_rs::Value = sonic_rs::from_slice(&data).unwrap();
let got = sonic_rs::to_string(&value).unwrap();
assert!(
diff_json(&got, &expect),
Expand All @@ -71,7 +71,7 @@ macro_rules! bench_file {
let mut group = c.benchmark_group(stringify!($name));
group.sampling_mode(SamplingMode::Flat);

let value = sonic_rs::value::dom_from_slice(&data).unwrap();
let value: sonic_rs::Value = sonic_rs::from_slice(&data).unwrap();
group.bench_with_input("sonic_rs::to_string", &value, |b, data| {
b.iter_batched(
|| data,
Expand Down
29 changes: 19 additions & 10 deletions examples/document.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Parse json into sonic_rs document.

use sonic_rs::value::{dom_from_slice, Value};
use sonic_rs::PointerNode;
use sonic_rs::{pointer, JsonValue};
use serde_json::json;
use sonic_rs::from_str;
use sonic_rs::JsonValueMutTrait;
use sonic_rs::{pointer, JsonValueTrait, Value};

fn main() {
let json = r#"{
"name": "Xiaoming",
Expand All @@ -17,9 +19,7 @@ fn main() {
]
}"#;

let mut dom = dom_from_slice(json.as_bytes()).unwrap();
// get the value from dom
let root = dom.as_value();
let mut root: Value = from_str(json).unwrap();

// get key from value
let age = root.get("age").as_i64();
Expand All @@ -34,8 +34,17 @@ fn main() {
assert_eq!(phones.as_str().unwrap(), "+123456");

// convert to mutable object
let mut obj = dom.as_object_mut().unwrap();
let value = Value::new_bool(true);
obj.insert("inserted", value);
assert!(obj.contains_key("inserted"));
let obj = root.as_object_mut().unwrap();
obj.insert(&"inserted", true);
assert!(obj.contains_key(&"inserted"));

let mut object = json!({ "A": 65, "B": 66, "C": 67 });
*object.get_mut("A").unwrap() = json!({
"code": 123,
"success": false,
"payload": {}
});

let mut array = json!(["A", "B", "C"]);
*array.get_mut(2).unwrap() = json!("D");
}
3 changes: 2 additions & 1 deletion examples/get_from.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use sonic_rs::{get, get_unchecked, pointer, JsonValue, PointerNode};
use sonic_rs::JsonValueTrait;
use sonic_rs::{get, get_unchecked, pointer};

fn main() {
let path = pointer!["a", "b", "c", 1];
Expand Down
1 change: 0 additions & 1 deletion examples/get_many.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use sonic_rs::pointer;
use sonic_rs::PointerNode;

fn main() {
let json = r#"
Expand Down
4 changes: 2 additions & 2 deletions examples/iterator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bytes::Bytes;
use faststr::FastStr;
use sonic_rs::{to_array_iter, to_object_iter_unchecked, JsonValue};

use sonic_rs::JsonValueTrait;
use sonic_rs::{to_array_iter, to_object_iter_unchecked};
fn main() {
let json = Bytes::from(r#"[1, 2, 3, 4, 5, 6]"#);
let iter = to_array_iter(&json);
Expand Down
Loading

0 comments on commit 8b7158d

Please sign in to comment.