feat(compress): move CCtx metadata copies into Rust

Pass CCtx destination fields and source fields through the copyCCtx bridge,
then copy dictID and dictContentSize in Rust at the original metadata-copy
point. Remove the final redundant C metadata callback while preserving copy
ordering, reset error handling, and block-state propagation.

Add focused tests for metadata values and callback sequencing.

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:37:36 +02:00
parent 7b94de78ff
commit 4117ec7b3e
2 changed files with 55 additions and 20 deletions
+9 -12
View File
@@ -480,7 +480,10 @@ typedef struct {
ZSTD_rust_copyCCtxCopyTables_f copyTables;
ZSTD_rust_copyCCtxMarkTables_f markTablesClean;
ZSTD_rust_copyCCtxCopyState_f copyMatchState;
ZSTD_rust_copyCCtxCopyState_f copyDictState;
U32* destinationDictID;
const U32* sourceDictID;
size_t* destinationDictContentSize;
const size_t* sourceDictContentSize;
ZSTD_compressedBlockState_t** destinationBlockState;
const ZSTD_compressedBlockState_t* sourceBlockState;
int zbuff;
@@ -523,7 +526,7 @@ typedef char ZSTD_rust_copy_cctx_internal_state_layout[
== 3 * sizeof(void*) + sizeof(U64)
&& offsetof(ZSTD_rust_copyCCtxInternalState, zbuff)
== 3 * sizeof(void*) + sizeof(U64)
+ 10 * sizeof(void*)
+ 13 * sizeof(void*)
&& sizeof(ZSTD_rust_copyCCtxInternalState)
== ((offsetof(ZSTD_rust_copyCCtxInternalState, zbuff)
+ sizeof(int) + sizeof(void*) - 1) / sizeof(void*))
@@ -4837,15 +4840,6 @@ static void ZSTD_rust_copyCCtx_copy_match_state(
dstMatchState->loadedDictEnd = srcMatchState->loadedDictEnd;
}
static void ZSTD_rust_copyCCtx_copy_dict_state(
void* context, const void* srcCCtx)
{
ZSTD_CCtx* const dst = (ZSTD_CCtx*)context;
const ZSTD_CCtx* const src = (const ZSTD_CCtx*)srcCCtx;
dst->dictID = src->dictID;
dst->dictContentSize = src->dictContentSize;
}
static size_t ZSTD_rust_copyCCtx_internal_callback(
void* context, const void* srcCCtx,
const ZSTD_frameParameters* fParams,
@@ -4863,7 +4857,10 @@ static size_t ZSTD_rust_copyCCtx_internal_callback(
state.copyTables = ZSTD_rust_copyCCtx_copy_tables;
state.markTablesClean = ZSTD_rust_copyCCtx_mark_tables_clean;
state.copyMatchState = ZSTD_rust_copyCCtx_copy_match_state;
state.copyDictState = ZSTD_rust_copyCCtx_copy_dict_state;
state.destinationDictID = &((ZSTD_CCtx*)context)->dictID;
state.sourceDictID = &((const ZSTD_CCtx*)srcCCtx)->dictID;
state.destinationDictContentSize = &((ZSTD_CCtx*)context)->dictContentSize;
state.sourceDictContentSize = &((const ZSTD_CCtx*)srcCCtx)->dictContentSize;
state.destinationBlockState = &((ZSTD_CCtx*)context)->blockState.prevCBlock;
state.sourceBlockState = ((const ZSTD_CCtx*)srcCCtx)->blockState.prevCBlock;
state.zbuff = zbuff;
+46 -8
View File
@@ -1702,7 +1702,10 @@ pub struct ZSTD_rust_copyCCtxInternalState {
copy_tables: Option<CopyCCtxCopyTablesFn>,
mark_tables_clean: Option<CopyCCtxMarkTablesFn>,
copy_match_state: Option<CopyCCtxCopyStateFn>,
copy_dict_state: Option<CopyCCtxCopyStateFn>,
destination_dict_id: *mut c_uint,
source_dict_id: *const c_uint,
destination_dict_content_size: *mut usize,
source_dict_content_size: *const usize,
destination_block_state: *mut *mut ZSTD_compressedBlockState_t,
source_block_state: *const ZSTD_compressedBlockState_t,
zbuff: c_int,
@@ -1726,7 +1729,7 @@ const _: () = {
);
assert!(
offset_of!(ZSTD_rust_copyCCtxInternalState, zbuff)
== 3 * size_of::<usize>() + size_of::<u64>() + 10 * size_of::<usize>()
== 3 * size_of::<usize>() + size_of::<u64>() + 13 * size_of::<usize>()
);
assert!(
size_of::<ZSTD_rust_copyCCtxInternalState>()
@@ -1754,7 +1757,6 @@ pub unsafe extern "C" fn ZSTD_rust_copyCCtxInternal(
Some(copy_tables),
Some(mark_tables_clean),
Some(copy_match_state),
Some(copy_dict_state),
) = (
state.check_stage,
state.copy_custom_mem,
@@ -1763,7 +1765,6 @@ pub unsafe extern "C" fn ZSTD_rust_copyCCtxInternal(
state.copy_tables,
state.mark_tables_clean,
state.copy_match_state,
state.copy_dict_state,
)
else {
return ERROR(ZstdErrorCode::Generic);
@@ -1771,6 +1772,10 @@ pub unsafe extern "C" fn ZSTD_rust_copyCCtxInternal(
if state.callback_context.is_null()
|| state.src_cctx.is_null()
|| state.f_params.is_null()
|| state.destination_dict_id.is_null()
|| state.source_dict_id.is_null()
|| state.destination_dict_content_size.is_null()
|| state.source_dict_content_size.is_null()
|| state.destination_block_state.is_null()
|| state.source_block_state.is_null()
{
@@ -1798,7 +1803,8 @@ pub unsafe extern "C" fn ZSTD_rust_copyCCtxInternal(
copy_tables(state.callback_context, state.src_cctx);
mark_tables_clean(state.callback_context);
copy_match_state(state.callback_context, state.src_cctx);
copy_dict_state(state.callback_context, state.src_cctx);
*state.destination_dict_id = *state.source_dict_id;
*state.destination_dict_content_size = *state.source_dict_content_size;
let destination_block_state = *state.destination_block_state;
if destination_block_state.is_null() {
return ERROR(ZstdErrorCode::Generic);
@@ -13871,7 +13877,6 @@ mod tests {
let event = match context.events.len() {
1 => "custom",
6 => "match",
7 => "dict",
_ => panic!("unexpected copy-state callback order"),
};
context.events.push(event);
@@ -13916,6 +13921,10 @@ mod tests {
context: &mut CopyCCtxInternalTestContext,
src_cctx: *const c_void,
f_params: &ZSTD_frameParameters,
destination_dict_id: *mut c_uint,
source_dict_id: *const c_uint,
destination_dict_content_size: *mut usize,
source_dict_content_size: *const usize,
destination_block_state: *mut *mut ZSTD_compressedBlockState_t,
source_block_state: *const ZSTD_compressedBlockState_t,
) -> ZSTD_rust_copyCCtxInternalState {
@@ -13931,7 +13940,10 @@ mod tests {
copy_tables: Some(copy_cctx_internal_test_copy_tables),
mark_tables_clean: Some(copy_cctx_internal_test_mark_tables_clean),
copy_match_state: Some(copy_cctx_internal_test_copy_state),
copy_dict_state: Some(copy_cctx_internal_test_copy_state),
destination_dict_id,
source_dict_id,
destination_dict_content_size,
source_dict_content_size,
destination_block_state,
source_block_state,
zbuff: 7,
@@ -13955,10 +13967,18 @@ mod tests {
unsafe { MaybeUninit::<ZSTD_compressedBlockState_t>::zeroed().assume_init() };
let mut destination_block_state_slot =
&mut destination_block_state as *mut ZSTD_compressedBlockState_t;
let source_dict_id = 0x1234_5678;
let mut destination_dict_id = 0;
let source_dict_content_size = 9876;
let mut destination_dict_content_size = 0;
let state = copy_cctx_internal_test_state(
&mut context,
0x5000usize as *const c_void,
&f_params,
&mut destination_dict_id,
&source_dict_id,
&mut destination_dict_content_size,
&source_dict_content_size,
&mut destination_block_state_slot,
&source_block_state,
);
@@ -13968,11 +13988,13 @@ mod tests {
assert_eq!(result, 0);
assert_eq!(
context.events,
["check", "custom", "reset", "dirty", "tables", "clean", "match", "dict"]
["check", "custom", "reset", "dirty", "tables", "clean", "match"]
);
assert_eq!(context.frame_params, f_params);
assert_eq!(context.pledged_src_size, 123);
assert_eq!(context.zbuff, 7);
assert_eq!(destination_dict_id, source_dict_id);
assert_eq!(destination_dict_content_size, source_dict_content_size);
assert_eq!(destination_block_state.rep, source_block_state.rep);
assert_eq!(
destination_block_state.entropy.huf.repeatMode,
@@ -13997,10 +14019,18 @@ mod tests {
&mut destination_block_state as *mut ZSTD_compressedBlockState_t;
let source_block_state =
unsafe { MaybeUninit::<ZSTD_compressedBlockState_t>::zeroed().assume_init() };
let source_dict_id = 0x1234_5678;
let mut destination_dict_id = 0;
let source_dict_content_size = 9876;
let mut destination_dict_content_size = 0;
let state = copy_cctx_internal_test_state(
&mut context,
0x5000usize as *const c_void,
&f_params,
&mut destination_dict_id,
&source_dict_id,
&mut destination_dict_content_size,
&source_dict_content_size,
&mut destination_block_state_slot,
&source_block_state,
);
@@ -14024,10 +14054,18 @@ mod tests {
&mut destination_block_state as *mut ZSTD_compressedBlockState_t;
let source_block_state =
unsafe { MaybeUninit::<ZSTD_compressedBlockState_t>::zeroed().assume_init() };
let source_dict_id = 0x1234_5678;
let mut destination_dict_id = 0;
let source_dict_content_size = 9876;
let mut destination_dict_content_size = 0;
let state = copy_cctx_internal_test_state(
&mut context,
0x5000usize as *const c_void,
&f_params,
&mut destination_dict_id,
&source_dict_id,
&mut destination_dict_content_size,
&source_dict_content_size,
&mut destination_block_state_slot,
&source_block_state,
);