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
This commit is contained in:
2026-07-18 12:44:53 +02:00
parent fe0ace0370
commit ba7cc52bcc
2 changed files with 35 additions and 2 deletions
+5 -2
View File
@@ -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(
+30
View File
@@ -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);