From ba7cc52bccd08df8146d14e87a95645ba50fe75f Mon Sep 17 00:00:00 2001 From: ddidderr Date: Sat, 18 Jul 2026 12:44:53 +0200 Subject: [PATCH] feat(compress): move CDict size aggregation to Rust Keep ZSTD_sizeof_CDict's public behavior and workspace-sensitive layout accounting in C, while routing its final two-size_t addition through the Rust compression ABI. The Rust helper uses wrapping_add so its result matches C size_t arithmetic, including overflow. Test Plan: - `cargo clippy --manifest-path rust/Cargo.toml --no-default-features --features compression` -- passed before and after formatting - `cargo clippy --manifest-path rust/Cargo.toml --no-default-features --features compression --benches` -- passed before and after formatting - `cargo clippy --manifest-path rust/Cargo.toml --no-default-features --features compression --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 sizeof_cdict` -- 3 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, 6,809 and 9,613 randomized passed - `git diff --check` and `git diff --cached --check` -- passed --- lib/compress/zstd_compress.c | 7 +++++-- rust/src/zstd_compress.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index a14c929ba..b56ce07d5 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -88,6 +88,7 @@ size_t ZSTD_rust_nextInputSizeHint(int inBufferMode, size_t inBuffPos); size_t ZSTD_rust_CStreamInSize(void); size_t ZSTD_rust_CStreamOutSize(void); +size_t ZSTD_rust_sizeofCDict(size_t objectSize, size_t workspaceSize); size_t ZSTD_rust_sizeofLocalDict(int dictBufferPresent, size_t dictSize, size_t cdictSize); size_t ZSTD_rust_sizeofCCtx(size_t objectSize, size_t workspaceSize, @@ -4000,11 +4001,13 @@ size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel) size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict) { + size_t objectSize, workspaceSize; if (cdict==NULL) return 0; /* support sizeof on NULL */ DEBUGLOG(5, "sizeof(*cdict) : %u", (unsigned)sizeof(*cdict)); /* cdict may be in the workspace */ - return (cdict->workspace.workspace == cdict ? 0 : sizeof(*cdict)) - + ZSTD_cwksp_sizeof(&cdict->workspace); + objectSize = cdict->workspace.workspace == cdict ? 0 : sizeof(*cdict); + workspaceSize = ZSTD_cwksp_sizeof(&cdict->workspace); + return ZSTD_rust_sizeofCDict(objectSize, workspaceSize); } static size_t ZSTD_initCDict_internal( diff --git a/rust/src/zstd_compress.rs b/rust/src/zstd_compress.rs index b25c631ed..47a840ca2 100644 --- a/rust/src/zstd_compress.rs +++ b/rust/src/zstd_compress.rs @@ -485,6 +485,18 @@ pub extern "C" fn ZSTD_rust_sizeofLocalDict( sizeof_local_dict(dict_buffer_present, dict_size, cdict_size) } +#[inline] +fn sizeof_cdict(object_size: usize, workspace_size: usize) -> usize { + object_size.wrapping_add(workspace_size) +} + +/// Aggregate C-owned dictionary size components with C `size_t` wrapping +/// semantics. +#[no_mangle] +pub extern "C" fn ZSTD_rust_sizeofCDict(objectSize: usize, workspaceSize: usize) -> usize { + sizeof_cdict(objectSize, workspaceSize) +} + #[inline] fn sizeof_cctx( object_size: usize, @@ -1678,6 +1690,24 @@ mod tests { assert_eq!(sizeof_local_dict(0, usize::MAX, usize::MAX), usize::MAX); } + #[test] + fn sizeof_cdict_handles_zero_components() { + assert_eq!(sizeof_cdict(0, 0), 0); + assert_eq!(ZSTD_rust_sizeofCDict(0, 0), 0); + } + + #[test] + fn sizeof_cdict_adds_components_in_order() { + assert_eq!(sizeof_cdict(17, 25), 42); + assert_eq!(ZSTD_rust_sizeofCDict(17, 25), 42); + } + + #[test] + fn sizeof_cdict_wraps_like_c_size_t_addition() { + assert_eq!(sizeof_cdict(usize::MAX, 1), 0); + assert_eq!(ZSTD_rust_sizeofCDict(usize::MAX, 1), 0); + } + #[test] fn sizeof_cctx_handles_zero_components() { assert_eq!(sizeof_cctx(0, 0, 0, 0), 0);