Skip to content

Commit

Permalink
[add] databuf (#59)
Browse files Browse the repository at this point in the history
* [add] databuf

* [fix] remove cloning

Co-authored-by: David Koloski <djkoloski@gmail.com>

---------

Co-authored-by: David Koloski <djkoloski@gmail.com>
  • Loading branch information
kitsuniru and djkoloski committed Nov 5, 2023
1 parent 307ad9d commit 7eb7283
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -103,6 +104,7 @@ default = [
"capnp",
"cbor4ii",
"ciborium",
"databuf",
"dlhn",
"flatbuffers",
"msgpacker",
Expand Down
15 changes: 15 additions & 0 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
31 changes: 31 additions & 0 deletions src/bench_databuf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use criterion::{black_box, Criterion};
use databuf::{config::num::LE, *};

pub fn bench<T>(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::<LE>(&mut serialize_buffer));
})
});

let deserialize_buffer = data.to_bytes::<LE>();

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

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

group.finish();
}
3 changes: 3 additions & 0 deletions src/datasets/log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -133,6 +134,7 @@ impl alkahest::Pack<Address> 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",
Expand Down Expand Up @@ -341,6 +343,7 @@ impl alkahest::Pack<LogSchema> 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",
Expand Down
3 changes: 3 additions & 0 deletions src/datasets/mesh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -127,6 +128,7 @@ impl alkahest::Pack<Vector3> 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",
Expand Down Expand Up @@ -228,6 +230,7 @@ impl alkahest::Pack<Triangle> 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",
Expand Down
7 changes: 7 additions & 0 deletions src/datasets/minecraft_savedata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -136,6 +137,7 @@ impl alkahest::Pack<GameType> 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",
Expand Down Expand Up @@ -256,6 +258,7 @@ impl alkahest::Pack<ItemSchema> 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",
Expand Down Expand Up @@ -376,6 +379,7 @@ impl alkahest::Pack<Abilities> 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",
Expand Down Expand Up @@ -640,6 +644,7 @@ impl alkahest::Pack<EntitySchema> 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",
Expand Down Expand Up @@ -844,6 +849,7 @@ impl alkahest::Pack<RecipeBookSchema> 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",
Expand Down Expand Up @@ -1293,6 +1299,7 @@ impl alkahest::Pack<PlayerSchema> 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",
Expand Down
7 changes: 7 additions & 0 deletions src/datasets/mk48/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -378,6 +380,7 @@ impl alkahest::Pack<Transform> 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",
Expand Down Expand Up @@ -468,6 +471,7 @@ impl alkahest::Pack<Guidance> 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",
Expand Down Expand Up @@ -672,6 +676,7 @@ impl alkahest::Pack<ContactSchema> 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",
Expand Down Expand Up @@ -794,6 +799,7 @@ impl alkahest::Pack<TerrainUpdateSchema> 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",
Expand Down Expand Up @@ -949,6 +955,7 @@ impl alkahest::Pack<UpdateSchema> 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",
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down

0 comments on commit 7eb7283

Please sign in to comment.