feat(compress): move CDict reset selection into Rust

Move the shared CDict attach-versus-copy policy and callback dispatch into a
Rust-owned ABI leaf. Keep both workspace- and match-state-heavy reset
implementations in C behind opaque callbacks.

Test Plan:
- cargo test --manifest-path rust/Cargo.toml --lib
- cargo clippy --manifest-path rust/Cargo.toml --all-targets -- -D warnings
- make -B -C programs -j1 zstd
- make -C tests -j1 test-zstream ZSTREAM_TESTTIME=-T1s
- focused reset_using_cdict unit tests
This commit is contained in:
2026-07-19 15:23:55 +02:00
parent 5f1e0e3d91
commit 9fdc27d596
2 changed files with 376 additions and 23 deletions
+84 -22
View File
@@ -1011,9 +1011,6 @@ ZSTD_compressionParameters
ZSTD_rust_params_dedicatedDictSearch_getCParams(ZSTD_compressionParameters cParams);
ZSTD_compressionParameters
ZSTD_rust_params_dedicatedDictSearch_revertCParams(ZSTD_compressionParameters cParams);
int ZSTD_rust_params_shouldAttachDict(int strategy, int dedicatedDictSearch,
U64 pledgedSrcSize, int attachDictPref,
int forceWindow);
int ZSTD_rust_params_getCParamMode(int cdict_present, int cdict_strategy,
int cdict_dedicated_search,
U64 pledgedSrcSize,
@@ -1555,6 +1552,52 @@ typedef char ZSTD_rust_compress_begin_state_layout[
&& sizeof(ZSTD_rust_compressBeginState) == 18 * sizeof(void*))
? 1 : -1];
typedef size_t (*ZSTD_rust_resetCCtxUsingCDictAttach_f)(
void* context, const void* cdict, const void* params,
U64 pledgedSrcSize, int zbuff);
typedef size_t (*ZSTD_rust_resetCCtxUsingCDictCopy_f)(
void* context, const void* cdict, const void* params,
U64 pledgedSrcSize, int zbuff);
typedef struct {
void* callbackContext;
const void* cdict;
const void* params;
const int* cdictStrategy;
const int* dedicatedDictSearch;
const int* attachDictPref;
const int* forceWindow;
const U64* pledgedSrcSize;
const int* zbuff;
ZSTD_rust_resetCCtxUsingCDictAttach_f attach;
ZSTD_rust_resetCCtxUsingCDictCopy_f copy;
} ZSTD_rust_resetCCtxUsingCDictState;
size_t ZSTD_rust_resetCCtxUsingCDict(
const ZSTD_rust_resetCCtxUsingCDictState* state);
typedef char ZSTD_rust_reset_cctx_using_cdict_state_layout[
(offsetof(ZSTD_rust_resetCCtxUsingCDictState, callbackContext) == 0
&& offsetof(ZSTD_rust_resetCCtxUsingCDictState, cdict)
== sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxUsingCDictState, params)
== 2 * sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxUsingCDictState, cdictStrategy)
== 3 * sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxUsingCDictState, dedicatedDictSearch)
== 4 * sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxUsingCDictState, attachDictPref)
== 5 * sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxUsingCDictState, forceWindow)
== 6 * sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxUsingCDictState, pledgedSrcSize)
== 7 * sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxUsingCDictState, zbuff)
== 8 * sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxUsingCDictState, attach)
== 9 * sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxUsingCDictState, copy)
== 10 * sizeof(void*)
&& sizeof(ZSTD_rust_resetCCtxUsingCDictState) == 11 * sizeof(void*))
? 1 : -1];
/* The sequence-compression loop receives only the state it actually reads or
* updates. In particular, neither ZSTD_CCtx nor a C function pointer crosses
* the Rust ABI. */
@@ -3164,18 +3207,6 @@ void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx) {
assert(!ZSTD_window_hasExtDict(cctx->blockState.matchState.window));
}
static int ZSTD_shouldAttachDict(const ZSTD_CDict* cdict,
const ZSTD_CCtx_params* params,
U64 pledgedSrcSize)
{
return ZSTD_rust_params_shouldAttachDict(
cdict->matchState.cParams.strategy,
cdict->matchState.dedicatedDictSearch,
pledgedSrcSize,
(int)params->attachDictPref,
params->forceWindow);
}
static size_t
ZSTD_resetCCtx_byAttachingCDict(ZSTD_CCtx* cctx,
const ZSTD_CDict* cdict,
@@ -3331,6 +3362,26 @@ static size_t ZSTD_resetCCtx_byCopyingCDict(ZSTD_CCtx* cctx,
return 0;
}
static size_t ZSTD_rust_resetCCtxUsingCDict_attach(
void* context, const void* cdict, const void* params,
U64 pledgedSrcSize, int zbuff)
{
return ZSTD_resetCCtx_byAttachingCDict(
(ZSTD_CCtx*)context, (const ZSTD_CDict*)cdict,
*(const ZSTD_CCtx_params*)params, pledgedSrcSize,
(ZSTD_buffered_policy_e)zbuff);
}
static size_t ZSTD_rust_resetCCtxUsingCDict_copy(
void* context, const void* cdict, const void* params,
U64 pledgedSrcSize, int zbuff)
{
return ZSTD_resetCCtx_byCopyingCDict(
(ZSTD_CCtx*)context, (const ZSTD_CDict*)cdict,
*(const ZSTD_CCtx_params*)params, pledgedSrcSize,
(ZSTD_buffered_policy_e)zbuff);
}
/* We have a choice between copying the dictionary context into the working
* context, or referencing the dictionary context from the working context
* in-place. We decide here which strategy to use. */
@@ -3340,17 +3391,28 @@ static size_t ZSTD_resetCCtx_usingCDict(ZSTD_CCtx* cctx,
U64 pledgedSrcSize,
ZSTD_buffered_policy_e zbuff)
{
ZSTD_rust_resetCCtxUsingCDictState state;
int const cdictStrategy = (int)cdict->matchState.cParams.strategy;
int const dedicatedDictSearch = (int)cdict->matchState.dedicatedDictSearch;
int const attachDictPref = (int)params->attachDictPref;
int const forceWindow = (int)params->forceWindow;
int const zbuffValue = (int)zbuff;
DEBUGLOG(4, "ZSTD_resetCCtx_usingCDict (pledgedSrcSize=%u)",
(unsigned)pledgedSrcSize);
if (ZSTD_shouldAttachDict(cdict, params, pledgedSrcSize)) {
return ZSTD_resetCCtx_byAttachingCDict(
cctx, cdict, *params, pledgedSrcSize, zbuff);
} else {
return ZSTD_resetCCtx_byCopyingCDict(
cctx, cdict, *params, pledgedSrcSize, zbuff);
}
state.callbackContext = cctx;
state.cdict = cdict;
state.params = params;
state.cdictStrategy = &cdictStrategy;
state.dedicatedDictSearch = &dedicatedDictSearch;
state.attachDictPref = &attachDictPref;
state.forceWindow = &forceWindow;
state.pledgedSrcSize = &pledgedSrcSize;
state.zbuff = &zbuffValue;
state.attach = ZSTD_rust_resetCCtxUsingCDict_attach;
state.copy = ZSTD_rust_resetCCtxUsingCDict_copy;
return ZSTD_rust_resetCCtxUsingCDict(&state);
}
/*! ZSTD_copyCCtx_internal() :