feat(compress): move streaming size helpers to Rust
The public streaming-size functions still calculated their values in the C compressor entry point. Keep those public symbols and their size_t ABI in C, but route the pure formulas through zstd_compress_api.rs. Rust uses usize for the C size_t-equivalent constants and reuses its compressBound implementation; the fixed block and header sizes make the output helper exactly 131591 bytes. Test Plan: - Focused Rust cstream size test -- passed (1 test) - make -B -C lib -j2 lib -- passed - make -C tests -j2 fuzzer -- passed - ./tests/fuzzer -s4560 -t47 -i48 -v -- passed - Required clippy, nightly fmt, and staged diff checks -- passed
This commit is contained in:
@@ -79,6 +79,8 @@ int ZSTD_rust_targetCBlockSizeAction(int bss, int isFirstBlock,
|
||||
int maybeRLE, int isRLE,
|
||||
size_t cSize, size_t srcSize,
|
||||
int strategy);
|
||||
size_t ZSTD_rust_CStreamInSize(void);
|
||||
size_t ZSTD_rust_CStreamOutSize(void);
|
||||
|
||||
/* Context-free compression-parameter selection and sizing leaves live in
|
||||
* Rust (rust/src/zstd_compress_params.rs). This file retains
|
||||
@@ -4384,11 +4386,11 @@ size_t ZSTD_freeCStream(ZSTD_CStream* zcs)
|
||||
|
||||
/*====== Initialization ======*/
|
||||
|
||||
size_t ZSTD_CStreamInSize(void) { return ZSTD_BLOCKSIZE_MAX; }
|
||||
size_t ZSTD_CStreamInSize(void) { return ZSTD_rust_CStreamInSize(); }
|
||||
|
||||
size_t ZSTD_CStreamOutSize(void)
|
||||
{
|
||||
return ZSTD_compressBound(ZSTD_BLOCKSIZE_MAX) + ZSTD_blockHeaderSize + 4 /* 32-bits hash */ ;
|
||||
return ZSTD_rust_CStreamOutSize();
|
||||
}
|
||||
|
||||
static ZSTD_CParamMode_e ZSTD_getCParamMode(ZSTD_CDict const* cdict, ZSTD_CCtx_params const* params, U64 pledgedSrcSize)
|
||||
|
||||
@@ -14,6 +14,8 @@ const ZSTD_MAX_INPUT_SIZE: usize = 0xFF00_FF00;
|
||||
const SMALL_INPUT_THRESHOLD: usize = 128 << 10;
|
||||
const ZSTD_MINMATCH_MIN: usize = 3;
|
||||
const ZSTD_BLOCKSIZE_MAX_MIN: usize = 1 << 10;
|
||||
const ZSTD_BLOCKSIZE_MAX: usize = 1 << 17;
|
||||
const ZSTD_BLOCK_HEADER_SIZE: usize = 3;
|
||||
|
||||
/// ABI-compatible `ZSTD_Sequence` from `zstd.h`.
|
||||
#[repr(C)]
|
||||
@@ -41,6 +43,18 @@ pub extern "C" fn ZSTD_compressBound(src_size: usize) -> usize {
|
||||
src_size.wrapping_add(src_size >> 8).wrapping_add(margin)
|
||||
}
|
||||
|
||||
/// Returns the recommended input buffer size for the public streaming API.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn ZSTD_rust_CStreamInSize() -> usize {
|
||||
ZSTD_BLOCKSIZE_MAX
|
||||
}
|
||||
|
||||
/// Returns the recommended output buffer size for the public streaming API.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn ZSTD_rust_CStreamOutSize() -> usize {
|
||||
ZSTD_compressBound(ZSTD_BLOCKSIZE_MAX) + ZSTD_BLOCK_HEADER_SIZE + 4
|
||||
}
|
||||
|
||||
/// Returns the maximum number of public sequences generated for an input.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn ZSTD_sequenceBound(src_size: usize) -> usize {
|
||||
@@ -89,6 +103,16 @@ mod tests {
|
||||
assert!(ERR_isError(ZSTD_compressBound(ZSTD_MAX_INPUT_SIZE)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cstream_sizes_match_the_public_formulas() {
|
||||
assert_eq!(ZSTD_rust_CStreamInSize(), 128 * 1024);
|
||||
assert_eq!(
|
||||
ZSTD_rust_CStreamOutSize(),
|
||||
ZSTD_compressBound(ZSTD_BLOCKSIZE_MAX) + ZSTD_BLOCK_HEADER_SIZE + 4
|
||||
);
|
||||
assert_eq!(ZSTD_rust_CStreamOutSize(), 131_591);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sequence_bound_matches_the_public_formula() {
|
||||
assert_eq!(ZSTD_sequenceBound(0), 2);
|
||||
|
||||
Reference in New Issue
Block a user