Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
Rewrite gzip buffer-building strategy to maybe be friendlier to rayon
Browse files Browse the repository at this point in the history
  • Loading branch information
caass committed Nov 3, 2020
1 parent eeb38cf commit cbbed17
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions src/build/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,46 +251,54 @@ impl<P: AsRef<Path> + Debug> Parseable<P> for BundlerOutput {
}
}

/// Check the sizes of all the files the user wants to upload,
/// Check the bundle size of the files the user wants to upload,
/// and then lint them all. I suspect this would be a good
/// use case for rayon, but I'm reluctant to add more dependencies
/// than are absolutely necessary for this PR
impl Lintable for BundlerOutput {
fn lint(&self) -> Result<(), failure::Error> {
// Check the bundle size
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
let mut buffer = Vec::new();

iter::once(&self.module_path)
let buffer = iter::once(&self.module_path)
.chain(self.javascript.keys())
.chain(self.webassembly.keys())
.chain(&self.other_files)
.try_for_each(|path: &PathBuf| -> Result<(), failure::Error> {
.map(|path| -> Result<Vec<u8>, failure::Error> {
let mut file = File::open(path)?;
file.read_to_end(&mut buffer)?;
Ok(())
let mut buf = Vec::new();
file.read_to_end(&mut buf)?;
Ok(buf)
})
.try_fold(Vec::new(), |mut buffer, read_result| match read_result {
Ok(file_buf) => {
buffer.extend(file_buf);
Ok(buffer)
}
Err(e) => Err(e),
})?;

encoder.write_all(&buffer)?;

let compressed_size = encoder.finish()?.len() as u64;
let human_compressed_size = match NumberPrefix::binary(compressed_size as f64) {
NumberPrefix::Standalone(bytes) => format!("{} bytes", bytes),
NumberPrefix::Prefixed(prefix, n) => format!("{:.0} {}B", n, prefix),
};

// i wish this was a const but
// "caLlS IN cOnSTAntS ArE LiMITed TO CoNstaNt fUNCtIoNs, TupLE sTrUCts aND TUpLE VaRiANTs"
// or whatever
let human_max_size = match NumberPrefix::binary(MAX_BUNDLE_SIZE as f64) {
NumberPrefix::Standalone(bytes) => format!("{} bytes", bytes),
NumberPrefix::Prefixed(prefix, n) => format!("{:.0} {}B", n, prefix),
};

// warn, but continue
if compressed_size > MAX_BUNDLE_SIZE {
let human_compressed_size = match NumberPrefix::binary(compressed_size as f64) {
NumberPrefix::Standalone(bytes) => format!("{} bytes", bytes),
NumberPrefix::Prefixed(prefix, n) => format!("{:.0} {}B", n, prefix),
};

// i wish this was a const but
// "caLlS IN cOnSTAntS ArE LiMITed TO CoNstaNt fUNCtIoNs, TupLE sTrUCts aND TUpLE VaRiANTs"
// or whatever
let human_max_size = match NumberPrefix::binary(MAX_BUNDLE_SIZE as f64) {
NumberPrefix::Standalone(bytes) => format!("{} bytes", bytes),
NumberPrefix::Prefixed(prefix, n) => format!("{:.0} {}B", n, prefix),
};

println!(
"Your project ({}) has exceeded the {} limit and may fail to deploy.",
"Your project ({}) exceeds the {} limit and may fail to deploy.",
human_compressed_size, human_max_size
);
}
Expand Down

0 comments on commit cbbed17

Please sign in to comment.