diff --git a/bench/algorithm/json-serde/4.rs b/bench/algorithm/json-serde/4.rs new file mode 100644 index 00000000..7cd97ba7 --- /dev/null +++ b/bench/algorithm/json-serde/4.rs @@ -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(&self, serializer: S) -> Result + 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, +} + +#[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>, +} diff --git a/bench/bench_rust.yaml b/bench/bench_rust.yaml index cb93ff30..974117b2 100644 --- a/bench/bench_rust.yaml +++ b/bench/bench_rust.yaml @@ -59,6 +59,7 @@ problems: - 1.rs - 2.rs - 3.rs + - 4.rs - name: coro-prime-sieve source: - 1.rs diff --git a/bench/include/rust/Cargo.toml b/bench/include/rust/Cargo.toml index 0456ed24..05bb057e 100644 --- a/bench/include/rust/Cargo.toml +++ b/bench/include/rust/Cargo.toml @@ -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}