Skip to content

Commit

Permalink
add cbor4ii to benchmarks (#56)
Browse files Browse the repository at this point in the history
* add cbor4ii to benchmarks

* Pin cbor4ii to 0.3.1

* fix fmt

---------

Co-authored-by: David Koloski <djkoloski@gmail.com>
  • Loading branch information
quininer and djkoloski authored Nov 1, 2023
1 parent f9e827a commit 1a589b4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ bson = { version = "2.6.0", git = "https://github.com/djkoloski/bson-rust", bran
bytecheck = { version = "0.6.11", optional = true }
bytemuck = { version = "1.14.0", optional = true }
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"
dlhn = { version = "0.1.6", optional = true }
Expand Down Expand Up @@ -100,6 +101,7 @@ default = [
"bytecheck",
"bytemuck",
"capnp",
"cbor4ii",
"ciborium",
"dlhn",
"flatbuffers",
Expand Down
14 changes: 14 additions & 0 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use rust_serialization_benchmark::bench_borsh;
use rust_serialization_benchmark::bench_bson;
#[cfg(feature = "capnp")]
use rust_serialization_benchmark::bench_capnp;
#[cfg(feature = "cbor4ii")]
use rust_serialization_benchmark::bench_cbor4ii;
#[cfg(feature = "ciborium")]
use rust_serialization_benchmark::bench_ciborium;
#[cfg(feature = "dlhn")]
Expand Down Expand Up @@ -115,6 +117,9 @@ fn bench_log(c: &mut Criterion) {
}
});

#[cfg(feature = "cbor4ii")]
bench_cbor4ii::bench(BENCH, c, &data);

#[cfg(feature = "ciborium")]
bench_ciborium::bench(BENCH, c, &data);

Expand Down Expand Up @@ -278,6 +283,9 @@ fn bench_mesh(c: &mut Criterion) {
}
});

#[cfg(feature = "cbor4ii")]
bench_cbor4ii::bench(BENCH, c, &data);

#[cfg(feature = "ciborium")]
bench_ciborium::bench(BENCH, c, &data);

Expand Down Expand Up @@ -427,6 +435,9 @@ fn bench_minecraft_savedata(c: &mut Criterion) {
}
});

#[cfg(feature = "cbor4ii")]
bench_cbor4ii::bench(BENCH, c, &data);

#[cfg(feature = "ciborium")]
bench_ciborium::bench(BENCH, c, &data);

Expand Down Expand Up @@ -580,6 +591,9 @@ fn bench_mk48(c: &mut Criterion) {
}
});

#[cfg(feature = "cbor4ii")]
bench_cbor4ii::bench(BENCH, c, &data);

#[cfg(feature = "ciborium")]
bench_ciborium::bench(BENCH, c, &data);

Expand Down
35 changes: 35 additions & 0 deletions src/bench_cbor4ii.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use criterion::{black_box, Criterion};
use serde::{Deserialize, Serialize};

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

let mut group = c.benchmark_group(format!("{}/cbor4ii", name));

let mut serialize_buffer = vec![0; BUFFER_LEN];
group.bench_function("serialize", |b| {
b.iter(|| {
serialize_buffer = cbor4ii::serde::to_vec(
std::mem::take(black_box(&mut serialize_buffer)),
black_box(&data),
)
.unwrap();
black_box(());
})
});

let deserialize_buffer = cbor4ii::serde::to_vec(Vec::new(), &data).unwrap();

group.bench_function("deserialize", |b| {
b.iter(|| {
black_box(cbor4ii::serde::from_slice::<'_, T>(black_box(&deserialize_buffer)).unwrap());
})
});

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

group.finish();
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub mod bench_borsh;
pub mod bench_bson;
#[cfg(feature = "capnp")]
pub mod bench_capnp;
#[cfg(feature = "cbor4ii")]
pub mod bench_cbor4ii;
#[cfg(feature = "ciborium")]
pub mod bench_ciborium;
#[cfg(feature = "dlhn")]
Expand Down

0 comments on commit 1a589b4

Please sign in to comment.