From c0038a8aaa7489ba2f225c5ac9c4ae0f7fa08542 Mon Sep 17 00:00:00 2001 From: ddidderr Date: Sat, 18 Jul 2026 13:39:04 +0200 Subject: [PATCH] feat(compress): move CCtx estimate max to Rust ZSTD_estimateCCtxSize_internal() evaluates four fixed source-size tiers and must retain every C-owned parameter and sizing call, including raw error-shaped size_t results. Store those four results in C, then delegate only the final unsigned maximum comparison to a scalar Rust ABI helper. The outer monotonic compression-level budget and the separate CStream estimation path remain unchanged. The Rust unit tests cover zero, ordinary, equal, SIZE_MAX, and encoded error values to document that the helper compares raw size_t values without arithmetic or error interpretation. Test Plan: - `cargo clippy --manifest-path rust/Cargo.toml --no-default-features --features compression` -- passed before and after formatting - Same clippy command with `--benches` -- passed before and after formatting - Same clippy command with `--tests` -- passed before and after formatting - `cargo +nightly fmt --manifest-path rust/Cargo.toml` -- passed - `cargo test --manifest-path rust/Cargo.toml --no-default-features --features compression` -- 328 passed - `make -B -C lib -j2 lib` -- passed - `make -C tests test-rust-lib-smoke` -- passed - `tests/fuzzer -s4560 -t56 -i57 -v` -- passed - `make -C tests -j2 test-zstream` -- 84 named plus 5,923 and 9,247 randomized cases passed - `git diff --check` and `git diff --cached --check` -- passed The zstream build retains the pre-existing initializer-string warning at tests/zstreamtest.c:1899. --- lib/compress/zstd_compress.c | 9 ++++-- rust/src/zstd_compress.rs | 63 ++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 8c8e6e40d..238b98308 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -103,6 +103,8 @@ size_t ZSTD_rust_estimateWorkspaceSize(size_t cctxSpace, size_t tokenSpace, size_t bufferSpace, size_t externalSeqSpace); +size_t ZSTD_rust_maxEstimateCCtxSize(size_t estimate0, size_t estimate1, + size_t estimate2, size_t estimate3); ZSTD_inBuffer ZSTD_rust_inBufferForEndFlush(int inBufferMode, const void* expectedSrc, size_t expectedSize, @@ -1346,14 +1348,15 @@ size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams) static size_t ZSTD_estimateCCtxSize_internal(int compressionLevel) { int tier = 0; - size_t largestSize = 0; + size_t estimates[4]; static const unsigned long long srcSizeTiers[4] = {16 KB, 128 KB, 256 KB, ZSTD_CONTENTSIZE_UNKNOWN}; for (; tier < 4; ++tier) { /* Choose the set of cParams for a given level across all srcSizes that give the largest cctxSize */ ZSTD_compressionParameters const cParams = ZSTD_getCParams_internal(compressionLevel, srcSizeTiers[tier], 0, ZSTD_cpm_noAttachDict); - largestSize = MAX(ZSTD_estimateCCtxSize_usingCParams(cParams), largestSize); + estimates[tier] = ZSTD_estimateCCtxSize_usingCParams(cParams); } - return largestSize; + return ZSTD_rust_maxEstimateCCtxSize( + estimates[0], estimates[1], estimates[2], estimates[3]); } size_t ZSTD_estimateCCtxSize(int compressionLevel) diff --git a/rust/src/zstd_compress.rs b/rust/src/zstd_compress.rs index 5255dd8b4..d282938af 100644 --- a/rust/src/zstd_compress.rs +++ b/rust/src/zstd_compress.rs @@ -611,6 +611,27 @@ pub extern "C" fn ZSTD_rust_estimateWorkspaceSize( ) } +#[inline] +fn max_estimate_cctx_size( + estimate0: usize, + estimate1: usize, + estimate2: usize, + estimate3: usize, +) -> usize { + estimate0.max(estimate1).max(estimate2).max(estimate3) +} + +/// Return the largest raw estimate, including any C size_t error values. +#[no_mangle] +pub extern "C" fn ZSTD_rust_maxEstimateCCtxSize( + estimate0: usize, + estimate1: usize, + estimate2: usize, + estimate3: usize, +) -> usize { + max_estimate_cctx_size(estimate0, estimate1, estimate2, estimate3) +} + #[inline] fn reduce_table_internal(table: &mut [u32], reducer_value: u32, preserve_mark: bool) { debug_assert_eq!(table.len() % ZSTD_ROWSIZE, 0); @@ -1860,6 +1881,48 @@ mod tests { ); } + #[test] + fn max_estimate_cctx_size_handles_zero_values() { + assert_eq!(max_estimate_cctx_size(0, 0, 0, 0), 0); + assert_eq!(ZSTD_rust_maxEstimateCCtxSize(0, 0, 0, 0), 0); + } + + #[test] + fn max_estimate_cctx_size_selects_the_largest_ordinary_value() { + assert_eq!(max_estimate_cctx_size(17, 42, 9, 31), 42); + assert_eq!(ZSTD_rust_maxEstimateCCtxSize(17, 42, 9, 31), 42); + } + + #[test] + fn max_estimate_cctx_size_preserves_equal_values() { + assert_eq!(max_estimate_cctx_size(42, 42, 42, 42), 42); + assert_eq!(ZSTD_rust_maxEstimateCCtxSize(42, 42, 42, 42), 42); + } + + #[test] + fn max_estimate_cctx_size_accepts_size_max() { + assert_eq!(max_estimate_cctx_size(1, usize::MAX, 3, 2), usize::MAX); + assert_eq!( + ZSTD_rust_maxEstimateCCtxSize(1, usize::MAX, 3, 2), + usize::MAX + ); + } + + #[test] + fn max_estimate_cctx_size_compares_error_like_raw_values() { + let smaller_error = ERROR(ZstdErrorCode::DstSizeTooSmall); + let larger_error = ERROR(ZstdErrorCode::ParameterUnsupported); + + assert_eq!( + max_estimate_cctx_size(128, smaller_error, 256, larger_error), + larger_error + ); + assert_eq!( + ZSTD_rust_maxEstimateCCtxSize(128, smaller_error, 256, larger_error), + larger_error + ); + } + #[test] fn public_one_shot_abi_is_c_compatible() { let entry: unsafe extern "C" fn(*mut c_void, usize, *const c_void, usize, c_int) -> usize =