Skip to content

Commit

Permalink
Add minicbor support (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
Quantumplation authored Jan 23, 2025
1 parent 6674756 commit 129d68a
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 12 deletions.
20 changes: 8 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ ciborium = { version = "=0.2.2", optional = true }
databuf = { version = "=0.5.0", optional = true }
dlhn = { version = "=0.1.7", optional = true }
flatbuffers = { version = "=24.12.23", optional = true }
minicbor = { version = "=0.25.1", optional = true, features = [
"alloc",
"derive",
] }
msgpacker = { version = "=0.4.5", optional = true }
nachricht-serde = { version = "=0.4.0", optional = true }
nanoserde = { version = "=0.1.37", optional = true }
Expand All @@ -74,9 +78,7 @@ ron = { version = "=0.8.1", optional = true }
savefile = { version = "=0.18.5", optional = true }
savefile-derive = { version = "=0.18.5", optional = true }
serde_bare = { version = "=0.5.0", optional = true }
serde-brief = { version = "=0.1.0", features = [
"std",
], optional = true }
serde-brief = { version = "=0.1.0", features = ["std"], optional = true }
serde_cbor = { version = "=0.11.2", optional = true }
serde_json = { version = "=1.0.134", features = [
"float_roundtrip",
Expand All @@ -94,10 +96,7 @@ serde = { version = "=1.0.216", features = ["derive"] }
zstd = "=0.13.2"

[features]
default = [
"default-encoding-set",
"measure-compression",
]
default = ["default-encoding-set", "measure-compression"]
default-encoding-set = [
"bilrost",
"bincode1",
Expand All @@ -110,6 +109,7 @@ default-encoding-set = [
"databuf",
"dlhn",
"flatbuffers",
"minicbor",
"msgpacker",
"nachricht-serde",
"nanoserde",
Expand Down Expand Up @@ -138,11 +138,7 @@ savefile = ["dep:savefile", "dep:savefile-derive"]
scale = ["dep:parity-scale-codec", "dep:parity-scale-codec-derive"]

# Enable these features to regenerate generated files rather than using the committed versions.
regenerate = [
"regenerate-capnp",
"regenerate-flatbuffers",
"regenerate-prost",
]
regenerate = ["regenerate-capnp", "regenerate-flatbuffers", "regenerate-prost"]
regenerate-capnp = ["dep:capnpc"]
regenerate-flatbuffers = ["dep:flatc-rust"]
regenerate-prost = ["dep:prost-build"]
Expand Down
11 changes: 11 additions & 0 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use rust_serialization_benchmark::bench_databuf;
use rust_serialization_benchmark::bench_dlhn;
#[cfg(feature = "flatbuffers")]
use rust_serialization_benchmark::bench_flatbuffers;
#[cfg(feature = "minicbor")]
use rust_serialization_benchmark::bench_minicbor;
#[cfg(feature = "msgpacker")]
use rust_serialization_benchmark::bench_msgpacker;
#[cfg(feature = "nachricht-serde")]
Expand Down Expand Up @@ -149,6 +151,9 @@ fn bench_log(c: &mut Criterion) {
},
);

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

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

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

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

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

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

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

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

Expand Down
34 changes: 34 additions & 0 deletions src/bench_minicbor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use criterion::{black_box, Criterion};
use minicbor::{Decode, Encode};

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

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

let mut serialize_buffer = vec![0; BUFFER_LEN];
group.bench_function("serialize", |b| {
b.iter(|| {
minicbor::encode(black_box(data), black_box(serialize_buffer.as_mut_slice())).unwrap();
black_box(());
})
});

let mut deserialize_buffer = Vec::new();
minicbor::encode(&data, &mut deserialize_buffer).unwrap();

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

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

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

group.finish();
}
15 changes: 15 additions & 0 deletions src/datasets/log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use crate::Generate;
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))]
#[cfg_attr(feature = "minicbor", derive(minicbor::Encode, minicbor::Decode))]
#[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))]
#[cfg_attr(
feature = "rkyv",
Expand All @@ -56,9 +57,13 @@ use crate::Generate;
#[cfg_attr(feature = "wiring", derive(Wiring, Unwiring))]
pub struct Address {
#[cfg_attr(feature = "wiring", fixed)]
#[cfg_attr(feature = "minicbor", n(0))]
pub x0: u8,
#[cfg_attr(feature = "minicbor", n(1))]
pub x1: u8,
#[cfg_attr(feature = "minicbor", n(2))]
pub x2: u8,
#[cfg_attr(feature = "minicbor", n(3))]
pub x3: u8,
}

Expand Down Expand Up @@ -187,6 +192,7 @@ impl From<log_prost::Address> for Address {
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))]
#[cfg_attr(feature = "minicbor", derive(minicbor::Encode, minicbor::Decode))]
#[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))]
#[cfg_attr(
feature = "rkyv",
Expand All @@ -206,13 +212,20 @@ impl From<log_prost::Address> for Address {
#[cfg_attr(feature = "nanoserde", derive(nanoserde::SerBin, nanoserde::DeBin))]
#[cfg_attr(feature = "wiring", derive(Wiring, Unwiring))]
pub struct Log {
#[cfg_attr(feature = "minicbor", n(0))]
pub address: Address,
#[cfg_attr(feature = "minicbor", b(1))]
pub identity: String,
#[cfg_attr(feature = "minicbor", b(2))]
pub userid: String,
#[cfg_attr(feature = "minicbor", b(3))]
pub date: String,
#[cfg_attr(feature = "minicbor", b(4))]
pub request: String,
#[cfg_attr(feature = "wiring", fixed)]
#[cfg_attr(feature = "minicbor", n(5))]
pub code: u16,
#[cfg_attr(feature = "minicbor", n(6))]
pub size: u64,
}

Expand Down Expand Up @@ -364,6 +377,7 @@ impl From<log_prost::Log> for Log {
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))]
#[cfg_attr(feature = "minicbor", derive(minicbor::Encode, minicbor::Decode))]
#[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))]
#[cfg_attr(
feature = "rkyv",
Expand All @@ -384,6 +398,7 @@ impl From<log_prost::Log> for Log {
#[cfg_attr(feature = "wiring", derive(Wiring, Unwiring))]
pub struct Logs {
#[cfg_attr(feature = "bilrost", bilrost(encoding(packed)))]
#[cfg_attr(feature = "minicbor", n(0))]
pub logs: Vec<Log>,
}

Expand Down
11 changes: 11 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;
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))]
#[cfg_attr(feature = "minicbor", derive(minicbor::Encode, minicbor::Decode))]
#[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))]
#[cfg_attr(
feature = "rkyv",
Expand All @@ -57,8 +58,11 @@ use crate::Generate;
#[cfg_attr(feature = "wiring", derive(Wiring, Unwiring))]
pub struct Vector3 {
#[cfg_attr(feature = "wiring", fixed)]
#[cfg_attr(feature = "minicbor", n(0))]
pub x: f32,
#[cfg_attr(feature = "minicbor", n(1))]
pub y: f32,
#[cfg_attr(feature = "minicbor", n(2))]
pub z: f32,
}

Expand Down Expand Up @@ -127,6 +131,7 @@ impl From<mesh_prost::Vector3> for Vector3 {
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))]
#[cfg_attr(feature = "minicbor", derive(minicbor::Encode, minicbor::Decode))]
#[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))]
#[cfg_attr(
feature = "rkyv",
Expand All @@ -147,9 +152,13 @@ impl From<mesh_prost::Vector3> for Vector3 {
#[cfg_attr(feature = "wiring", derive(Wiring, Unwiring))]
pub struct Triangle {
#[cfg_attr(feature = "wiring", fixed)]
#[cfg_attr(feature = "minicbor", n(0))]
pub v0: Vector3,
#[cfg_attr(feature = "minicbor", n(1))]
pub v1: Vector3,
#[cfg_attr(feature = "minicbor", n(2))]
pub v2: Vector3,
#[cfg_attr(feature = "minicbor", n(3))]
pub normal: Vector3,
}

Expand Down Expand Up @@ -228,6 +237,7 @@ impl From<mesh_prost::Triangle> for Triangle {
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "databuf", derive(databuf::Encode, databuf::Decode))]
#[cfg_attr(feature = "minicbor", derive(minicbor::Encode, minicbor::Decode))]
#[cfg_attr(feature = "msgpacker", derive(msgpacker::MsgPacker))]
#[cfg_attr(
feature = "rkyv",
Expand All @@ -248,6 +258,7 @@ impl From<mesh_prost::Triangle> for Triangle {
#[cfg_attr(feature = "wiring", derive(Wiring, Unwiring))]
pub struct Mesh {
#[cfg_attr(feature = "bilrost", bilrost(encoding(packed)))]
#[cfg_attr(feature = "minicbor", n(0))]
pub triangles: Vec<Triangle>,
}

Expand Down
Loading

0 comments on commit 129d68a

Please sign in to comment.