From a3e5b536e38d4f9a1cc0f8aa3bf5927d90701ef7 Mon Sep 17 00:00:00 2001 From: Aron Parker Date: Sun, 8 Jan 2023 12:42:42 +0100 Subject: [PATCH] Add compress_estimate_max_mem_usage --- Cargo.toml | 2 +- brotlic-sys/Cargo.toml | 2 +- brotlic-sys/brotli | 2 +- brotlic-sys/build.rs | 1 + src/lib.rs | 16 ++++++++++++++++ tests/memory.rs | 23 ++++++++++++++++++++++- 6 files changed, 42 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a6c0d35..50749d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "brotlic" -version = "0.8.0" +version = "0.8.1" edition = "2021" authors = ["Aron Parker ", "The Brotli Authors"] description = """ diff --git a/brotlic-sys/Cargo.toml b/brotlic-sys/Cargo.toml index f4f8099..aeaaa09 100644 --- a/brotlic-sys/Cargo.toml +++ b/brotlic-sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "brotlic-sys" -version = "0.2.0" +version = "0.2.1" edition = "2021" authors = ["Aron Parker "] description = """ diff --git a/brotlic-sys/brotli b/brotlic-sys/brotli index f4153a0..71fe6ca 160000 --- a/brotlic-sys/brotli +++ b/brotlic-sys/brotli @@ -1 +1 @@ -Subproject commit f4153a09f87cbb9c826d8fc12c74642bb2d879ea +Subproject commit 71fe6cac061ac62c0241f410fbd43a04a6b4f303 diff --git a/brotlic-sys/build.rs b/brotlic-sys/build.rs index 4446511..7a4f1b2 100644 --- a/brotlic-sys/build.rs +++ b/brotlic-sys/build.rs @@ -41,6 +41,7 @@ fn main() { ]) .include("brotli/c/include") .define("BROTLI_BUILD_ENC_EXTRA_API", None) + .define("BROTLI_HAVE_LOG2", "1") .warnings(false) .compile("brotli"); diff --git a/src/lib.rs b/src/lib.rs index 349cf02..c387228 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -920,6 +920,22 @@ pub fn compress_bound(input_size: usize, quality: Quality) -> Option { } } +/// Returns peak memory usage for a given quality and window size +/// +/// Given an input of `input_size` bytes in size, a `quality` and a +/// `window_size`, estimate the peak memory usage in bytes, not counting the +/// memory needed for the input and output. +#[doc(alias = "BrotliEncoderEstimatePeakMemoryUsage")] +pub fn compress_estimate_max_mem_usage( + input_size: usize, + quality: Quality, + window_size: impl Into, +) -> usize { + unsafe { + BrotliEncoderEstimatePeakMemoryUsage(quality.0 as _, window_size.into().0 as _, input_size) + } +} + /// Read all bytes from `input` and decompress them into `output`, returning how /// many bytes were written. /// diff --git a/tests/memory.rs b/tests/memory.rs index a45cfd8..078656f 100644 --- a/tests/memory.rs +++ b/tests/memory.rs @@ -1,4 +1,4 @@ -use brotlic::{CompressionMode, Quality, WindowSize}; +use brotlic::{CompressionMode, LargeWindowSize, Quality, WindowSize}; mod common; @@ -72,3 +72,24 @@ fn test_medium_entropy_large() { fn test_max_entropy_large() { verify(common::gen_max_entropy(8192).as_slice()); } + +#[test] +fn test_encoder_estimate_peak_memory_usage() { + let usage100 = + brotlic::compress_estimate_max_mem_usage(100, Quality::best(), WindowSize::best()); + + assert!(usage100 > 0); +} + +#[test] +fn test_google_brotli_issue_1001() { + let window_size = + brotlic::compress_estimate_max_mem_usage(1024 * 1024, Quality::best(), WindowSize::best()); + let large_window_size = brotlic::compress_estimate_max_mem_usage( + 1024 * 1024, + Quality::best(), + LargeWindowSize::best(), + ); + + assert!(large_window_size > window_size); +}