Skip to content

Commit

Permalink
bench serde-json using simd-json
Browse files Browse the repository at this point in the history
  • Loading branch information
biryukovmaxim committed Jan 9, 2024
1 parent c470d59 commit 22df88b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
70 changes: 70 additions & 0 deletions bench/algorithm/json-serde/4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fs;

fn main() -> anyhow::Result<()> {
let file_name = std::env::args_os()
.nth(1)
.and_then(|s| s.into_string().ok())
.unwrap_or("sample".to_string());
let n = std::env::args_os()
.nth(2)
.and_then(|s| s.into_string().ok())
.and_then(|s| s.parse().ok())
.unwrap_or(10);
let mut json_str = fs::read(format!("{}.json", file_name))?;
let json: GeoData = simd_json::from_slice(&mut json_str)?;

print_hash(simd_json::to_vec(&json)?);
let mut array = Vec::with_capacity(n);
for _i in 0..n {
let json: GeoData = simd_json::from_slice(&mut json_str)?;
array.push(json);
}
print_hash(simd_json::to_vec(&array)?);
Ok(())
}

fn print_hash(bytes: impl AsRef<[u8]>) {
let digest = md5::compute(&bytes);
println!("{:x}", digest);
}

#[derive(Deserialize, Debug, Default)]
struct MyF64(f64);

impl Serialize for MyF64 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if self.0.fract() == 0.0 {
serializer.serialize_i64(self.0 as i64)
} else {
serializer.serialize_f64(self.0)
}
}
}

#[derive(Deserialize, Serialize, Debug, Default)]
struct GeoData {
r#type: String,
features: Vec<Feature>,
}

#[derive(Deserialize, Serialize, Debug, Default)]
struct Feature {
r#type: String,
properties: Properties,
geometry: Geometry,
}

#[derive(Deserialize, Serialize, Debug, Default)]
struct Properties {
name: String,
}

#[derive(Deserialize, Serialize, Debug, Default)]
struct Geometry {
r#type: String,
coordinates: Vec<Vec<[MyF64; 2]>>,
}
1 change: 1 addition & 0 deletions bench/bench_rust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ problems:
- 1.rs
- 2.rs
- 3.rs
- 4.rs
- name: coro-prime-sieve
source:
- 1.rs
Expand Down
1 change: 1 addition & 0 deletions bench/include/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ rayon = "1"
regex = "1"
serde = {version = "1", features = ["derive"]}
serde_json = {version = "1", features = ["float_roundtrip", "preserve_order"]}
simd-json = "0.13.6"
static-rc = "0"

async-channel = {version = "2", optional = true}
Expand Down

0 comments on commit 22df88b

Please sign in to comment.