From 7eb7283673c57e33a188c4fbd5931064a927a0f3 Mon Sep 17 00:00:00 2001 From: Archie Kitsushimo <39557395+hot-moms@users.noreply.github.com> Date: Mon, 6 Nov 2023 02:44:43 +0300 Subject: [PATCH] [add] databuf (#59) * [add] databuf * [fix] remove cloning Co-authored-by: David Koloski --------- Co-authored-by: David Koloski --- Cargo.toml | 2 ++ benches/bench.rs | 15 +++++++++++++ src/bench_databuf.rs | 31 ++++++++++++++++++++++++++ src/datasets/log/mod.rs | 3 +++ src/datasets/mesh/mod.rs | 3 +++ src/datasets/minecraft_savedata/mod.rs | 7 ++++++ src/datasets/mk48/mod.rs | 7 ++++++ src/lib.rs | 2 ++ 8 files changed, 70 insertions(+) create mode 100644 src/bench_databuf.rs diff --git a/Cargo.toml b/Cargo.toml index 643e089..c143959 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -62,6 +62,7 @@ capnp = { version = "0.18.3", optional = true } cbor4ii = { version = "0.3.1", features = [ "use_std", "serde1" ], optional = true } ciborium = { version = "0.2.1", optional = true } criterion = "0.5" +databuf = { version = "0.5", optional = true } dlhn = { version = "0.1.6", optional = true } flatbuffers = { version = "23.5.26", optional = true } libflate = "1.3.0" @@ -103,6 +104,7 @@ default = [ "capnp", "cbor4ii", "ciborium", + "databuf", "dlhn", "flatbuffers", "msgpacker", diff --git a/benches/bench.rs b/benches/bench.rs index 318d699..d203746 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -18,6 +18,8 @@ use rust_serialization_benchmark::bench_capnp; use rust_serialization_benchmark::bench_cbor4ii; #[cfg(feature = "ciborium")] use rust_serialization_benchmark::bench_ciborium; +#[cfg(feature = "databuf")] +use rust_serialization_benchmark::bench_databuf; #[cfg(feature = "dlhn")] use rust_serialization_benchmark::bench_dlhn; #[cfg(feature = "flatbuffers")] @@ -54,6 +56,7 @@ use rust_serialization_benchmark::bench_serde_json; use rust_serialization_benchmark::bench_simd_json; #[cfg(feature = "speedy")] use rust_serialization_benchmark::bench_speedy; + use rust_serialization_benchmark::generate_vec; fn bench_log(c: &mut Criterion) { @@ -123,6 +126,9 @@ fn bench_log(c: &mut Criterion) { #[cfg(feature = "ciborium")] bench_ciborium::bench(BENCH, c, &data); + #[cfg(feature = "databuf")] + bench_databuf::bench(BENCH, c, &data); + #[cfg(feature = "dlhn")] bench_dlhn::bench(BENCH, c, &data); @@ -289,6 +295,9 @@ fn bench_mesh(c: &mut Criterion) { #[cfg(feature = "ciborium")] bench_ciborium::bench(BENCH, c, &data); + #[cfg(feature = "databuf")] + bench_databuf::bench(BENCH, c, &data); + #[cfg(feature = "dlhn")] bench_dlhn::bench(BENCH, c, &data); @@ -441,6 +450,9 @@ fn bench_minecraft_savedata(c: &mut Criterion) { #[cfg(feature = "ciborium")] bench_ciborium::bench(BENCH, c, &data); + #[cfg(feature = "databuf")] + bench_databuf::bench(BENCH, c, &data); + #[cfg(feature = "dlhn")] bench_dlhn::bench(BENCH, c, &data); @@ -597,6 +609,9 @@ fn bench_mk48(c: &mut Criterion) { #[cfg(feature = "ciborium")] bench_ciborium::bench(BENCH, c, &data); + #[cfg(feature = "databuf")] + bench_databuf::bench(BENCH, c, &data); + #[cfg(feature = "dlhn")] bench_dlhn::bench(BENCH, c, &data); diff --git a/src/bench_databuf.rs b/src/bench_databuf.rs new file mode 100644 index 0000000..3597ae2 --- /dev/null +++ b/src/bench_databuf.rs @@ -0,0 +1,31 @@ +use criterion::{black_box, Criterion}; +use databuf::{config::num::LE, *}; + +pub fn bench(name: &'static str, c: &mut Criterion, data: &T) +where + T: Encode + for<'de> Decode<'de>, +{ + const BUFFER_LEN: usize = 10_000_000; + + let mut group = c.benchmark_group(format!("{}/databuf", name)); + + let mut serialize_buffer = Vec::with_capacity(BUFFER_LEN); + group.bench_function("serialize", |b| { + b.iter(|| { + serialize_buffer.clear(); + black_box(data.encode::(&mut serialize_buffer)); + }) + }); + + let deserialize_buffer = data.to_bytes::(); + + group.bench_function("deserialize", |b| { + b.iter(|| { + black_box(T::from_bytes::(&deserialize_buffer).unwrap()); + }) + }); + + crate::bench_size(name, "databuf", deserialize_buffer.as_slice()); + + group.finish(); +} diff --git a/src/datasets/log/mod.rs b/src/datasets/log/mod.rs index 725790e..57c8524 100644 --- a/src/datasets/log/mod.rs +++ b/src/datasets/log/mod.rs @@ -38,6 +38,7 @@ use crate::Generate; feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize) )] +#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))] #[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))] #[cfg_attr( feature = "rkyv", @@ -133,6 +134,7 @@ impl alkahest::Pack
for Address { feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize) )] +#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))] #[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))] #[cfg_attr( feature = "rkyv", @@ -341,6 +343,7 @@ impl alkahest::Pack for &'_ Log { feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize) )] +#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))] #[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))] #[cfg_attr( feature = "rkyv", diff --git a/src/datasets/mesh/mod.rs b/src/datasets/mesh/mod.rs index 738b5d7..7157d70 100644 --- a/src/datasets/mesh/mod.rs +++ b/src/datasets/mesh/mod.rs @@ -37,6 +37,7 @@ use crate::Generate; feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize) )] +#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))] #[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))] #[cfg_attr( feature = "rkyv", @@ -127,6 +128,7 @@ impl alkahest::Pack for Vector3 { feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize) )] +#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))] #[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))] #[cfg_attr( feature = "rkyv", @@ -228,6 +230,7 @@ impl alkahest::Pack for &'_ Triangle { feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize) )] +#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))] #[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))] #[cfg_attr( feature = "rkyv", diff --git a/src/datasets/minecraft_savedata/mod.rs b/src/datasets/minecraft_savedata/mod.rs index 2a84ba8..cdf6d1e 100644 --- a/src/datasets/minecraft_savedata/mod.rs +++ b/src/datasets/minecraft_savedata/mod.rs @@ -38,6 +38,7 @@ use crate::{generate_vec, Generate}; feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize) )] +#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))] #[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))] #[cfg_attr( feature = "rkyv", @@ -136,6 +137,7 @@ impl alkahest::Pack for GameType { feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize) )] +#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))] #[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))] #[cfg_attr( feature = "rkyv", @@ -256,6 +258,7 @@ impl alkahest::Pack for &'_ Item { feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize) )] +#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))] #[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))] #[cfg_attr( feature = "rkyv", @@ -376,6 +379,7 @@ impl alkahest::Pack for Abilities { feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize) )] +#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))] #[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))] #[cfg_attr( feature = "rkyv", @@ -640,6 +644,7 @@ impl alkahest::Pack for &'_ Entity { feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize) )] +#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))] #[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))] #[cfg_attr( feature = "rkyv", @@ -844,6 +849,7 @@ impl alkahest::Pack for &'_ RecipeBook { feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize) )] +#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))] #[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))] #[cfg_attr( feature = "rkyv", @@ -1293,6 +1299,7 @@ impl alkahest::Pack for &'_ Player { feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize) )] +#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))] #[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))] #[cfg_attr( feature = "rkyv", diff --git a/src/datasets/mk48/mod.rs b/src/datasets/mk48/mod.rs index 30ab9fc..fad5a60 100644 --- a/src/datasets/mk48/mod.rs +++ b/src/datasets/mk48/mod.rs @@ -40,6 +40,7 @@ use crate::{generate_vec, Generate}; feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize) )] +#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))] #[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))] #[cfg_attr( feature = "rkyv", @@ -270,6 +271,7 @@ fn generate_velocity(rng: &mut impl Rng) -> i16 { feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize) )] +#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))] #[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))] #[cfg_attr( feature = "rkyv", @@ -378,6 +380,7 @@ impl alkahest::Pack for Transform { feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize) )] +#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))] #[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))] #[cfg_attr( feature = "rkyv", @@ -468,6 +471,7 @@ impl alkahest::Pack for Guidance { feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize) )] +#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))] #[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))] #[cfg_attr( feature = "rkyv", @@ -672,6 +676,7 @@ impl alkahest::Pack for &'_ Contact { feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize) )] +#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))] #[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))] #[cfg_attr( feature = "rkyv", @@ -794,6 +799,7 @@ impl alkahest::Pack for &'_ TerrainUpdate { feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize) )] +#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))] #[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))] #[cfg_attr( feature = "rkyv", @@ -949,6 +955,7 @@ impl alkahest::Pack for &'_ Update { feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize) )] +#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))] #[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))] #[cfg_attr( feature = "rkyv", diff --git a/src/lib.rs b/src/lib.rs index 05e52a7..1fb7fb1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,8 @@ pub mod bench_capnp; pub mod bench_cbor4ii; #[cfg(feature = "ciborium")] pub mod bench_ciborium; +#[cfg(feature = "databuf")] +pub mod bench_databuf; #[cfg(feature = "dlhn")] pub mod bench_dlhn; #[cfg(feature = "flatbuffers")]