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
+3 -10
View File
@@ -2225,7 +2225,6 @@ typedef char ZSTD_rust_external_sequence_store_state_layout[
typedef void* (*ZSTD_rust_initCDictReserveContent_f)(
void* context, size_t dictSize);
typedef void* (*ZSTD_rust_initCDictReserveEntropy_f)(void* context);
typedef void (*ZSTD_rust_initCDictResetBlockState_f)(void* context);
typedef size_t (*ZSTD_rust_initCDictResetMatchState_f)(
void* context, const ZSTD_compressionParameters* cParams,
int useRowMatchFinder);
@@ -2249,7 +2248,7 @@ typedef struct {
int* contentSizeFlag;
ZSTD_rust_initCDictReserveContent_f reserveContent;
ZSTD_rust_initCDictReserveEntropy_f reserveEntropy;
ZSTD_rust_initCDictResetBlockState_f resetBlockState;
void* blockState;
ZSTD_rust_initCDictResetMatchState_f resetMatchState;
ZSTD_rust_initCDictInsertDictionary_f insertDictionary;
} ZSTD_rust_initCDictState;
@@ -2287,7 +2286,7 @@ typedef char ZSTD_rust_init_cdict_state_layout[
== 14 * sizeof(void*)
&& offsetof(ZSTD_rust_initCDictState, reserveEntropy)
== 15 * sizeof(void*)
&& offsetof(ZSTD_rust_initCDictState, resetBlockState)
&& offsetof(ZSTD_rust_initCDictState, blockState)
== 16 * sizeof(void*)
&& offsetof(ZSTD_rust_initCDictState, resetMatchState)
== 17 * sizeof(void*)
@@ -6042,12 +6041,6 @@ static void* ZSTD_rust_initCDict_reserveEntropy(void* context)
return ZSTD_cwksp_reserve_object(&cdict->workspace, HUF_WORKSPACE_SIZE);
}
static void ZSTD_rust_initCDict_resetBlockState(void* context)
{
ZSTD_CDict* const cdict = (ZSTD_CDict*)context;
ZSTD_reset_compressedBlockState(&cdict->cBlockState);
}
static size_t ZSTD_rust_initCDict_resetMatchState(
void* context, const ZSTD_compressionParameters* cParams,
int useRowMatchFinder)
@@ -6617,7 +6610,7 @@ static size_t ZSTD_initCDict_internal(
state.contentSizeFlag = &params.fParams.contentSizeFlag;
state.reserveContent = ZSTD_rust_initCDict_reserveContent;
state.reserveEntropy = ZSTD_rust_initCDict_reserveEntropy;
state.resetBlockState = ZSTD_rust_initCDict_resetBlockState;
state.blockState = &cdict->cBlockState;
state.resetMatchState = ZSTD_rust_initCDict_resetMatchState;
state.insertDictionary = ZSTD_rust_initCDict_insertDictionary;
return ZSTD_rust_initCDict(
+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);