feat(compress): move deprecated block-size formula to Rust

Keep validation of the applied compression parameters in C and delegate the context-free minimum calculation through a narrow ABI leaf. Preserve the public getter and deprecated block-compression paths.

Test Plan:

- cargo test --manifest-path rust/Cargo.toml --no-default-features --features compression

- cargo clippy --manifest-path rust/Cargo.toml --no-default-features --features compression --all-targets

- cargo +nightly fmt --manifest-path rust/Cargo.toml

- make -B -C lib -j2 lib

- make -C tests -j2 fuzzer

- tests/fuzzer -s4560 -t47 -i48 -v
This commit is contained in:
2026-07-18 08:33:07 +02:00
parent 7f22db844d
commit 33c937bc87
2 changed files with 20 additions and 1 deletions
+2 -1
View File
@@ -103,6 +103,7 @@ ZSTD_parameters ZSTD_rust_params_makeParams(ZSTD_compressionParameters cParams);
size_t ZSTD_rust_params_maxNbSeq(size_t blockSize, U32 minMatch,
int useSequenceProducer);
size_t ZSTD_rust_params_resolveMaxBlockSize(size_t maxBlockSize);
size_t ZSTD_rust_params_getBlockSize(size_t maxBlockSize, U32 windowLog);
int ZSTD_rust_params_rowMatchFinderSupported(int strategy);
int ZSTD_rust_params_rowMatchFinderUsed(int strategy, int mode);
int ZSTD_rust_params_resolveRowMatchFinderMode(
@@ -3350,7 +3351,7 @@ static size_t ZSTD_getBlockSize_deprecated(const ZSTD_CCtx* cctx)
{
ZSTD_compressionParameters const cParams = cctx->appliedParams.cParams;
assert(!ZSTD_checkCParams(cParams));
return MIN(cctx->appliedParams.maxBlockSize, (size_t)1 << cParams.windowLog);
return ZSTD_rust_params_getBlockSize(cctx->appliedParams.maxBlockSize, cParams.windowLog);
}
/* NOTE: Must just wrap ZSTD_getBlockSize_deprecated() */
+18
View File
@@ -900,6 +900,17 @@ pub extern "C" fn ZSTD_rust_params_resolveMaxBlockSize(maxBlockSize: usize) -> u
}
}
#[inline]
fn get_block_size(max_block_size: usize, window_log: u32) -> usize {
max_block_size.min(1usize << window_log)
}
/// Pure leaf for the deprecated `ZSTD_getBlockSize()` path.
#[no_mangle]
pub extern "C" fn ZSTD_rust_params_getBlockSize(maxBlockSize: usize, windowLog: u32) -> usize {
get_block_size(maxBlockSize, windowLog)
}
fn estimate_cdict_size_from_cparams(
dict_size: usize,
cparams: ZSTD_compressionParameters,
@@ -1319,4 +1330,11 @@ mod tests {
assert_eq!(ZSTD_rust_params_maxNbSeq(100, 4, 0), 25);
assert_eq!(ZSTD_rust_params_resolveMaxBlockSize(0), 128 * 1024);
}
#[test]
fn block_size_uses_the_smaller_configured_limit() {
assert_eq!(ZSTD_rust_params_getBlockSize(64 * 1024, 17), 64 * 1024);
assert_eq!(ZSTD_rust_params_getBlockSize(128 * 1024, 16), 64 * 1024);
assert_eq!(ZSTD_rust_params_getBlockSize(128 * 1024, 17), 128 * 1024);
}
}