From f4a005ccf748c154cbc039833433b8270d07cdb8 Mon Sep 17 00:00:00 2001 From: Kent Ross Date: Tue, 6 Feb 2024 19:12:41 -0800 Subject: [PATCH] check for deserialized equality to ensure round-tripping enable "float_roundtrip" in serde_json to make it pass; operating without this feature is a tripping hazard. --- Cargo.toml | 2 +- src/bench_abomonation.rs | 9 ++++++++- src/bench_bincode.rs | 9 ++++++++- src/bench_bincode1.rs | 4 +++- src/bench_bitcode.rs | 4 +++- src/bench_borsh.rs | 4 +++- src/bench_bson.rs | 4 +++- src/bench_cbor4ii.rs | 4 +++- src/bench_ciborium.rs | 4 +++- src/bench_databuf.rs | 4 +++- src/bench_dlhn.rs | 10 +++++++++- src/bench_msgpacker.rs | 4 +++- src/bench_nachricht_serde.rs | 4 +++- src/bench_nanoserde.rs | 4 +++- src/bench_parity_scale_codec.rs | 4 +++- src/bench_postcard.rs | 4 +++- src/bench_pot.rs | 4 +++- src/bench_prost.rs | 2 +- src/bench_rkyv.rs | 11 ++++++++++- src/bench_rmp_serde.rs | 4 +++- src/bench_ron.rs | 4 +++- src/bench_savefile.rs | 6 +++++- src/bench_serde_bare.rs | 4 +++- src/bench_serde_cbor.rs | 4 +++- src/bench_serde_json.rs | 4 +++- src/bench_simd_json.rs | 8 +++++++- src/bench_speedy.rs | 4 +++- 27 files changed, 106 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 50d96b6..5c0a936 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,7 +84,7 @@ ron = { version = "0.8.1", optional = true } serde = { version = "1.0.190", features = ["derive"], optional = true } serde_bare = { version = "0.5.0", optional = true } serde_cbor = { version = "0.11.2", optional = true } -serde_json = { version = "1.0.108", optional = true } +serde_json = { version = "1.0.108", features = ["float_roundtrip"], optional = true } simd-json = { version = "0.13.4", optional = true } simd-json-derive = { version = "0.13.0", optional = true } speedy = { version = "0.8.6", optional = true } diff --git a/src/bench_abomonation.rs b/src/bench_abomonation.rs index 530aeda..694a707 100644 --- a/src/bench_abomonation.rs +++ b/src/bench_abomonation.rs @@ -3,7 +3,7 @@ use criterion::{black_box, Criterion}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T, read: R) where - T: Abomonation + Clone, + T: Abomonation + Clone + PartialEq, R: Fn(&T), { const BUFFER_LEN: usize = 10_000_000; @@ -49,5 +49,12 @@ where crate::bench_size(name, "abomonation", deserialize_buffer.as_slice()); + assert!( + unsafe { decode::(black_box(&mut deserialize_buffer)) } + .unwrap() + .0 + == *data + ); + group.finish(); } diff --git a/src/bench_bincode.rs b/src/bench_bincode.rs index a36e38e..4213a68 100644 --- a/src/bench_bincode.rs +++ b/src/bench_bincode.rs @@ -2,7 +2,7 @@ use criterion::{black_box, Criterion}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: bincode::Encode + bincode::Decode, + T: bincode::Encode + bincode::Decode + PartialEq, { const BUFFER_LEN: usize = 10_000_000; let mut group = c.benchmark_group(format!("{}/bincode", name)); @@ -32,5 +32,12 @@ where crate::bench_size(name, "bincode", buffer); + assert!( + bincode::decode_from_slice::(black_box(buffer), conf) + .unwrap() + .0 + == *data + ); + group.finish(); } diff --git a/src/bench_bincode1.rs b/src/bench_bincode1.rs index 701b1c8..1e4c987 100644 --- a/src/bench_bincode1.rs +++ b/src/bench_bincode1.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: Serialize + for<'de> Deserialize<'de>, + T: Serialize + for<'de> Deserialize<'de> + PartialEq, { const BUFFER_LEN: usize = 10_000_000; @@ -29,5 +29,7 @@ where crate::bench_size(name, "bincode1", deserialize_buffer.as_slice()); + assert!(bincode1::deserialize::<'_, T>(black_box(&deserialize_buffer)).unwrap() == *data); + group.finish(); } diff --git a/src/bench_bitcode.rs b/src/bench_bitcode.rs index 00dcab7..bcffb72 100644 --- a/src/bench_bitcode.rs +++ b/src/bench_bitcode.rs @@ -3,7 +3,7 @@ use criterion::{black_box, Criterion}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: Encode + Decode, + T: Encode + Decode + PartialEq, { let mut group = c.benchmark_group(format!("{}/bitcode", name)); let mut buffer = bitcode::Buffer::with_capacity(10000000); @@ -25,5 +25,7 @@ where crate::bench_size(name, "bitcode", encoded.as_slice()); + assert!(buffer.decode::(&encoded).unwrap() == *data); + group.finish(); } diff --git a/src/bench_borsh.rs b/src/bench_borsh.rs index 224ac04..680b4a6 100644 --- a/src/bench_borsh.rs +++ b/src/bench_borsh.rs @@ -3,7 +3,7 @@ use criterion::{black_box, Criterion}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: BorshSerialize + BorshDeserialize, + T: BorshSerialize + BorshDeserialize + PartialEq, { const BUFFER_LEN: usize = 10_000_000; @@ -28,5 +28,7 @@ where crate::bench_size(name, "borsh", deserialize_buffer.as_slice()); + assert!(T::deserialize(&mut deserialize_buffer.as_slice()).unwrap() == *data); + group.finish(); } diff --git a/src/bench_bson.rs b/src/bench_bson.rs index 7f483ba..c8a31f6 100644 --- a/src/bench_bson.rs +++ b/src/bench_bson.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: Serialize + for<'de> Deserialize<'de>, + T: Serialize + for<'de> Deserialize<'de> + PartialEq, { const BUFFER_LEN: usize = 50_000_000; @@ -31,5 +31,7 @@ where crate::bench_size(name, "bson", deserialize_buffer.as_slice()); + assert!(bson::from_slice::(&deserialize_buffer).unwrap() == *data); + group.finish(); } diff --git a/src/bench_cbor4ii.rs b/src/bench_cbor4ii.rs index a080b60..0a3c535 100644 --- a/src/bench_cbor4ii.rs +++ b/src/bench_cbor4ii.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: Serialize + for<'de> Deserialize<'de>, + T: Serialize + for<'de> Deserialize<'de> + PartialEq, { const BUFFER_LEN: usize = 50_000_000; @@ -29,5 +29,7 @@ where crate::bench_size(name, "cbor4ii", deserialize_buffer.as_slice()); + assert!(cbor4ii::serde::from_slice::(&deserialize_buffer).unwrap() == *data); + group.finish(); } diff --git a/src/bench_ciborium.rs b/src/bench_ciborium.rs index 032c520..8ea398c 100644 --- a/src/bench_ciborium.rs +++ b/src/bench_ciborium.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: Serialize + for<'de> Deserialize<'de>, + T: Serialize + for<'de> Deserialize<'de> + PartialEq, { const BUFFER_LEN: usize = 50_000_000; @@ -35,5 +35,7 @@ where crate::bench_size(name, "ciborium", deserialize_buffer.as_slice()); + assert!(ciborium::de::from_reader::(deserialize_buffer.as_slice()).unwrap() == *data); + group.finish(); } diff --git a/src/bench_databuf.rs b/src/bench_databuf.rs index 3597ae2..b0ede60 100644 --- a/src/bench_databuf.rs +++ b/src/bench_databuf.rs @@ -3,7 +3,7 @@ use databuf::{config::num::LE, *}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: Encode + for<'de> Decode<'de>, + T: Encode + for<'de> Decode<'de> + PartialEq, { const BUFFER_LEN: usize = 10_000_000; @@ -27,5 +27,7 @@ where crate::bench_size(name, "databuf", deserialize_buffer.as_slice()); + assert!(T::from_bytes::(&deserialize_buffer).unwrap() == *data); + group.finish(); } diff --git a/src/bench_dlhn.rs b/src/bench_dlhn.rs index 27f95c8..0c3efbf 100644 --- a/src/bench_dlhn.rs +++ b/src/bench_dlhn.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: Serialize + for<'de> Deserialize<'de>, + T: Serialize + for<'de> Deserialize<'de> + PartialEq, { const BUFFER_LEN: usize = 10_000_000; @@ -39,5 +39,13 @@ where crate::bench_size(name, "dlhn", deserialize_buffer.as_slice()); + assert!( + ::deserialize(&mut dlhn::de::Deserializer::new( + &mut deserialize_buffer.as_slice() + )) + .unwrap() + == *data + ); + group.finish(); } diff --git a/src/bench_msgpacker.rs b/src/bench_msgpacker.rs index 2b489bb..dbfe41d 100644 --- a/src/bench_msgpacker.rs +++ b/src/bench_msgpacker.rs @@ -4,7 +4,7 @@ use msgpacker::prelude::*; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: Packable + Unpackable, + T: Packable + Unpackable + PartialEq, ::Error: fmt::Debug, { const BUFFER_LEN: usize = 10_000_000; @@ -30,5 +30,7 @@ where crate::bench_size(name, "msgpacker", &deserialize_buffer); + assert!(T::unpack(&deserialize_buffer).unwrap().1 == *data); + group.finish(); } diff --git a/src/bench_nachricht_serde.rs b/src/bench_nachricht_serde.rs index df5ba5d..b18a43f 100644 --- a/src/bench_nachricht_serde.rs +++ b/src/bench_nachricht_serde.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: Serialize + for<'de> Deserialize<'de>, + T: Serialize + for<'de> Deserialize<'de> + PartialEq, { const BUFFER_LEN: usize = 25_000_000; @@ -32,5 +32,7 @@ where crate::bench_size(name, "nachricht-serde", deserialize_buffer.as_slice()); + assert!(nachricht_serde::from_bytes::(&deserialize_buffer).unwrap() == *data); + group.finish(); } diff --git a/src/bench_nanoserde.rs b/src/bench_nanoserde.rs index 50c9682..1c31208 100644 --- a/src/bench_nanoserde.rs +++ b/src/bench_nanoserde.rs @@ -3,7 +3,7 @@ use nanoserde::{DeBin, SerBin}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: DeBin + SerBin, + T: DeBin + SerBin + PartialEq, { let mut group = c.benchmark_group(format!("{}/nanoserde", name)); @@ -24,5 +24,7 @@ where crate::bench_size(name, "nanoserde", deserialize_buffer.as_slice()); + assert!(::deserialize_bin(&deserialize_buffer).unwrap() == *data); + group.finish(); } diff --git a/src/bench_parity_scale_codec.rs b/src/bench_parity_scale_codec.rs index f11f66a..0219560 100644 --- a/src/bench_parity_scale_codec.rs +++ b/src/bench_parity_scale_codec.rs @@ -3,7 +3,7 @@ use parity_scale_codec::{Decode, Encode}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: Encode + Decode, + T: Encode + Decode + PartialEq, { const BUFFER_LEN: usize = 10_000_000; @@ -27,5 +27,7 @@ where crate::bench_size(name, "parity-scale-codec", deserialize_buffer.as_slice()); + assert!(T::decode(&mut deserialize_buffer.as_slice()).unwrap() == *data); + group.finish(); } diff --git a/src/bench_postcard.rs b/src/bench_postcard.rs index 16d9b23..375c385 100644 --- a/src/bench_postcard.rs +++ b/src/bench_postcard.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: Serialize + for<'de> Deserialize<'de>, + T: Serialize + for<'de> Deserialize<'de> + PartialEq, { const BUFFER_LEN: usize = 10_000_000; @@ -29,5 +29,7 @@ where crate::bench_size(name, "postcard", deserialize_buffer.as_slice()); + assert!(postcard::from_bytes::(&deserialize_buffer).unwrap() == *data); + group.finish(); } diff --git a/src/bench_pot.rs b/src/bench_pot.rs index 7412108..0262c48 100644 --- a/src/bench_pot.rs +++ b/src/bench_pot.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: Serialize + for<'de> Deserialize<'de>, + T: Serialize + for<'de> Deserialize<'de> + PartialEq, { const BUFFER_LEN: usize = 70_000_000; @@ -29,5 +29,7 @@ where crate::bench_size(name, "pot", deserialize_buffer.as_slice()); + assert!(pot::from_slice::(&deserialize_buffer).unwrap() == *data); + group.finish(); } diff --git a/src/bench_prost.rs b/src/bench_prost.rs index 2d47d0b..4dc5d0a 100644 --- a/src/bench_prost.rs +++ b/src/bench_prost.rs @@ -45,7 +45,7 @@ where crate::bench_size(name, "prost", deserialize_buffer.as_slice()); - assert!(&::decode(&*deserialize_buffer).unwrap().into() == data); + assert!(::decode(&*deserialize_buffer).unwrap().into() == *data); group.finish(); } diff --git a/src/bench_rkyv.rs b/src/bench_rkyv.rs index fb2ff84..01532f1 100644 --- a/src/bench_rkyv.rs +++ b/src/bench_rkyv.rs @@ -1,3 +1,4 @@ +use bincode1::Options; use core::pin::Pin; use bytecheck::CheckBytes; @@ -20,7 +21,7 @@ pub type BenchSerializer<'a> = CompositeSerializer< pub fn bench(name: &'static str, c: &mut Criterion, data: &T, read: R, update: U) where - T: Archive + for<'a> Serialize>, + T: Archive + for<'a> Serialize> + PartialEq, T::Archived: for<'a> CheckBytes> + Deserialize, R: Fn(&T::Archived), U: Fn(Pin<&mut T::Archived>), @@ -134,5 +135,13 @@ where crate::bench_size(name, "rkyv", deserialize_buffer); + assert!( + check_archived_value::(deserialize_buffer.as_ref(), pos) + .unwrap() + .deserialize(&mut Infallible) + .unwrap() + == *data + ); + group.finish(); } diff --git a/src/bench_rmp_serde.rs b/src/bench_rmp_serde.rs index 7f2d4fd..372c01a 100644 --- a/src/bench_rmp_serde.rs +++ b/src/bench_rmp_serde.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: Serialize + for<'de> Deserialize<'de>, + T: Serialize + for<'de> Deserialize<'de> + PartialEq, { const BUFFER_LEN: usize = 10_000_000; @@ -32,5 +32,7 @@ where crate::bench_size(name, "rmp-serde", deserialize_buffer.as_slice()); + assert!(rmp_serde::from_slice::(&deserialize_buffer).unwrap() == *data); + group.finish(); } diff --git a/src/bench_ron.rs b/src/bench_ron.rs index 5136615..803913e 100644 --- a/src/bench_ron.rs +++ b/src/bench_ron.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: Serialize + for<'de> Deserialize<'de>, + T: Serialize + for<'de> Deserialize<'de> + PartialEq, { const BUFFER_LEN: usize = 50_000_000; @@ -29,5 +29,7 @@ where crate::bench_size(name, "ron", deserialize_buffer.as_slice()); + assert!(ron::de::from_bytes::(&deserialize_buffer).unwrap() == *data); + group.finish(); } diff --git a/src/bench_savefile.rs b/src/bench_savefile.rs index 86e7381..e7953fe 100644 --- a/src/bench_savefile.rs +++ b/src/bench_savefile.rs @@ -4,7 +4,7 @@ use std::io::Cursor; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: Serialize + Deserialize + WithSchema, + T: Serialize + Deserialize + WithSchema + PartialEq, { let mut group = c.benchmark_group(format!("{}/savefile", name)); @@ -29,5 +29,9 @@ where crate::bench_size(name, "savefile", deserialize_buffer.as_slice()); + assert!( + savefile::load_noschema::(&mut Cursor::new(&deserialize_buffer), 0).unwrap() == *data + ); + group.finish(); } diff --git a/src/bench_serde_bare.rs b/src/bench_serde_bare.rs index b49c823..bd4f186 100644 --- a/src/bench_serde_bare.rs +++ b/src/bench_serde_bare.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: Serialize + for<'de> Deserialize<'de>, + T: Serialize + for<'de> Deserialize<'de> + PartialEq, { const BUFFER_LEN: usize = 50_000_000; @@ -29,5 +29,7 @@ where crate::bench_size(name, "serde_bare", deserialize_buffer.as_slice()); + assert!(serde_bare::from_slice::(&deserialize_buffer).unwrap() == *data); + group.finish(); } diff --git a/src/bench_serde_cbor.rs b/src/bench_serde_cbor.rs index fdd9f92..1dea4dd 100644 --- a/src/bench_serde_cbor.rs +++ b/src/bench_serde_cbor.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: Serialize + for<'de> Deserialize<'de>, + T: Serialize + for<'de> Deserialize<'de> + PartialEq, { const BUFFER_LEN: usize = 50_000_000; @@ -29,5 +29,7 @@ where crate::bench_size(name, "serde_cbor", deserialize_buffer.as_slice()); + assert!(serde_cbor::from_slice::(&deserialize_buffer).unwrap() == *data); + group.finish(); } diff --git a/src/bench_serde_json.rs b/src/bench_serde_json.rs index 5c2b767..b5e25e3 100644 --- a/src/bench_serde_json.rs +++ b/src/bench_serde_json.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: Serialize + for<'de> Deserialize<'de>, + T: Serialize + for<'de> Deserialize<'de> + PartialEq, { const BUFFER_LEN: usize = 50_000_000; @@ -29,5 +29,7 @@ where crate::bench_size(name, "serde_json", deserialize_buffer.as_slice()); + assert!(serde_json::from_slice::(&deserialize_buffer).unwrap() == *data); + group.finish(); } diff --git a/src/bench_simd_json.rs b/src/bench_simd_json.rs index 7203443..de00b0b 100644 --- a/src/bench_simd_json.rs +++ b/src/bench_simd_json.rs @@ -4,7 +4,7 @@ use simd_json_derive::{Deserialize, Serialize}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: Serialize + for<'de> Deserialize<'de>, + T: Serialize + for<'de> Deserialize<'de> + PartialEq, { const BUFFER_LEN: usize = 50_000_000; @@ -38,5 +38,11 @@ where crate::bench_size(name, "simd-json", deserialize_buffer.as_slice()); + assert!( + T::from_slice_with_buffers(deserialize_buffer.clone().as_mut_slice(), &mut buffers) + .unwrap() + == *data + ); + group.finish(); } diff --git a/src/bench_speedy.rs b/src/bench_speedy.rs index 25fd127..eb4051d 100644 --- a/src/bench_speedy.rs +++ b/src/bench_speedy.rs @@ -3,7 +3,7 @@ use speedy::{Endianness, Readable, Writable}; pub fn bench(name: &'static str, c: &mut Criterion, data: &T) where - T: for<'a> Readable<'a, Endianness> + Writable, + T: for<'a> Readable<'a, Endianness> + Writable + PartialEq, { const BUFFER_LEN: usize = 10_000_000; @@ -36,5 +36,7 @@ where crate::bench_size(name, "speedy", deserialize_buffer.as_slice()); + assert!(T::read_from_buffer_with_ctx(CONTEXT, &deserialize_buffer).unwrap() == *data); + group.finish(); }