Skip to content
Draft
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: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ ordered-float = { version = "5.1.0", default-features = false }
rand = { version = "0.9.2", features = ["small_rng"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", default-features = false, features = ["std"] }
sonic-simd = "0.1.4"
zmij = "1.0"

[dev-dependencies]
Expand Down
284 changes: 272 additions & 12 deletions benches/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,55 @@
use std::fs;
use std::io::Read;

use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use criterion::{
black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion, Throughput,
};

fn parse_jsonb(data: &[u8]) {
let _v: jsonb::OwnedJsonb = jsonb::parse_owned_jsonb(data).unwrap();
let v: jsonb::OwnedJsonb = jsonb::parse_owned_jsonb(data).unwrap();
black_box(v);
}

fn parse_jsonb_with_buf(data: &[u8], buf: &mut Vec<u8>) {
buf.clear();
jsonb::parse_owned_jsonb_with_buf(data, buf).unwrap();
black_box(&buf);
}

fn parse_jsonb_standard(data: &[u8]) {
let v: jsonb::OwnedJsonb = jsonb::parse_owned_jsonb_standard_mode(data).unwrap();
black_box(v);
}

fn parse_jsonb_direct(data: &[u8]) {
let v: jsonb::OwnedJsonb = jsonb::parse_owned_jsonb_direct(data).unwrap();
black_box(v);
}

fn parse_jsonb_standard_direct(data: &[u8]) {
let v: jsonb::OwnedJsonb = jsonb::parse_owned_jsonb_standard_mode_direct(data).unwrap();
black_box(v);
}

fn parse_jsonb_standard_with_buf(data: &[u8], buf: &mut Vec<u8>) {
buf.clear();
jsonb::parse_owned_jsonb_standard_mode_with_buf(data, buf).unwrap();
black_box(&buf);
}

fn parse_serde_json(data: &[u8]) {
let _v: serde_json::Value = serde_json::from_slice(data).unwrap();
let v: serde_json::Value = serde_json::from_slice(data).unwrap();
black_box(v);
}

fn parse_json_deserializer(data: &[u8]) {
let _v: json_deserializer::Value = json_deserializer::parse(data).unwrap();
let v: json_deserializer::Value = json_deserializer::parse(data).unwrap();
black_box(v);
}

fn parse_simd_json(data: &mut [u8]) {
let _v = simd_json::to_borrowed_value(data).unwrap();
let v = simd_json::to_borrowed_value(data).unwrap();
black_box(v);
}

fn read(file: &str) -> Vec<u8> {
Expand All @@ -40,33 +73,260 @@ fn read(file: &str) -> Vec<u8> {
data
}

fn add_benchmark(c: &mut Criterion) {
fn compact_object() -> Vec<u8> {
let mut s = String::from("{");
for i in 0..96 {
if i > 0 {
s.push(',');
}
s.push_str(&format!(
r#""key_{i}":{{"id":{i},"name":"item-{i}","active":{},"values":[1,2,3,4]}}"#,
i % 2 == 0
));
}
s.push('}');
s.into_bytes()
}

fn pretty_object() -> Vec<u8> {
let mut s = String::from("{\n");
for i in 0..96 {
if i > 0 {
s.push_str(",\n");
}
s.push_str(&format!(
" \"key_{i}\": {{\n \"id\": {i},\n \"name\": \"item-{i}\",\n \"active\": {},\n \"values\": [1, 2, 3, 4]\n }}",
i % 2 == 0
));
}
s.push_str("\n}");
s.into_bytes()
}

fn large_array() -> Vec<u8> {
let mut s = String::from("[");
for i in 0..4096 {
if i > 0 {
s.push(',');
}
s.push_str(&(i * 17).to_string());
}
s.push(']');
s.into_bytes()
}

fn large_string() -> Vec<u8> {
let payload = "abcdefghijklmnopqrstuvwxyz0123456789".repeat(1024);
format!(r#"{{"payload":"{payload}"}}"#).into_bytes()
}

fn escaped_string() -> Vec<u8> {
let mut payload = String::new();
for i in 0..1024 {
payload.push_str(r#"line\n\t\"quoted\"\\slash\u0041"#);
payload.push_str(&i.to_string());
}
format!(r#"{{"payload":"{payload}"}}"#).into_bytes()
}

fn decimal_heavy() -> Vec<u8> {
let mut s = String::from("[");
for i in 0..512 {
if i > 0 {
s.push(',');
}
s.push_str(&format!("{i}.12345678901234567890123456789012345678"));
}
s.push(']');
s.into_bytes()
}

fn extended_json5_like() -> Vec<u8> {
let mut s = String::from("{");
for i in 0..128 {
if i > 0 {
s.push(',');
}
s.push_str(&format!(
"key_{i}: {{hex: 0x{:x}, single: 'value-{i}', plus: +{i}, sparse: [1,,3,]}}",
i * 31
));
}
s.push('}');
s.into_bytes()
}

fn synthetic_standard_cases() -> Vec<(&'static str, Vec<u8>)> {
vec![
("compact_object", compact_object()),
("pretty_object", pretty_object()),
("large_array", large_array()),
("large_string", large_string()),
("escaped_string", escaped_string()),
("decimal_heavy", decimal_heavy()),
("json5_like", extended_json5_like()),
]
}

fn synthetic_extended_cases() -> Vec<(&'static str, Vec<u8>)> {
vec![
("compact_object", compact_object()),
("pretty_object", pretty_object()),
("large_array", large_array()),
("large_string", large_string()),
("escaped_string", escaped_string()),
("decimal_heavy", decimal_heavy()),
("json5_like", extended_json5_like()),
]
}

fn bench_file_inputs(c: &mut Criterion) {
let paths = fs::read_dir("./data/").unwrap();
for path in paths {
let file = format!("{}", path.unwrap().path().display());
let bytes = read(&file);
let mut group = c.benchmark_group(format!("parser/file/{file}"));
group.throughput(Throughput::Bytes(bytes.len() as u64));

group.bench_function("jsonb_owned", |b| b.iter(|| parse_jsonb(&bytes)));

c.bench_function(&format!("jsonb parse {file}"), |b| {
b.iter(|| parse_jsonb(&bytes))
group.bench_function("jsonb_owned_with_buf", |b| {
let mut buf = Vec::with_capacity(bytes.len());
b.iter(|| parse_jsonb_with_buf(&bytes, &mut buf))
});

c.bench_function(&format!("serde_json parse {file}"), |b| {
b.iter(|| parse_serde_json(&bytes))
group.bench_function("jsonb_standard_owned", |b| {
b.iter(|| parse_jsonb_standard(&bytes))
});

c.bench_function(&format!("json_deserializer parse {file}"), |b| {
group.bench_function("jsonb_direct_owned", |b| {
b.iter(|| parse_jsonb_direct(&bytes))
});

group.bench_function("jsonb_standard_direct_owned", |b| {
b.iter(|| parse_jsonb_standard_direct(&bytes))
});

group.bench_function("jsonb_standard_owned_with_buf", |b| {
let mut buf = Vec::with_capacity(bytes.len());
b.iter(|| parse_jsonb_standard_with_buf(&bytes, &mut buf))
});

group.bench_function("serde_json", |b| b.iter(|| parse_serde_json(&bytes)));

group.bench_function("json_deserializer", |b| {
b.iter(|| parse_json_deserializer(&bytes))
});

let bytes = bytes.clone();
c.bench_function(&format!("simd_json parse {file}"), move |b| {
group.bench_function("simd_json", move |b| {
b.iter_batched(
|| bytes.clone(),
|mut data| parse_simd_json(&mut data),
BatchSize::SmallInput,
)
});
group.finish();
}
}

fn bench_synthetic_standard_inputs(c: &mut Criterion) {
let mut group = c.benchmark_group("parser/synthetic_standard");
for (name, bytes) in synthetic_standard_cases() {
group.throughput(Throughput::Bytes(bytes.len() as u64));

group.bench_with_input(BenchmarkId::new("jsonb_owned", name), &bytes, |b, data| {
b.iter(|| parse_jsonb(data))
});

group.bench_with_input(
BenchmarkId::new("jsonb_owned_with_buf", name),
&bytes,
|b, data| {
let mut buf = Vec::with_capacity(data.len());
b.iter(|| parse_jsonb_with_buf(data, &mut buf))
},
);

group.bench_with_input(
BenchmarkId::new("jsonb_standard_owned", name),
&bytes,
|b, data| b.iter(|| parse_jsonb_standard(data)),
);

group.bench_with_input(
BenchmarkId::new("jsonb_direct_owned", name),
&bytes,
|b, data| b.iter(|| parse_jsonb_direct(data)),
);

group.bench_with_input(
BenchmarkId::new("jsonb_standard_direct_owned", name),
&bytes,
|b, data| b.iter(|| parse_jsonb_standard_direct(data)),
);

group.bench_with_input(
BenchmarkId::new("jsonb_standard_owned_with_buf", name),
&bytes,
|b, data| {
let mut buf = Vec::with_capacity(data.len());
b.iter(|| parse_jsonb_standard_with_buf(data, &mut buf))
},
);

group.bench_with_input(BenchmarkId::new("serde_json", name), &bytes, |b, data| {
b.iter(|| parse_serde_json(data))
});

group.bench_with_input(
BenchmarkId::new("json_deserializer", name),
&bytes,
|b, data| b.iter(|| parse_json_deserializer(data)),
);

group.bench_with_input(BenchmarkId::new("simd_json", name), &bytes, |b, data| {
b.iter_batched(
|| data.clone(),
|mut data| parse_simd_json(&mut data),
BatchSize::SmallInput,
)
});
}
group.finish();
}

fn bench_synthetic_extended_inputs(c: &mut Criterion) {
let mut group = c.benchmark_group("parser/synthetic_extended");
for (name, bytes) in synthetic_extended_cases() {
group.throughput(Throughput::Bytes(bytes.len() as u64));

group.bench_with_input(BenchmarkId::new("jsonb_owned", name), &bytes, |b, data| {
b.iter(|| parse_jsonb(data))
});

group.bench_with_input(
BenchmarkId::new("jsonb_direct_owned", name),
&bytes,
|b, data| b.iter(|| parse_jsonb_direct(data)),
);

group.bench_with_input(
BenchmarkId::new("jsonb_owned_with_buf", name),
&bytes,
|b, data| {
let mut buf = Vec::with_capacity(data.len());
b.iter(|| parse_jsonb_with_buf(data, &mut buf))
},
);
}
group.finish();
}

fn add_benchmark(c: &mut Criterion) {
bench_file_inputs(c);
bench_synthetic_standard_inputs(c);
bench_synthetic_extended_inputs(c);
}

criterion_group!(benches, add_benchmark);
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ pub mod keypath;
mod number;
mod owned;
mod parser;
#[allow(dead_code)]
mod parser_direct;
mod raw;
mod util;
mod value;
Expand All @@ -96,6 +98,8 @@ pub use parser::parse_owned_jsonb_standard_mode_with_buf;
pub use parser::parse_owned_jsonb_with_buf;
pub use parser::parse_value;
pub use parser::parse_value_standard_mode;
pub use parser_direct::parse_owned_jsonb_direct;
pub use parser_direct::parse_owned_jsonb_standard_mode_direct;
pub use raw::from_raw_jsonb;
pub use raw::RawJsonb;
pub use value::*;
Loading
Loading