Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(experiment): Compare bincode against other serialisation formats #7513

Draft
wants to merge 25 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ members = [
"acvm-repo/bn254_blackbox_solver",
# Utility crates
"utils/iter-extended",
"utils/protobuf",
]
default-members = [
"tooling/nargo_cli",
Expand Down Expand Up @@ -94,6 +95,7 @@ noirc_abi = { path = "tooling/noirc_abi" }
noirc_artifacts = { path = "tooling/noirc_artifacts" }
noirc_artifacts_info = { path = "tooling/noirc_artifacts_info" }
noir_artifact_cli = { path = "tooling/artifact_cli" }
noir_protobuf = { path = "utils/protobuf" }

# Arkworks
ark-bn254 = { version = "^0.5.0", default-features = false, features = [
Expand Down Expand Up @@ -137,6 +139,11 @@ criterion = "0.5.0"
# https://github.com/tikv/pprof-rs/pull/172
pprof = { version = "0.14", features = ["flamegraph", "criterion"] }

# Protobuf
prost = "0.13"
prost-build = "0.13"
protoc-prebuilt = "0.3"

cfg-if = "1.0.0"
dirs = "4"
serde = { version = "1.0.136", features = ["derive"] }
Expand Down
8 changes: 8 additions & 0 deletions acvm-repo/acir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,23 @@ workspace = true
[dependencies]
acir_field.workspace = true
brillig.workspace = true
noir_protobuf.workspace = true

color-eyre.workspace = true
serde.workspace = true
thiserror.workspace = true
flate2.workspace = true
bincode.workspace = true
base64.workspace = true
prost.workspace = true
serde-big-array = "0.5.1"
strum = { workspace = true }
strum_macros = { workspace = true }

[build-dependencies]
prost-build.workspace = true
protoc-prebuilt.workspace = true

[dev-dependencies]
serde_json = "1.0"
serde-reflection = "0.3.6"
Expand Down
22 changes: 22 additions & 0 deletions acvm-repo/acir/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::path::Path;

fn main() {
let (protoc_bin, include_dir) =
protoc_prebuilt::init("29.3").expect("failed to initialize protoc");

std::env::set_var("PROTOC", protoc_bin);

prost_build::compile_protos(
&[
// DTOs for a `Program`, which work with the types in `acir.cpp`

Check warning on line 11 in acvm-repo/acir/build.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (DTOs)
"./src/proto/program.proto",
// DTOs for the `WitnessStack`, which work with the types in `witness.cpp`

Check warning on line 13 in acvm-repo/acir/build.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (DTOs)
"./src/proto/acir/witness.proto",
// A pared down DTO for `Program`, so Barretenberg can ignore the Brillig part.
// This is only included to make sure it compiles.
"./src/proto/acir/program.proto",
],
&[Path::new("./src/proto"), include_dir.as_path()],
)
.expect("failed to compile .proto schemas");
}
47 changes: 40 additions & 7 deletions acvm-repo/acir/src/circuit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,27 @@
}
}

impl<F: Serialize> Program<F> {
impl<F: Serialize + AcirField> Program<F> {
fn write<W: Write>(&self, writer: W) -> std::io::Result<()> {
let buf = bincode::serialize(self).unwrap();
let buf = {
// # Bincode
// bincode::serialize(self).unwrap()

// # CBOR

Check warning on line 246 in acvm-repo/acir/src/circuit/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (CBOR)
// let mut buf = Vec::new();
// ciborium::into_writer(self, &mut buf).map_err(std::io::Error::other)?;
// buf

// # FlexBuffers
// let mut s = flexbuffers::FlexbufferSerializer::new();

Check warning on line 252 in acvm-repo/acir/src/circuit/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (flexbuffers)

Check warning on line 252 in acvm-repo/acir/src/circuit/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Flexbuffer)
// self.serialize(&mut s).map_err(std::io::Error::other)?;
// s.take_buffer()

// # Protobuf
use crate::proto::convert::ProtoSchema;
use noir_protobuf::ProtoCodec;
ProtoSchema::<F>::serialize_to_vec(self)
};
let mut encoder = flate2::write::GzEncoder::new(writer, Compression::default());
encoder.write_all(&buf)?;
encoder.finish()?;
Expand All @@ -263,13 +281,28 @@
}
}

impl<F: for<'a> Deserialize<'a>> Program<F> {
impl<F: AcirField + for<'a> Deserialize<'a>> Program<F> {
fn read<R: Read>(reader: R) -> std::io::Result<Self> {
let mut gz_decoder = flate2::read::GzDecoder::new(reader);
let mut buf_d = Vec::new();
gz_decoder.read_to_end(&mut buf_d)?;
bincode::deserialize(&buf_d)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidInput, err))
let mut buf = Vec::new();
gz_decoder.read_to_end(&mut buf)?;
let result = {
// # Bincode
// bincode::deserialize(&buf)

// # CBOR

Check warning on line 293 in acvm-repo/acir/src/circuit/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (CBOR)
// ciborium::from_reader(buf.as_slice())

// # FlexBuffers
// let r = flexbuffers::Reader::get_root(buf.as_slice()).map_err(std::io::Error::other)?;

Check warning on line 297 in acvm-repo/acir/src/circuit/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (flexbuffers)
// Self::deserialize(r)

// # Protobuf
use crate::proto::convert::ProtoSchema;
use noir_protobuf::ProtoCodec;
ProtoSchema::<F>::deserialize_from_vec(&buf)
};
result.map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidInput, err))
}

/// Deserialize bytecode.
Expand Down
1 change: 1 addition & 0 deletions acvm-repo/acir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

pub mod circuit;
pub mod native_types;
mod proto;

pub use acir_field;
pub use acir_field::{AcirField, FieldElement};
Expand All @@ -18,7 +19,7 @@
mod reflection {
//! Getting test failures? You've probably changed the ACIR serialization format.
//!
//! These tests generate C++ deserializers for [`ACIR bytecode`][super::circuit::Circuit]

Check warning on line 22 in acvm-repo/acir/src/lib.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (deserializers)
//! and the [`WitnessMap`] structs. These get checked against the C++ files committed to the `codegen` folder
//! to see if changes have been to the serialization format. These are almost always a breaking change!
//!
Expand Down
Loading
Loading