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
+63
View File
@@ -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 =