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.
This commit is contained in:
2026-07-18 13:39:04 +02:00
parent adb6928f04
commit c0038a8aaa
2 changed files with 69 additions and 3 deletions
+6 -3
View File
@@ -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)