feat(compress): call the Rust CDict block reset leaf directly

Pass the private CDict compressed-block-state pointer into the Rust CDict
initializer and invoke the existing Rust reset leaf directly. Remove the
redundant C adapter while keeping the block-state representation opaque at
the ABI boundary.

Strengthen the CDict initialization tests to verify reset repcodes and
repeat-mode state through the direct Rust path.

Test Plan:
- ulimit -v 41943040 && CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/Cargo.toml
- ulimit -v 41943040 && CARGO_BUILD_JOBS=1 cargo clippy --manifest-path rust/Cargo.toml --all-targets -- -D warnings
- ulimit -v 41943040 && make -j1
- ulimit -v 41943040 && make -j1 -C tests test-zstream ZSTREAM_TESTTIME=-T2s
- ulimit -v 41943040 && make -j1 -C tests test-fuzzer FUZZERTEST=-T3s FUZZER_FLAGS=--no-big-tests
- ulimit -v 41943040 && cargo fmt --manifest-path rust/Cargo.toml -- --check
- git diff --check
This commit is contained in:
2026-07-19 22:04:39 +02:00
parent 29198a1157
commit fb78186ed9
2 changed files with 17 additions and 37 deletions
+14 -27
View File
@@ -860,7 +860,6 @@ pub unsafe extern "C" fn ZSTD_rust_compressBeginUsingDict(
type InitCDictReserveContentFn = unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void;
type InitCDictReserveEntropyFn = unsafe extern "C" fn(*mut c_void) -> *mut c_void;
type InitCDictResetBlockStateFn = unsafe extern "C" fn(*mut c_void);
type InitCDictResetMatchStateFn = unsafe extern "C" fn(*mut c_void, *const c_void, c_int) -> usize;
type InitCDictInsertDictionaryFn =
unsafe extern "C" fn(*mut c_void, *const c_void, *const c_void, usize, c_int) -> usize;
@@ -889,7 +888,7 @@ pub struct ZSTD_rust_initCDictState {
content_size_flag: *mut c_int,
reserve_content: Option<InitCDictReserveContentFn>,
reserve_entropy: Option<InitCDictReserveEntropyFn>,
reset_block_state: Option<InitCDictResetBlockStateFn>,
block_state: *mut c_void,
reset_match_state: Option<InitCDictResetMatchStateFn>,
insert_dictionary: Option<InitCDictInsertDictionaryFn>,
}
@@ -897,7 +896,6 @@ pub struct ZSTD_rust_initCDictState {
const _: () = {
assert!(size_of::<InitCDictReserveContentFn>() == size_of::<usize>());
assert!(size_of::<InitCDictReserveEntropyFn>() == size_of::<usize>());
assert!(size_of::<InitCDictResetBlockStateFn>() == size_of::<usize>());
assert!(size_of::<InitCDictResetMatchStateFn>() == size_of::<usize>());
assert!(size_of::<InitCDictInsertDictionaryFn>() == size_of::<usize>());
assert!(offset_of!(ZSTD_rust_initCDictState, callback_context) == 0);
@@ -919,7 +917,7 @@ const _: () = {
assert!(offset_of!(ZSTD_rust_initCDictState, content_size_flag) == 13 * size_of::<usize>());
assert!(offset_of!(ZSTD_rust_initCDictState, reserve_content) == 14 * size_of::<usize>());
assert!(offset_of!(ZSTD_rust_initCDictState, reserve_entropy) == 15 * size_of::<usize>());
assert!(offset_of!(ZSTD_rust_initCDictState, reset_block_state) == 16 * size_of::<usize>());
assert!(offset_of!(ZSTD_rust_initCDictState, block_state) == 16 * size_of::<usize>());
assert!(offset_of!(ZSTD_rust_initCDictState, reset_match_state) == 17 * size_of::<usize>());
assert!(offset_of!(ZSTD_rust_initCDictState, insert_dictionary) == 18 * size_of::<usize>());
assert!(size_of::<ZSTD_rust_initCDictState>() == size_of::<[usize; 19]>());
@@ -946,9 +944,6 @@ pub unsafe extern "C" fn ZSTD_rust_initCDict(
let Some(reserve_entropy) = state.reserve_entropy else {
return ERROR(ZstdErrorCode::Generic);
};
let Some(reset_block_state) = state.reset_block_state else {
return ERROR(ZstdErrorCode::Generic);
};
let Some(reset_match_state) = state.reset_match_state else {
return ERROR(ZstdErrorCode::Generic);
};
@@ -969,6 +964,7 @@ pub unsafe extern "C" fn ZSTD_rust_initCDict(
|| state.dict_id.is_null()
|| state.compression_level.is_null()
|| state.content_size_flag.is_null()
|| state.block_state.is_null()
{
return ERROR(ZstdErrorCode::Generic);
}
@@ -1001,7 +997,7 @@ pub unsafe extern "C" fn ZSTD_rust_initCDict(
*state.entropy_workspace = entropy_workspace;
}
unsafe { reset_block_state(state.callback_context) };
unsafe { ZSTD_rust_resetCompressedBlockState(state.block_state.cast()) };
let reset_match_result = unsafe {
reset_match_state(
state.callback_context,
@@ -2075,12 +2071,6 @@ mod tests {
probe.entropy_workspace
}
unsafe extern "C" fn init_cdict_reset_block_state(context: *mut c_void) {
unsafe { init_cdict_probe(context) }
.events
.push("reset-block");
}
unsafe extern "C" fn init_cdict_reset_match_state(
context: *mut c_void,
c_params: *const c_void,
@@ -2130,6 +2120,8 @@ mod tests {
let enable_dedicated_dict_search = 1;
let use_row_match_finder = 2;
let mut dedicated_dict_search = 0;
let mut block_state =
unsafe { std::mem::MaybeUninit::<ZSTD_compressedBlockState_t>::zeroed().assume_init() };
let mut dict_content = dictionary.as_ptr().cast::<c_void>();
let mut dict_content_size = 0;
let mut dict_content_type = 0;
@@ -2156,7 +2148,7 @@ mod tests {
content_size_flag: &mut content_size_flag,
reserve_content: Some(init_cdict_reserve_content),
reserve_entropy: Some(init_cdict_reserve_entropy),
reset_block_state: Some(init_cdict_reset_block_state),
block_state: (&mut block_state as *mut ZSTD_compressedBlockState_t).cast(),
reset_match_state: Some(init_cdict_reset_match_state),
insert_dictionary: Some(init_cdict_insert_dictionary),
};
@@ -2172,10 +2164,7 @@ mod tests {
};
assert_eq!(result, 0);
assert_eq!(
probe.events,
["reserve", "reset-block", "reset-match", "insert"]
);
assert_eq!(probe.events, ["reserve", "reset-match", "insert"]);
assert_eq!(dict_content, dictionary.as_ptr().cast());
assert_eq!(probe.reset_c_params, c_params);
assert_eq!(probe.reset_use_row_match_finder, use_row_match_finder);
@@ -2191,6 +2180,8 @@ mod tests {
assert_eq!(dict_id, probe.insert_result as c_uint);
assert_eq!(compression_level, 3);
assert_eq!(content_size_flag, 1);
assert_eq!(block_state.rep, [1, 4, 8]);
assert_eq!(block_state.entropy.huf.repeatMode, 0);
}
#[test]
@@ -2214,6 +2205,8 @@ mod tests {
let enable_dedicated_dict_search = 1;
let use_row_match_finder = 2;
let mut dedicated_dict_search = 0;
let mut block_state =
unsafe { std::mem::MaybeUninit::<ZSTD_compressedBlockState_t>::zeroed().assume_init() };
let mut dict_content = ptr::null();
let mut dict_content_size = 0;
let mut dict_content_type = 0;
@@ -2240,7 +2233,7 @@ mod tests {
content_size_flag: &mut content_size_flag,
reserve_content: Some(init_cdict_reserve_content),
reserve_entropy: Some(init_cdict_reserve_entropy),
reset_block_state: Some(init_cdict_reset_block_state),
block_state: (&mut block_state as *mut ZSTD_compressedBlockState_t).cast(),
reset_match_state: Some(init_cdict_reset_match_state),
insert_dictionary: Some(init_cdict_insert_dictionary),
};
@@ -2258,13 +2251,7 @@ mod tests {
assert_eq!(result, 0);
assert_eq!(
probe.events,
[
"reserve-content",
"reserve",
"reset-block",
"reset-match",
"insert"
]
["reserve-content", "reserve", "reset-match", "insert"]
);
assert_eq!(probe.reserved_content_size, dictionary.len());
assert_eq!(&probe.content_storage[..dictionary.len()], &dictionary);