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
+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);