Skip to content

Commit

Permalink
Adjust xz compression settings
Browse files Browse the repository at this point in the history
This adjusts our compression settings for xz compressed files, which
should reduce download sizes by around 5-25%, at the cost of ~44% longer
compression times. Given that compression happens almost universally in
CI, rather than something humans are directly waiting for, this tradeoff
feels worth it.

If we end up seeing this be too significant an increase in CI times,
moving the more aggressive compression solely to promote-release should
be relatively straightforward and give most of the benefits.
  • Loading branch information
Mark-Simulacrum committed Feb 27, 2023
1 parent 9981e4d commit 06976f6
Showing 1 changed file with 38 additions and 12 deletions.
50 changes: 38 additions & 12 deletions src/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,37 @@ impl CompressionFormat {
Ok(match self {
CompressionFormat::Gz => Box::new(GzEncoder::new(file, flate2::Compression::best())),
CompressionFormat::Xz => {
// Note that preset 6 takes about 173MB of memory per thread, so we limit the number of
// threads to not blow out 32-bit hosts. (We could be more precise with
// `MtStreamBuilder::memusage()` if desired.)
let stream = xz2::stream::MtStreamBuilder::new()
.threads(Ord::min(num_cpus::get(), 8) as u32)
.preset(6)
.encoder()?;
Box::new(XzEncoder::new_stream(file, stream))
let mut filters = xz2::stream::Filters::new();
// the preset is overridden by the other options so it doesn't matter
let mut lzma_ops = xz2::stream::LzmaOptions::new_preset(9).unwrap();
// This sets the overall dictionary size, which is also how much memory (baseline)
// is needed for decompression.
lzma_ops.dict_size(128 * 1024 * 1024);
// Use the best match finder for compression ratio.
lzma_ops.match_finder(xz2::stream::MatchFinder::BinaryTree4);
lzma_ops.mode(xz2::stream::Mode::Normal);
// Set nice len to the maximum for best compression ratio
lzma_ops.nice_len(273);
// Set depth to a reasonable value, 0 means auto, 1000 is somwhat high but gives
// good results.
lzma_ops.depth(1000);
// 2 is the default and does well for most files
lzma_ops.position_bits(2);
// 0 is the default and does well for most files
lzma_ops.literal_position_bits(0);
// 3 is the default and does well for most files
lzma_ops.literal_context_bits(3);

filters.lzma2(&lzma_ops);
let compressor = XzEncoder::new_stream(
std::io::BufWriter::new(file),
xz2::stream::MtStreamBuilder::new()
.threads(1)
.filters(filters)
.encoder()
.unwrap(),
);
Box::new(compressor)
}
})
}
Expand Down Expand Up @@ -94,10 +117,13 @@ impl fmt::Display for CompressionFormats {
if i != 0 {
write!(f, ",")?;
}
fmt::Display::fmt(match format {
CompressionFormat::Xz => "xz",
CompressionFormat::Gz => "gz",
}, f)?;
fmt::Display::fmt(
match format {
CompressionFormat::Xz => "xz",
CompressionFormat::Gz => "gz",
},
f,
)?;
}
Ok(())
}
Expand Down

0 comments on commit 06976f6

Please sign in to comment.