Skip to content

Commit

Permalink
Corrections to prost benchmark (#62)
Browse files Browse the repository at this point in the history
* add round-trip guards to prost proving that it is not serializing all of the "logs" bench

More serializers should be forced to check this! the data they serialized should be fully recoverable!

* Fix prost serialization of Log

this significantly slows its encoding in this bench and increases its encoded/compressed size by 15/38%

* Run `cargo fmt`

---------

Co-authored-by: David Koloski <djkoloski@gmail.com>
  • Loading branch information
mumbleskates and djkoloski authored Feb 6, 2024
1 parent aa4d0ca commit 2a8527e
Show file tree
Hide file tree
Showing 5 changed files with 361 additions and 32 deletions.
10 changes: 6 additions & 4 deletions src/bench_prost.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use criterion::{black_box, Criterion};
use prost::Message;

pub trait Serialize {
type Message: Default + Message;
pub trait Serialize: Sized {
type Message: Default + Into<Self> + Message;

fn serialize_pb(&self) -> Self::Message;
}

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

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

group.bench_function("deserialize", |b| {
b.iter(|| {
black_box(T::Message::decode(black_box(&deserialize_buffer).as_slice()).unwrap());
black_box(<T::Message>::decode(black_box(&deserialize_buffer).as_slice()).unwrap());
})
});

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

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

group.finish();
}
59 changes: 48 additions & 11 deletions src/datasets/log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::bench_flatbuffers;
use crate::bench_prost;
use crate::Generate;

#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq)]
#[cfg_attr(feature = "abomonation", derive(abomonation_derive::Abomonation))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
Expand Down Expand Up @@ -114,6 +114,18 @@ impl bench_prost::Serialize for Address {
}
}

#[cfg(feature = "prost")]
impl From<log_prost::Address> for Address {
fn from(value: log_prost::Address) -> Self {
Address {
x0: value.x0.try_into().unwrap(),
x1: value.x1.try_into().unwrap(),
x2: value.x2.try_into().unwrap(),
x3: value.x3.try_into().unwrap(),
}
}
}

#[cfg(feature = "alkahest")]
impl alkahest::Pack<Address> for Address {
#[inline]
Expand All @@ -128,7 +140,7 @@ impl alkahest::Pack<Address> for Address {
}
}

#[derive(Clone)]
#[derive(Clone, PartialEq)]
#[cfg_attr(feature = "abomonation", derive(abomonation_derive::Abomonation))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
Expand Down Expand Up @@ -310,14 +322,30 @@ impl bench_prost::Serialize for Log {

#[inline]
fn serialize_pb(&self) -> Self::Message {
let mut result = Self::Message::default();
result.identity = self.identity.clone();
result.userid = self.userid.clone();
result.date = self.date.clone();
result.request = self.request.clone();
result.code = self.code as u32;
result.size = self.size;
result
log_prost::Log {
address: Some(self.address.serialize_pb()),
identity: self.identity.clone(),
userid: self.userid.clone(),
date: self.date.clone(),
request: self.request.clone(),
code: self.code as u32,
size: self.size,
}
}
}

#[cfg(feature = "prost")]
impl From<log_prost::Log> for Log {
fn from(value: log_prost::Log) -> Self {
Log {
address: value.address.unwrap().into(),
identity: value.identity,
userid: value.userid,
date: value.date,
request: value.request,
code: value.code.try_into().unwrap(),
size: value.size,
}
}
}

Expand All @@ -338,7 +366,7 @@ impl alkahest::Pack<LogSchema> for &'_ Log {
}
}

#[derive(Clone)]
#[derive(Clone, PartialEq)]
#[cfg_attr(feature = "abomonation", derive(abomonation_derive::Abomonation))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
Expand Down Expand Up @@ -429,6 +457,15 @@ impl bench_prost::Serialize for Logs {
}
}

#[cfg(feature = "prost")]
impl From<log_prost::Logs> for Logs {
fn from(value: log_prost::Logs) -> Self {
Logs {
logs: value.logs.into_iter().map(Into::into).collect(),
}
}
}

#[cfg(feature = "alkahest")]
#[derive(alkahest::Schema)]
pub struct LogsSchema {
Expand Down
38 changes: 35 additions & 3 deletions src/datasets/mesh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::bench_flatbuffers;
use crate::bench_prost;
use crate::Generate;

#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq)]
#[cfg_attr(feature = "abomonation", derive(abomonation_derive::Abomonation))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
Expand Down Expand Up @@ -109,6 +109,17 @@ impl bench_prost::Serialize for Vector3 {
}
}

#[cfg(feature = "prost")]
impl From<mesh_prost::Vector3> for Vector3 {
fn from(value: mesh_prost::Vector3) -> Self {
Vector3 {
x: value.x,
y: value.y,
z: value.z,
}
}
}

#[cfg(feature = "alkahest")]
impl alkahest::Pack<Vector3> for Vector3 {
#[inline]
Expand All @@ -122,7 +133,7 @@ impl alkahest::Pack<Vector3> for Vector3 {
}
}

#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq)]
#[cfg_attr(feature = "abomonation", derive(abomonation_derive::Abomonation))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
Expand Down Expand Up @@ -211,6 +222,18 @@ impl bench_prost::Serialize for Triangle {
}
}

#[cfg(feature = "prost")]
impl From<mesh_prost::Triangle> for Triangle {
fn from(value: mesh_prost::Triangle) -> Self {
Triangle {
v0: value.v0.unwrap().into(),
v1: value.v1.unwrap().into(),
v2: value.v2.unwrap().into(),
normal: value.normal.unwrap().into(),
}
}
}

#[cfg(feature = "alkahest")]
impl alkahest::Pack<Triangle> for &'_ Triangle {
#[inline]
Expand All @@ -225,7 +248,7 @@ impl alkahest::Pack<Triangle> for &'_ Triangle {
}
}

#[derive(Clone)]
#[derive(Clone, PartialEq)]
#[cfg_attr(feature = "abomonation", derive(abomonation_derive::Abomonation))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
Expand Down Expand Up @@ -318,6 +341,15 @@ impl bench_prost::Serialize for Mesh {
}
}

#[cfg(feature = "prost")]
impl From<mesh_prost::Mesh> for Mesh {
fn from(value: mesh_prost::Mesh) -> Self {
Mesh {
triangles: value.triangles.into_iter().map(Into::into).collect(),
}
}
}

#[cfg(feature = "alkahest")]
#[derive(alkahest::Schema)]
pub struct MeshSchema {
Expand Down
Loading

0 comments on commit 2a8527e

Please sign in to comment.