Skip to content

Commit

Permalink
add bilrost (#72)
Browse files Browse the repository at this point in the history
* add bilrost

* regenerate prost files
  • Loading branch information
mumbleskates authored Apr 7, 2024
1 parent ef95abb commit 57a77a7
Show file tree
Hide file tree
Showing 14 changed files with 647 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ alkahest = { version = "0.1.5", optional = true, features = [
"nightly",
] }
bebop = { version = "2.4.9", optional = true }
bilrost = { version = "0.1005.1", optional = true }
bincode1 = { package = "bincode", version = "1.3.3", optional = true }
# Can't call it bincode2 because of a current issue of bincode2
bincode = { package = "bincode", version = "2.0.0-rc", optional = true }
Expand Down Expand Up @@ -99,6 +100,7 @@ default = [
"abomonation_derive",
"alkahest",
# "bebop",
"bilrost",
"bincode1",
"bincode",
"bitcode",
Expand Down
14 changes: 14 additions & 0 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use rand_pcg::Lcg64Xsh32;
use rust_serialization_benchmark::bench_abomonation;
#[cfg(feature = "alkahest")]
use rust_serialization_benchmark::bench_alkahest;
#[cfg(feature = "bilrost")]
use rust_serialization_benchmark::bench_bilrost;
#[cfg(feature = "bincode")]
use rust_serialization_benchmark::bench_bincode;
#[cfg(feature = "bincode1")]
Expand Down Expand Up @@ -97,6 +99,9 @@ fn bench_log(c: &mut Criterion) {
}
});

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

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

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

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

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

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

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

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

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

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

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

Expand Down
106 changes: 106 additions & 0 deletions src/bench_bilrost.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use bilrost::buf::ReverseBuffer;
use bilrost::bytes::BufMut;
use bilrost::Message;
use criterion::{black_box, Criterion};
use std::borrow::Borrow;

pub trait ToBilrost: Sized {
type Message: Message + Into<Self>;
type Serializable<'a>: Borrow<Self::Message>
where
Self: 'a;

fn to_bilrost(&self) -> Self::Serializable<'_>;

fn is_already_bilrost() -> bool {
false // provided: false
}
}

impl<T> ToBilrost for T
where
T: Clone + Message,
{
type Message = Self;
type Serializable<'a> = &'a Self
where
Self: 'a;

fn to_bilrost(&self) -> &Self {
self
}

fn is_already_bilrost() -> bool {
true // true for this covering impl only
}
}

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

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

let mut serialize_buffer = Vec::with_capacity(BUFFER_LEN);
let mut prepend_buffer = ReverseBuffer::with_capacity(BUFFER_LEN);

if !T::is_already_bilrost() {
group.bench_function("serialize (populate + encode)", |b| {
b.iter(|| {
black_box(&mut serialize_buffer).clear();
data.to_bilrost()
.borrow()
.encode(&mut serialize_buffer)
.unwrap();
black_box(());
})
});
group.bench_function("serialize (populate + prepend)", |b| {
b.iter(|| {
black_box(&mut prepend_buffer).clear();
data.to_bilrost().borrow().prepend(&mut prepend_buffer);
black_box(());
})
});
}

let message = data.to_bilrost();
group.bench_function("serialize (only encode)", |b| {
b.iter(|| {
black_box(&mut serialize_buffer).clear();
message.borrow().encode(&mut serialize_buffer).unwrap();
black_box(());
})
});

let message = data.to_bilrost();
group.bench_function("serialize (only prepend)", |b| {
b.iter(|| {
black_box(&mut prepend_buffer).clear();
message.borrow().prepend(&mut prepend_buffer);
black_box(());
})
});

let mut deserialize_buffer = Vec::new();
message.borrow().encode(&mut deserialize_buffer).unwrap();
let mut prepended_data = Vec::new();
prepended_data.put(message.borrow().encode_fast());
// Because there are no unordered collections in the benchmarked types, we can assert that the
// prepended encoding path emits precisely the same bytes as the forward-encoded one.
assert_eq!(prepended_data, deserialize_buffer);

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

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

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

group.finish();
}
8 changes: 8 additions & 0 deletions src/datasets/log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use crate::Generate;

#[derive(Clone, Copy, PartialEq)]
#[cfg_attr(feature = "abomonation", derive(abomonation_derive::Abomonation))]
#[cfg_attr(feature = "bilrost", derive(bilrost::Message))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
#[cfg_attr(
Expand Down Expand Up @@ -59,9 +60,13 @@ use crate::Generate;
#[cfg_attr(feature = "alkahest", derive(alkahest::Schema))]
#[cfg_attr(feature = "nanoserde", derive(nanoserde::SerBin, nanoserde::DeBin))]
pub struct Address {
#[cfg_attr(feature = "bilrost", bilrost(encoding(varint)))]
pub x0: u8,
#[cfg_attr(feature = "bilrost", bilrost(encoding(varint)))]
pub x1: u8,
#[cfg_attr(feature = "bilrost", bilrost(encoding(varint)))]
pub x2: u8,
#[cfg_attr(feature = "bilrost", bilrost(encoding(varint)))]
pub x3: u8,
}

Expand Down Expand Up @@ -141,6 +146,7 @@ impl alkahest::Pack<Address> for Address {

#[derive(Clone, PartialEq)]
#[cfg_attr(feature = "abomonation", derive(abomonation_derive::Abomonation))]
#[cfg_attr(feature = "bilrost", derive(bilrost::Message))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
#[cfg_attr(
Expand Down Expand Up @@ -365,6 +371,7 @@ impl alkahest::Pack<LogSchema> for &'_ Log {

#[derive(Clone, PartialEq)]
#[cfg_attr(feature = "abomonation", derive(abomonation_derive::Abomonation))]
#[cfg_attr(feature = "bilrost", derive(bilrost::Message))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
#[cfg_attr(
Expand All @@ -391,6 +398,7 @@ impl alkahest::Pack<LogSchema> for &'_ Log {
#[cfg_attr(feature = "savefile", derive(savefile_derive::Savefile))]
#[cfg_attr(feature = "nanoserde", derive(nanoserde::SerBin, nanoserde::DeBin))]
pub struct Logs {
#[cfg_attr(feature = "bilrost", bilrost(encoding(packed)))]
pub logs: Vec<Log>,
}

Expand Down
1 change: 1 addition & 0 deletions src/datasets/log/prost.log.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// This file is @generated by prost-build.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Address {
Expand Down
4 changes: 4 additions & 0 deletions src/datasets/mesh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::Generate;

#[derive(Clone, Copy, PartialEq)]
#[cfg_attr(feature = "abomonation", derive(abomonation_derive::Abomonation))]
#[cfg_attr(feature = "bilrost", derive(bilrost::Message))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
#[cfg_attr(
Expand Down Expand Up @@ -133,6 +134,7 @@ impl alkahest::Pack<Vector3> for Vector3 {

#[derive(Clone, Copy, PartialEq)]
#[cfg_attr(feature = "abomonation", derive(abomonation_derive::Abomonation))]
#[cfg_attr(feature = "bilrost", derive(bilrost::Message))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
#[cfg_attr(
Expand Down Expand Up @@ -248,6 +250,7 @@ impl alkahest::Pack<Triangle> for &'_ Triangle {

#[derive(Clone, PartialEq)]
#[cfg_attr(feature = "abomonation", derive(abomonation_derive::Abomonation))]
#[cfg_attr(feature = "bilrost", derive(bilrost::Message))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
#[cfg_attr(
Expand All @@ -274,6 +277,7 @@ impl alkahest::Pack<Triangle> for &'_ Triangle {
#[cfg_attr(feature = "savefile", derive(savefile_derive::Savefile))]
#[cfg_attr(feature = "nanoserde", derive(nanoserde::SerBin, nanoserde::DeBin))]
pub struct Mesh {
#[cfg_attr(feature = "bilrost", bilrost(encoding(packed)))]
pub triangles: Vec<Triangle>,
}

Expand Down
1 change: 1 addition & 0 deletions src/datasets/mesh/prost.mesh.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// This file is @generated by prost-build.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Vector3 {
Expand Down
Loading

0 comments on commit 57a77a7

Please sign in to comment.