Skip to content

Commit

Permalink
feat!: Compress binary ECCs using zlib
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Jul 22, 2024
1 parent 903278f commit 70eacd1
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 34 deletions.
98 changes: 73 additions & 25 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ typetag = "0.2.8"
urlencoding = "2.1.2"
webbrowser = "1.0.0"
cool_asserts = "2.0.3"
zstd = "0.13.2"
6 changes: 5 additions & 1 deletion badger-optimiser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ publish = false
[dependencies]
clap = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
tket2 = { path = "../tket2", features = ["portmatching", "rewrite-tracing"] }
tket2 = { path = "../tket2", features = [
"portmatching",
"rewrite-tracing",
"binary-eccs",
] }
hugr = { workspace = true }
itertools = { workspace = true }
tket-json-rs = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion compile-rewriter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ publish = false

[dependencies]
clap = { workspace = true, features = ["derive"] }
tket2 = { path = "../tket2", features = ["portmatching"] }
tket2 = { path = "../tket2", features = ["portmatching", "binary-eccs"] }
hugr = { workspace = true }
itertools = { workspace = true }
Binary file added test_files/nam_4_2.rwr
Binary file not shown.
Binary file modified test_files/nam_6_3.rwr
Binary file not shown.
Binary file modified test_files/small_eccs.rwr
Binary file not shown.
1 change: 1 addition & 0 deletions tket2-py/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bench = false
[dependencies]
tket2 = { path = "../tket2", version = "0.1.0-alpha.1", features = [
"portmatching",
"binary-eccs",
] }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
Expand Down
Binary file modified tket2-py/tket2/data/nam_6_3.rwr
Binary file not shown.
6 changes: 5 additions & 1 deletion tket2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ portmatching = ["dep:portmatching", "dep:rmp-serde"]
# Stores a trace of the applied rewrites
rewrite-tracing = []

default = []
# Support compressed binary encoded ECC files
binary-eccs = ["dep:zstd"]

default = ["binary-eccs"]

[dependencies]
lazy_static = { workspace = true }
Expand Down Expand Up @@ -62,6 +65,7 @@ chrono = { workspace = true }
bytemuck = { workspace = true }
crossbeam-channel = { workspace = true }
tracing = { workspace = true }
zstd = { workspace = true, optional = true }

[dev-dependencies]
rstest = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions tket2/src/optimiser/badger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ mod badger_default {
}

/// A sane default optimiser using a precompiled binary rewriter.
#[cfg(feature = "binary-eccs")]
pub fn default_with_rewriter_binary(
rewriter_path: impl AsRef<Path>,
) -> Result<Self, RewriterSerialisationError> {
Expand Down
21 changes: 15 additions & 6 deletions tket2/src/rewrite/ecc_rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,24 @@ impl ECCRewriter {
///
/// Precomputed rewriters can be serialised as binary and then loaded
/// later using [`ECCRewriter::load_binary_io`].
#[cfg(feature = "binary-eccs")]
pub fn save_binary_io<W: io::Write>(
&self,
writer: &mut W,
) -> Result<(), RewriterSerialisationError> {
rmp_serde::encode::write(writer, &self)?;
let mut encoder = zstd::Encoder::new(writer, 9)?;
rmp_serde::encode::write(&mut encoder, &self)?;
encoder.finish()?;
Ok(())
}

/// Load a rewriter from an IO stream.
///
/// Loads streams as created by [`ECCRewriter::save_binary_io`].
#[cfg(feature = "binary-eccs")]
pub fn load_binary_io<R: io::Read>(reader: &mut R) -> Result<Self, RewriterSerialisationError> {
let matcher: Self = rmp_serde::decode::from_read(reader)?;
Ok(matcher)
let data = zstd::decode_all(reader)?;
Ok(rmp_serde::decode::from_slice(&data)?)
}

/// Save a rewriter as a binary file.
Expand All @@ -144,6 +148,7 @@ impl ECCRewriter {
/// `.rwr`.
///
/// If successful, returns the path to the newly created file.
#[cfg(feature = "binary-eccs")]
pub fn save_binary(
&self,
name: impl AsRef<Path>,
Expand All @@ -157,10 +162,14 @@ impl ECCRewriter {
}

/// Loads a rewriter saved using [`ECCRewriter::save_binary`].
///
/// Requires the `binary-eccs` feature to be enabled.
#[cfg(feature = "binary-eccs")]
pub fn load_binary(name: impl AsRef<Path>) -> Result<Self, RewriterSerialisationError> {
let file = File::open(name)?;
let mut reader = std::io::BufReader::new(file);
Self::load_binary_io(&mut reader)
let mut file = File::open(name)?;
// Note: Buffering does not improve performance when using
// `zstd::decode_all`.
Self::load_binary_io(&mut file)
}
}

Expand Down

0 comments on commit 70eacd1

Please sign in to comment.