Skip to content

Instantly share code, notes, and snippets.

@KodrAus
Last active November 4, 2021 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KodrAus/2a6122dd8d8c032866874de1c950539d to your computer and use it in GitHub Desktop.
Save KodrAus/2a6122dd8d8c032866874de1c950539d to your computer and use it in GitHub Desktop.
Sizes of bytes vs tuples in some serde binary formats
#![feature(test)]
extern crate test;
use serde::{Serialize, Serializer};
pub struct Bytes([u8; 16]);
pub struct Tuple([u8; 16]);
pub struct U128([u8; 16]);
impl Serialize for Bytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_bytes(&self.0)
}
}
impl Serialize for Tuple {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.0.serialize(serializer)
}
}
impl Serialize for U128 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
use byteorder::{BE, ByteOrder};
BE::read_u128(&self.0).serialize(serializer)
}
}
#[derive(Serialize)]
pub struct UsingBytes {
field: Bytes,
}
#[derive(Serialize)]
pub struct UsingTuple {
field: Tuple,
}
#[derive(Serialize)]
pub struct UsingU128 {
field: U128,
}
fn cbor(v: impl Serialize) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
let mut b = Vec::new();
ciborium::ser::into_writer(&v, &mut b)?;
Ok(b)
}
fn main() {
let using_bytes = UsingBytes {
field: Bytes([1; 16]),
};
let using_tuple = UsingTuple {
field: Tuple([1; 16]),
};
let using_u128 = UsingU128 {
field: U128([1; 16]),
};
println!("bincode (bytes): {:?}", bincode::serialize(&using_bytes).map(|v| v.len()));
println!("bincode (tuple): {:?}", bincode::serialize(&using_tuple).map(|v| v.len()));
println!("bincode (u128) : {:?}", bincode::serialize(&using_u128).map(|v| v.len()));
println!();
println!("msgpack (bytes): {:?}", rmp_serde::encode::to_vec(&using_bytes).map(|v| v.len()));
println!("msgpack (tuple): {:?}", rmp_serde::encode::to_vec(&using_tuple).map(|v| v.len()));
println!("msgpack (u128) : {:?}", rmp_serde::encode::to_vec(&using_u128).map(|v| v.len()));
println!();
println!("flexbuf (bytes): {:?}", flexbuffers::to_vec(&using_bytes).map(|v| v.len()));
println!("flexbuf (tuple): {:?}", flexbuffers::to_vec(&using_tuple).map(|v| v.len()));
println!("flexbuf (u128) : {:?}", flexbuffers::to_vec(&using_u128).map(|v| v.len()));
println!();
println!("postcrd (bytes): {:?}", postcard::to_stdvec(&using_bytes).map(|v| v.len()));
println!("postcrd (tuple): {:?}", postcard::to_stdvec(&using_tuple).map(|v| v.len()));
println!("postcrd (u128) : {:?}", postcard::to_stdvec(&using_u128).map(|v| v.len()));
println!();
println!("pickle (bytes): {:?}", serde_pickle::to_vec(&using_bytes, Default::default()).map(|v| v.len()));
println!("pickle (tuple): {:?}", serde_pickle::to_vec(&using_tuple, Default::default()).map(|v| v.len()));
println!("pickle (u128) : {:?}", serde_pickle::to_vec(&using_u128, Default::default()).map(|v| v.len()));
println!();
println!("cbor (bytes): {:?}", cbor(&using_bytes).map(|v| v.len()));
println!("cbor (tuple): {:?}", cbor(&using_tuple).map(|v| v.len()));
println!("cbor (u128) : {:?}", cbor(&using_u128).map(|v| v.len()));
println!();
}
#[bench]
fn bincode_bytes(b: &mut test::Bencher) {
let using_bytes = UsingBytes {
field: Bytes([1; 16]),
};
b.iter(|| bincode::serialize(&using_bytes))
}
#[bench]
fn bincode_tuple(b: &mut test::Bencher) {
let using_tuple = UsingTuple {
field: Tuple([1; 16]),
};
b.iter(|| bincode::serialize(&using_tuple))
}
#[bench]
fn bincode_u128(b: &mut test::Bencher) {
let using_u128 = UsingU128 {
field: U128([1; 16]),
};
b.iter(|| bincode::serialize(&using_u128))
}
#[bench]
fn msgpack_bytes(b: &mut test::Bencher) {
let using_bytes = UsingBytes {
field: Bytes([1; 16]),
};
b.iter(|| rmp_serde::encode::to_vec(&using_bytes))
}
#[bench]
fn msgpack_tuple(b: &mut test::Bencher) {
let using_tuple = UsingTuple {
field: Tuple([1; 16]),
};
b.iter(|| rmp_serde::encode::to_vec(&using_tuple))
}
#[bench]
fn msgpack_u128(b: &mut test::Bencher) {
let using_u128 = UsingU128 {
field: U128([1; 16]),
};
b.iter(|| rmp_serde::encode::to_vec(&using_u128))
}
#[bench]
fn flexbuffers_bytes(b: &mut test::Bencher) {
let using_bytes = UsingBytes {
field: Bytes([1; 16]),
};
b.iter(|| flexbuffers::to_vec(&using_bytes))
}
#[bench]
fn flexbuffers_tuple(b: &mut test::Bencher) {
let using_tuple = UsingTuple {
field: Tuple([1; 16]),
};
b.iter(|| flexbuffers::to_vec(&using_tuple))
}
#[bench]
fn flexbuffers_u128(b: &mut test::Bencher) {
let using_u128 = UsingU128 {
field: U128([1; 16]),
};
b.iter(|| flexbuffers::to_vec(&using_u128))
}
#[bench]
fn postcard_bytes(b: &mut test::Bencher) {
let using_bytes = UsingBytes {
field: Bytes([1; 16]),
};
b.iter(|| postcard::to_stdvec(&using_bytes))
}
#[bench]
fn postcard_tuple(b: &mut test::Bencher) {
let using_tuple = UsingTuple {
field: Tuple([1; 16]),
};
b.iter(|| postcard::to_stdvec(&using_tuple))
}
#[bench]
fn postcard_u128(b: &mut test::Bencher) {
let using_u128 = UsingU128 {
field: U128([1; 16]),
};
b.iter(|| postcard::to_stdvec(&using_u128))
}
#[bench]
fn pickle_bytes(b: &mut test::Bencher) {
let using_bytes = UsingBytes {
field: Bytes([1; 16]),
};
b.iter(|| serde_pickle::to_vec(&using_bytes, Default::default()))
}
#[bench]
fn pickle_tuple(b: &mut test::Bencher) {
let using_tuple = UsingTuple {
field: Tuple([1; 16]),
};
b.iter(|| serde_pickle::to_vec(&using_tuple, Default::default()))
}
#[bench]
fn pickle_u128(b: &mut test::Bencher) {
let using_u128 = UsingU128 {
field: U128([1; 16]),
};
b.iter(|| serde_pickle::to_vec(&using_u128, Default::default()))
}
#[bench]
fn cbor_bytes(b: &mut test::Bencher) {
let using_bytes = UsingBytes {
field: Bytes([1; 16]),
};
b.iter(|| cbor(&using_bytes))
}
#[bench]
fn cbor_tuple(b: &mut test::Bencher) {
let using_tuple = UsingTuple {
field: Tuple([1; 16]),
};
b.iter(|| cbor(&using_tuple))
}
#[bench]
fn cbor_u128(b: &mut test::Bencher) {
let using_u128 = UsingU128 {
field: U128([1; 16]),
};
b.iter(|| cbor(&using_u128))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment