Skip to content

Commit

Permalink
check for deserialized equality to ensure round-tripping (#64)
Browse files Browse the repository at this point in the history
enable "float_roundtrip" in serde_json to make it pass; operating without this feature is a tripping hazard.
  • Loading branch information
mumbleskates authored Mar 16, 2024
1 parent 9c45d34 commit 59eb2b2
Show file tree
Hide file tree
Showing 27 changed files with 106 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
9 changes: 8 additions & 1 deletion src/bench_abomonation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use criterion::{black_box, Criterion};

pub fn bench<T, R>(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;
Expand Down Expand Up @@ -49,5 +49,12 @@ where

crate::bench_size(name, "abomonation", deserialize_buffer.as_slice());

assert!(
unsafe { decode::<T>(black_box(&mut deserialize_buffer)) }
.unwrap()
.0
== data
);

group.finish();
}
9 changes: 8 additions & 1 deletion src/bench_bincode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use criterion::{black_box, Criterion};

pub fn bench<T>(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));
Expand Down Expand Up @@ -32,5 +32,12 @@ where

crate::bench_size(name, "bincode", buffer);

assert!(
bincode::decode_from_slice::<T, _>(black_box(buffer), conf)
.unwrap()
.0
== *data
);

group.finish();
}
4 changes: 3 additions & 1 deletion src/bench_bincode1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};

pub fn bench<T>(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;

Expand All @@ -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();
}
4 changes: 3 additions & 1 deletion src/bench_bitcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use criterion::{black_box, Criterion};

pub fn bench<T>(name: &'static str, c: &mut Criterion, data: &T)
where
T: Encode + DecodeOwned,
T: Encode + DecodeOwned + PartialEq,
{
let mut group = c.benchmark_group(format!("{}/bitcode", name));
let mut buffer = bitcode::EncodeBuffer::<T>::default();
Expand All @@ -25,5 +25,7 @@ where

crate::bench_size(name, "bitcode", encoded);

assert!(buffer.decode(&encoded).unwrap() == *data);

group.finish();
}
4 changes: 3 additions & 1 deletion src/bench_borsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use criterion::{black_box, Criterion};

pub fn bench<T>(name: &'static str, c: &mut Criterion, data: &T)
where
T: BorshSerialize + BorshDeserialize,
T: BorshSerialize + BorshDeserialize + PartialEq,
{
const BUFFER_LEN: usize = 10_000_000;

Expand All @@ -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();
}
4 changes: 3 additions & 1 deletion src/bench_bson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};

pub fn bench<T>(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;

Expand Down Expand Up @@ -31,5 +31,7 @@ where

crate::bench_size(name, "bson", deserialize_buffer.as_slice());

assert!(bson::from_slice::<T>(&deserialize_buffer).unwrap() == *data);

group.finish();
}
4 changes: 3 additions & 1 deletion src/bench_cbor4ii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};

pub fn bench<T>(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;

Expand All @@ -29,5 +29,7 @@ where

crate::bench_size(name, "cbor4ii", deserialize_buffer.as_slice());

assert!(cbor4ii::serde::from_slice::<T>(&deserialize_buffer).unwrap() == *data);

group.finish();
}
4 changes: 3 additions & 1 deletion src/bench_ciborium.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};

pub fn bench<T>(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;

Expand Down Expand Up @@ -35,5 +35,7 @@ where

crate::bench_size(name, "ciborium", deserialize_buffer.as_slice());

assert!(ciborium::de::from_reader::<T, _>(deserialize_buffer.as_slice()).unwrap() == *data);

group.finish();
}
4 changes: 3 additions & 1 deletion src/bench_databuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use databuf::{config::num::LE, *};

pub fn bench<T>(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;

Expand All @@ -27,5 +27,7 @@ where

crate::bench_size(name, "databuf", deserialize_buffer.as_slice());

assert!(T::from_bytes::<LE>(&deserialize_buffer).unwrap() == *data);

group.finish();
}
10 changes: 9 additions & 1 deletion src/bench_dlhn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};

pub fn bench<T>(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;

Expand Down Expand Up @@ -39,5 +39,13 @@ where

crate::bench_size(name, "dlhn", deserialize_buffer.as_slice());

assert!(
<T>::deserialize(&mut dlhn::de::Deserializer::new(
&mut deserialize_buffer.as_slice()
))
.unwrap()
== *data
);

group.finish();
}
4 changes: 3 additions & 1 deletion src/bench_msgpacker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use msgpacker::prelude::*;

pub fn bench<T>(name: &'static str, c: &mut Criterion, data: &T)
where
T: Packable + Unpackable,
T: Packable + Unpackable + PartialEq,
<T as Unpackable>::Error: fmt::Debug,
{
const BUFFER_LEN: usize = 10_000_000;
Expand All @@ -30,5 +30,7 @@ where

crate::bench_size(name, "msgpacker", &deserialize_buffer);

assert!(T::unpack(&deserialize_buffer).unwrap().1 == *data);

group.finish();
}
4 changes: 3 additions & 1 deletion src/bench_nachricht_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};

pub fn bench<T>(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;

Expand Down Expand Up @@ -32,5 +32,7 @@ where

crate::bench_size(name, "nachricht-serde", deserialize_buffer.as_slice());

assert!(nachricht_serde::from_bytes::<T>(&deserialize_buffer).unwrap() == *data);

group.finish();
}
4 changes: 3 additions & 1 deletion src/bench_nanoserde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use nanoserde::{DeBin, SerBin};

pub fn bench<T>(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));

Expand All @@ -24,5 +24,7 @@ where

crate::bench_size(name, "nanoserde", deserialize_buffer.as_slice());

assert!(<T as DeBin>::deserialize_bin(&deserialize_buffer).unwrap() == *data);

group.finish();
}
4 changes: 3 additions & 1 deletion src/bench_parity_scale_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use parity_scale_codec::{Decode, Encode};

pub fn bench<T>(name: &'static str, c: &mut Criterion, data: &T)
where
T: Encode + Decode,
T: Encode + Decode + PartialEq,
{
const BUFFER_LEN: usize = 10_000_000;

Expand All @@ -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();
}
4 changes: 3 additions & 1 deletion src/bench_postcard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};

pub fn bench<T>(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;

Expand All @@ -29,5 +29,7 @@ where

crate::bench_size(name, "postcard", deserialize_buffer.as_slice());

assert!(postcard::from_bytes::<T>(&deserialize_buffer).unwrap() == *data);

group.finish();
}
4 changes: 3 additions & 1 deletion src/bench_pot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};

pub fn bench<T>(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;

Expand All @@ -29,5 +29,7 @@ where

crate::bench_size(name, "pot", deserialize_buffer.as_slice());

assert!(pot::from_slice::<T>(&deserialize_buffer).unwrap() == *data);

group.finish();
}
2 changes: 1 addition & 1 deletion src/bench_prost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ where

crate::bench_size(name, "prost", deserialize_buffer.as_slice());

assert!(&<T::Message>::decode(&*deserialize_buffer).unwrap().into() == data);
assert!(<T::Message>::decode(&*deserialize_buffer).unwrap().into() == *data);

group.finish();
}
11 changes: 10 additions & 1 deletion src/bench_rkyv.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bincode1::Options;
use core::pin::Pin;

use bytecheck::CheckBytes;
Expand All @@ -20,7 +21,7 @@ pub type BenchSerializer<'a> = CompositeSerializer<

pub fn bench<T, R, U>(name: &'static str, c: &mut Criterion, data: &T, read: R, update: U)
where
T: Archive + for<'a> Serialize<BenchSerializer<'a>>,
T: Archive + for<'a> Serialize<BenchSerializer<'a>> + PartialEq,
T::Archived: for<'a> CheckBytes<DefaultValidator<'a>> + Deserialize<T, Infallible>,
R: Fn(&T::Archived),
U: Fn(Pin<&mut T::Archived>),
Expand Down Expand Up @@ -134,5 +135,13 @@ where

crate::bench_size(name, "rkyv", deserialize_buffer);

assert!(
check_archived_value::<T>(deserialize_buffer.as_ref(), pos)
.unwrap()
.deserialize(&mut Infallible)
.unwrap()
== *data
);

group.finish();
}
4 changes: 3 additions & 1 deletion src/bench_rmp_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};

pub fn bench<T>(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;

Expand Down Expand Up @@ -32,5 +32,7 @@ where

crate::bench_size(name, "rmp-serde", deserialize_buffer.as_slice());

assert!(rmp_serde::from_slice::<T>(&deserialize_buffer).unwrap() == *data);

group.finish();
}
4 changes: 3 additions & 1 deletion src/bench_ron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};

pub fn bench<T>(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;

Expand All @@ -29,5 +29,7 @@ where

crate::bench_size(name, "ron", deserialize_buffer.as_slice());

assert!(ron::de::from_bytes::<T>(&deserialize_buffer).unwrap() == *data);

group.finish();
}
6 changes: 5 additions & 1 deletion src/bench_savefile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io::Cursor;

pub fn bench<T>(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));

Expand All @@ -29,5 +29,9 @@ where

crate::bench_size(name, "savefile", deserialize_buffer.as_slice());

assert!(
savefile::load_noschema::<T>(&mut Cursor::new(&deserialize_buffer), 0).unwrap() == *data
);

group.finish();
}
Loading

0 comments on commit 59eb2b2

Please sign in to comment.