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

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

Strengthen reset-tail tests to verify the direct reset state.

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:09:11 +02:00
parent fb78186ed9
commit 45191e1413
2 changed files with 30 additions and 52 deletions
+3 -11
View File
@@ -1563,14 +1563,14 @@ typedef char ZSTD_rust_reset_cctx_workspace_state_layout[
typedef struct { typedef struct {
void* callbackContext; void* callbackContext;
ZSTD_rust_resetCCtxStorageCallback_f initialize; ZSTD_rust_resetCCtxStorageCallback_f initialize;
ZSTD_rust_resetCCtxStorageCallback_f resetCompressedBlockState; void* compressedBlockState;
ZSTD_rust_resetCCtxTailCallback_f resetMatchState; ZSTD_rust_resetCCtxTailCallback_f resetMatchState;
ZSTD_rust_resetCCtxTailCallback_f resetStorage; ZSTD_rust_resetCCtxTailCallback_f resetStorage;
} ZSTD_rust_resetCCtxTailState; } ZSTD_rust_resetCCtxTailState;
typedef char ZSTD_rust_reset_cctx_tail_state_layout[ typedef char ZSTD_rust_reset_cctx_tail_state_layout[
(offsetof(ZSTD_rust_resetCCtxTailState, callbackContext) == 0 (offsetof(ZSTD_rust_resetCCtxTailState, callbackContext) == 0
&& offsetof(ZSTD_rust_resetCCtxTailState, initialize) == sizeof(void*) && offsetof(ZSTD_rust_resetCCtxTailState, initialize) == sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxTailState, resetCompressedBlockState) && offsetof(ZSTD_rust_resetCCtxTailState, compressedBlockState)
== 2 * sizeof(void*) == 2 * sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxTailState, resetMatchState) && offsetof(ZSTD_rust_resetCCtxTailState, resetMatchState)
== 3 * sizeof(void*) == 3 * sizeof(void*)
@@ -4248,13 +4248,6 @@ static void ZSTD_rust_resetCCtxTail_initialize(void* opaque)
cctx->dictContentSize = 0; cctx->dictContentSize = 0;
} }
static void ZSTD_rust_resetCCtxTail_resetCompressedBlockState(void* opaque)
{
ZSTD_rust_resetCCtxTailContext* const context =
(ZSTD_rust_resetCCtxTailContext*)opaque;
ZSTD_reset_compressedBlockState(context->cctx->blockState.prevCBlock);
}
static size_t ZSTD_rust_resetCCtxTail_resetMatchState(void* opaque) static size_t ZSTD_rust_resetCCtxTail_resetMatchState(void* opaque)
{ {
ZSTD_rust_resetCCtxTailContext* const context = ZSTD_rust_resetCCtxTailContext* const context =
@@ -4427,8 +4420,7 @@ static size_t ZSTD_resetCCtx_internal(ZSTD_CCtx* zc,
tailContext.storageState = &storageState; tailContext.storageState = &storageState;
tailState.callbackContext = &tailContext; tailState.callbackContext = &tailContext;
tailState.initialize = ZSTD_rust_resetCCtxTail_initialize; tailState.initialize = ZSTD_rust_resetCCtxTail_initialize;
tailState.resetCompressedBlockState = tailState.compressedBlockState = zc->blockState.prevCBlock;
ZSTD_rust_resetCCtxTail_resetCompressedBlockState;
tailState.resetMatchState = ZSTD_rust_resetCCtxTail_resetMatchState; tailState.resetMatchState = ZSTD_rust_resetCCtxTail_resetMatchState;
tailState.resetStorage = ZSTD_rust_resetCCtxTail_resetStorage; tailState.resetStorage = ZSTD_rust_resetCCtxTail_resetStorage;
FORWARD_IF_ERROR(ZSTD_rust_resetCCtxTail(&tailState), ""); FORWARD_IF_ERROR(ZSTD_rust_resetCCtxTail(&tailState), "");
+27 -41
View File
@@ -43,8 +43,8 @@ use crate::zstd_compress_stats::{
ZSTD_rust_deriveSeqStoreChunk, ZSTD_rust_determineBlockSize, ZSTD_rust_entropyCompressSeqStore, ZSTD_rust_deriveSeqStoreChunk, ZSTD_rust_determineBlockSize, ZSTD_rust_entropyCompressSeqStore,
ZSTD_rust_entropyCompressSeqStore_internal, ZSTD_rust_fastSequenceLengthSum, ZSTD_rust_entropyCompressSeqStore_internal, ZSTD_rust_fastSequenceLengthSum,
ZSTD_rust_finalizeOffBase, ZSTD_rust_get1BlockSummary, ZSTD_rust_isRLE, ZSTD_rust_maybeRLE, ZSTD_rust_finalizeOffBase, ZSTD_rust_get1BlockSummary, ZSTD_rust_isRLE, ZSTD_rust_maybeRLE,
ZSTD_rust_postProcessSequenceProducerResult, ZSTD_rust_resetSeqStore, ZSTD_rust_postProcessSequenceProducerResult, ZSTD_rust_resetCompressedBlockState,
ZSTD_rust_seqStore_resolveOffCodes, ZSTD_rust_storeLastLiterals, ZSTD_rust_resetSeqStore, ZSTD_rust_seqStore_resolveOffCodes, ZSTD_rust_storeLastLiterals,
ZSTD_rust_transferSequencesNoDelim, ZSTD_rust_transferSequencesWBlockDelim, ZSTD_rust_transferSequencesNoDelim, ZSTD_rust_transferSequencesWBlockDelim,
ZSTD_rust_validateSeqStore, ZSTD_LLT_LITERAL_LENGTH, ZSTD_LLT_MATCH_LENGTH, ZSTD_rust_validateSeqStore, ZSTD_LLT_LITERAL_LENGTH, ZSTD_LLT_MATCH_LENGTH,
}; };
@@ -6803,14 +6803,14 @@ const _: () = {
/// C-owned operations used by the post-workspace CCtx reset tail. /// C-owned operations used by the post-workspace CCtx reset tail.
/// ///
/// Rust owns the operation order and error propagation. C retains the /// Rust owns the operation order, compressed-block reset, and error
/// private context initialization, compressed-block reset, match-state reset, /// propagation. C retains the private context initialization, match-state
/// and storage publication operations. /// reset, and storage publication operations.
#[repr(C)] #[repr(C)]
pub struct ZSTD_rust_resetCCtxTailState { pub struct ZSTD_rust_resetCCtxTailState {
callbackContext: *mut c_void, callbackContext: *mut c_void,
initialize: Option<ResetCCtxStorageCallback>, initialize: Option<ResetCCtxStorageCallback>,
resetCompressedBlockState: Option<ResetCCtxStorageCallback>, compressedBlockState: *mut ZSTD_compressedBlockState_t,
resetMatchState: Option<ResetCCtxTailCallback>, resetMatchState: Option<ResetCCtxTailCallback>,
resetStorage: Option<ResetCCtxTailCallback>, resetStorage: Option<ResetCCtxTailCallback>,
} }
@@ -6819,8 +6819,7 @@ const _: () = {
assert!(offset_of!(ZSTD_rust_resetCCtxTailState, callbackContext) == 0); assert!(offset_of!(ZSTD_rust_resetCCtxTailState, callbackContext) == 0);
assert!(offset_of!(ZSTD_rust_resetCCtxTailState, initialize) == size_of::<usize>()); assert!(offset_of!(ZSTD_rust_resetCCtxTailState, initialize) == size_of::<usize>());
assert!( assert!(
offset_of!(ZSTD_rust_resetCCtxTailState, resetCompressedBlockState) offset_of!(ZSTD_rust_resetCCtxTailState, compressedBlockState) == 2 * size_of::<usize>()
== 2 * size_of::<usize>()
); );
assert!(offset_of!(ZSTD_rust_resetCCtxTailState, resetMatchState) == 3 * size_of::<usize>()); assert!(offset_of!(ZSTD_rust_resetCCtxTailState, resetMatchState) == 3 * size_of::<usize>());
assert!( assert!(
@@ -7126,19 +7125,19 @@ pub unsafe extern "C" fn ZSTD_rust_resetCCtxTail(
let Some(initialize) = state.initialize else { let Some(initialize) = state.initialize else {
return ERROR(ZstdErrorCode::Generic); return ERROR(ZstdErrorCode::Generic);
}; };
let Some(reset_compressed_block_state) = state.resetCompressedBlockState else {
return ERROR(ZstdErrorCode::Generic);
};
let Some(reset_match_state) = state.resetMatchState else { let Some(reset_match_state) = state.resetMatchState else {
return ERROR(ZstdErrorCode::Generic); return ERROR(ZstdErrorCode::Generic);
}; };
let Some(reset_storage) = state.resetStorage else { let Some(reset_storage) = state.resetStorage else {
return ERROR(ZstdErrorCode::Generic); return ERROR(ZstdErrorCode::Generic);
}; };
if state.compressedBlockState.is_null() {
return ERROR(ZstdErrorCode::Generic);
}
unsafe { unsafe {
initialize(state.callbackContext); initialize(state.callbackContext);
reset_compressed_block_state(state.callbackContext); ZSTD_rust_resetCompressedBlockState(state.compressedBlockState);
let result = reset_match_state(state.callbackContext); let result = reset_match_state(state.callbackContext);
if ERR_isError(result) { if ERR_isError(result) {
return result; return result;
@@ -16740,11 +16739,6 @@ mod tests {
context.events.push("initialize"); context.events.push("initialize");
} }
unsafe extern "C" fn reset_cctx_tail_test_reset_compressed_block_state(context: *mut c_void) {
let context = unsafe { reset_cctx_tail_test_context(context) };
context.events.push("reset-compressed-block-state");
}
unsafe extern "C" fn reset_cctx_tail_test_reset_match_state(context: *mut c_void) -> usize { unsafe extern "C" fn reset_cctx_tail_test_reset_match_state(context: *mut c_void) -> usize {
let context = unsafe { reset_cctx_tail_test_context(context) }; let context = unsafe { reset_cctx_tail_test_context(context) };
context.events.push("reset-match-state"); context.events.push("reset-match-state");
@@ -16759,11 +16753,12 @@ mod tests {
fn reset_cctx_tail_test_state( fn reset_cctx_tail_test_state(
context: &mut ResetCCtxTailTestContext, context: &mut ResetCCtxTailTestContext,
compressed_block_state: *mut ZSTD_compressedBlockState_t,
) -> ZSTD_rust_resetCCtxTailState { ) -> ZSTD_rust_resetCCtxTailState {
ZSTD_rust_resetCCtxTailState { ZSTD_rust_resetCCtxTailState {
callbackContext: (context as *mut ResetCCtxTailTestContext).cast(), callbackContext: (context as *mut ResetCCtxTailTestContext).cast(),
initialize: Some(reset_cctx_tail_test_initialize), initialize: Some(reset_cctx_tail_test_initialize),
resetCompressedBlockState: Some(reset_cctx_tail_test_reset_compressed_block_state), compressedBlockState: compressed_block_state,
resetMatchState: Some(reset_cctx_tail_test_reset_match_state), resetMatchState: Some(reset_cctx_tail_test_reset_match_state),
resetStorage: Some(reset_cctx_tail_test_reset_storage), resetStorage: Some(reset_cctx_tail_test_reset_storage),
} }
@@ -16772,18 +16767,17 @@ mod tests {
#[test] #[test]
fn cctx_reset_tail_preserves_post_workspace_callback_order() { fn cctx_reset_tail_preserves_post_workspace_callback_order() {
let mut context = ResetCCtxTailTestContext::default(); let mut context = ResetCCtxTailTestContext::default();
let state = reset_cctx_tail_test_state(&mut context); let mut block_state =
unsafe { MaybeUninit::<ZSTD_compressedBlockState_t>::zeroed().assume_init() };
let state = reset_cctx_tail_test_state(&mut context, &mut block_state);
assert_eq!(unsafe { ZSTD_rust_resetCCtxTail(&state) }, 0); assert_eq!(unsafe { ZSTD_rust_resetCCtxTail(&state) }, 0);
assert_eq!( assert_eq!(
context.events, context.events,
[ ["initialize", "reset-match-state", "reset-storage"]
"initialize",
"reset-compressed-block-state",
"reset-match-state",
"reset-storage",
]
); );
assert_eq!(block_state.rep, [1, 4, 8]);
assert_eq!(block_state.entropy.huf.repeatMode, 0);
} }
#[test] #[test]
@@ -16792,20 +16786,15 @@ mod tests {
match_result: ERROR(ZstdErrorCode::StageWrong), match_result: ERROR(ZstdErrorCode::StageWrong),
..ResetCCtxTailTestContext::default() ..ResetCCtxTailTestContext::default()
}; };
let state = reset_cctx_tail_test_state(&mut context); let mut block_state =
unsafe { MaybeUninit::<ZSTD_compressedBlockState_t>::zeroed().assume_init() };
let state = reset_cctx_tail_test_state(&mut context, &mut block_state);
assert_eq!( assert_eq!(
unsafe { ZSTD_rust_resetCCtxTail(&state) }, unsafe { ZSTD_rust_resetCCtxTail(&state) },
ERROR(ZstdErrorCode::StageWrong) ERROR(ZstdErrorCode::StageWrong)
); );
assert_eq!( assert_eq!(context.events, ["initialize", "reset-match-state"]);
context.events,
[
"initialize",
"reset-compressed-block-state",
"reset-match-state"
]
);
} }
#[test] #[test]
@@ -16814,7 +16803,9 @@ mod tests {
storage_result: ERROR(ZstdErrorCode::MemoryAllocation), storage_result: ERROR(ZstdErrorCode::MemoryAllocation),
..ResetCCtxTailTestContext::default() ..ResetCCtxTailTestContext::default()
}; };
let state = reset_cctx_tail_test_state(&mut context); let mut block_state =
unsafe { MaybeUninit::<ZSTD_compressedBlockState_t>::zeroed().assume_init() };
let state = reset_cctx_tail_test_state(&mut context, &mut block_state);
assert_eq!( assert_eq!(
unsafe { ZSTD_rust_resetCCtxTail(&state) }, unsafe { ZSTD_rust_resetCCtxTail(&state) },
@@ -16822,12 +16813,7 @@ mod tests {
); );
assert_eq!( assert_eq!(
context.events, context.events,
[ ["initialize", "reset-match-state", "reset-storage"]
"initialize",
"reset-compressed-block-state",
"reset-match-state",
"reset-storage",
]
); );
} }