From fab2257317c3d2d7faf2697ec223964bd92afa4a Mon Sep 17 00:00:00 2001 From: Amos Wenger Date: Tue, 29 Oct 2024 18:36:56 +0100 Subject: [PATCH] Allow overriding compression levels with environment variables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This introduces three environment variables, with the following defaults: * `AXOASSET_GZ_LEVEL=6` * `AXOASSET_XZ_LEVEL=9` * `AXOASSET_ZSTD_LEVEL=3` (The defaults are unchanged, but when we iterate on dist builds using dist itself, exporting `AXOASSET_XZ_LEVEL=1` will make it go faster — it is _genuinely_ the blocker on warm builds lol) --- Cargo.lock | 5 +++-- Cargo.toml | 1 + src/compression.rs | 38 ++++++++++++++++++++++++++++++-------- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f7a4b07..d6f9e5a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -136,6 +136,7 @@ dependencies = [ "clap", "flate2", "image", + "lazy_static", "miette", "mime", "reqwest", @@ -908,9 +909,9 @@ dependencies = [ [[package]] name = "lazy_static" -version = "1.4.0" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" diff --git a/Cargo.toml b/Cargo.toml index 5e21c38..920a244 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,6 +45,7 @@ xz2 = { version = "0.1.7", optional = true, features = ["static"] } zstd = { version = "0.13.0", optional = true } toml_edit = { version = "0.22.5", optional = true } walkdir = "2.5.0" +lazy_static = "1.5.0" [dev-dependencies] assert_fs = "1" diff --git a/src/compression.rs b/src/compression.rs index cf917c5..3883967 100644 --- a/src/compression.rs +++ b/src/compression.rs @@ -18,6 +18,27 @@ pub(crate) enum CompressionImpl { Zstd, } +lazy_static::lazy_static! { + static ref DEFAULT_GZ_LEVEL: u32 = { + std::env::var("AXOASSET_GZ_LEVEL") + .ok() + .and_then(|val| val.parse().ok()) + .unwrap_or(6) + }; + static ref DEFAULT_XZ_LEVEL: u32 = { + std::env::var("AXOASSET_XZ_LEVEL") + .ok() + .and_then(|val| val.parse().ok()) + .unwrap_or(9) + }; + static ref DEFAULT_ZSTD_LEVEL: i32 = { + std::env::var("AXOASSET_ZSTD_LEVEL") + .ok() + .and_then(|val| val.parse().ok()) + .unwrap_or(3) + }; +} + #[cfg(feature = "compression-tar")] pub(crate) fn tar_dir( src_path: &Utf8Path, @@ -52,7 +73,7 @@ pub(crate) fn tar_dir( // Wrap our file in compression let zip_output = GzBuilder::new() .filename(zip_contents_name) - .write(final_zip_file, Compression::default()); + .write(final_zip_file, Compression::new(*DEFAULT_GZ_LEVEL)); // Write the tar to the compression stream let mut tar = tar::Builder::new(zip_output); @@ -87,7 +108,7 @@ pub(crate) fn tar_dir( // Drop the file to close it } CompressionImpl::Xzip => { - let zip_output = XzEncoder::new(final_zip_file, 9); + let zip_output = XzEncoder::new(final_zip_file, *DEFAULT_XZ_LEVEL); // Write the tar to the compression stream let mut tar = tar::Builder::new(zip_output); @@ -122,12 +143,13 @@ pub(crate) fn tar_dir( } CompressionImpl::Zstd => { // Wrap our file in compression - let zip_output = ZstdEncoder::new(final_zip_file, 0).map_err(|details| { - AxoassetError::Compression { - reason: "failed to create zstd encoder".to_string(), - details, - } - })?; + let zip_output = + ZstdEncoder::new(final_zip_file, *DEFAULT_ZSTD_LEVEL).map_err(|details| { + AxoassetError::Compression { + reason: "failed to create zstd encoder".to_string(), + details, + } + })?; // Write the tar to the compression stream let mut tar = tar::Builder::new(zip_output);