From fb78186ed96c0819e1933140299a14f3b33e739c Mon Sep 17 00:00:00 2001 From: ddidderr Date: Sun, 19 Jul 2026 22:04:39 +0200 Subject: [PATCH] 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 --- lib/compress/zstd_compress.c | 13 ++------- rust/src/zstd_compress_dictionary.rs | 41 ++++++++++------------------ 2 files changed, 17 insertions(+), 37 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index a083c7a54..17a5b7908 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -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 = ¶ms.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( diff --git a/rust/src/zstd_compress_dictionary.rs b/rust/src/zstd_compress_dictionary.rs index 20f7a997e..a9c73f824 100644 --- a/rust/src/zstd_compress_dictionary.rs +++ b/rust/src/zstd_compress_dictionary.rs @@ -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, reserve_entropy: Option, - reset_block_state: Option, + block_state: *mut c_void, reset_match_state: Option, insert_dictionary: Option, } @@ -897,7 +896,6 @@ pub struct ZSTD_rust_initCDictState { const _: () = { assert!(size_of::() == size_of::()); assert!(size_of::() == size_of::()); - assert!(size_of::() == size_of::()); assert!(size_of::() == size_of::()); assert!(size_of::() == size_of::()); 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::()); assert!(offset_of!(ZSTD_rust_initCDictState, reserve_content) == 14 * size_of::()); assert!(offset_of!(ZSTD_rust_initCDictState, reserve_entropy) == 15 * size_of::()); - assert!(offset_of!(ZSTD_rust_initCDictState, reset_block_state) == 16 * size_of::()); + assert!(offset_of!(ZSTD_rust_initCDictState, block_state) == 16 * size_of::()); assert!(offset_of!(ZSTD_rust_initCDictState, reset_match_state) == 17 * size_of::()); assert!(offset_of!(ZSTD_rust_initCDictState, insert_dictionary) == 18 * size_of::()); assert!(size_of::() == 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::::zeroed().assume_init() }; let mut dict_content = dictionary.as_ptr().cast::(); 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::::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);