Skip to content

Commit

Permalink
Compress signature data
Browse files Browse the repository at this point in the history
  • Loading branch information
emesare committed Nov 4, 2024
1 parent f3ae4bd commit 8572bd1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ flatbuffers = "24.3.25"
bon = "2.3.0"
uuid = { version = "1.11.0", features = ["v5"]}
rand = "0.8.5"
flate2 = "1.0.34"

[features]
default = []
Expand Down
16 changes: 11 additions & 5 deletions rust/signature.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::io::Write;

Check warning on line 1 in rust/signature.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/warp/warp/rust/signature.rs
use crate::fb_sig as fb;
use crate::r#type::ComputedType;
use crate::signature::function::Function;
use flatbuffers::{FlatBufferBuilder, WIPOffset};

Check warning on line 5 in rust/signature.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/warp/warp/rust/signature.rs
use flate2::Compression;

pub mod basic_block;
pub mod function;
Expand All @@ -18,23 +20,25 @@ impl Data {
}

pub fn from_bytes(buf: &[u8]) -> Option<Self> {
let mut decoder = flate2::read::GzDecoder::new(buf);
let mut decompressed_data = Vec::new();
std::io::copy(&mut decoder, &mut decompressed_data).ok()?;
let opts = flatbuffers::VerifierOptions {
// Trust me bro.
max_tables: 10_000_000,
..Default::default()
};
flatbuffers::root_with_opts::<fb::Data>(&opts, buf)
flatbuffers::root_with_opts::<fb::Data>(&opts, &decompressed_data)
.ok()
.map(Into::into)
}

pub fn deduplicate(&mut self) {
// Sort and remove types with the same guid.
self.types.sort_unstable_by(|a, b| a.guid.cmp(&b.guid));
self.types.sort_by_key(|ty| ty.guid);
self.types.dedup_by_key(|ty| ty.guid);
// Sort and remove functions with the same symbol and guid.
self.functions
.sort_unstable_by(|a, b| a.symbol.name.cmp(&b.symbol.name));
self.functions.sort_by_key(|func| func.guid);
self.functions.dedup_by(|a, b| {
if a.guid == b.guid {
// Keep `a`s constraints.
Expand Down Expand Up @@ -71,7 +75,9 @@ impl Data {
let mut builder = FlatBufferBuilder::new();
let fb_data = self.create(&mut builder);

Check warning on line 76 in rust/signature.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/warp/warp/rust/signature.rs
builder.finish_minimal(fb_data);
builder.finished_data().to_vec()
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), Compression::default());
encoder.write_all(builder.finished_data()).expect("Failed to compress data");
encoder.finish().expect("Failed to finish compression")
}

pub(crate) fn create<'a>(
Expand Down

0 comments on commit 8572bd1

Please sign in to comment.