feat(compress): move CDict attach reset orchestration into Rust

Move prepared-CDict attachment reset sequencing into a Rust projection with
explicit reset, window-attach, dictionary-metadata, and block-state ordering.
C retains parameter adjustment, private window linkage, and CCtx/CDict field
access behind callbacks, with reset allocation failures stopping before any
attachment mutation.

Test Plan:
- ulimit -v 41943040 && CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/Cargo.toml (717 passed)
- 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-fuzzer FUZZERTEST=-T3s FUZZER_FLAGS=--no-big-tests (287 cases)
- ulimit -v 41943040 && make -j1 -C tests test-zstream ZSTREAM_TESTTIME=-T2s (84 deterministic plus 137 randomized cases)
This commit is contained in:
2026-07-19 20:41:52 +02:00
parent 28becbe6ee
commit b8e70c1467
2 changed files with 332 additions and 57 deletions
+132 -57
View File
@@ -2401,6 +2401,43 @@ typedef char ZSTD_rust_reset_cctx_by_copying_cdict_state_layout[
+ sizeof(int) + sizeof(void*) - 1) / sizeof(void*))
* sizeof(void*))
? 1 : -1];
typedef size_t (*ZSTD_rust_resetCCtxByAttachingCDictReset_f)(
void* context, const void* cdict, const void* params,
U64 pledgedSrcSize, int zbuff);
typedef void (*ZSTD_rust_resetCCtxByAttachingCDictAttach_f)(
void* context, const void* cdict);
typedef void (*ZSTD_rust_resetCCtxByAttachingCDictState_f)(
void* context, const void* cdict);
typedef struct {
void* callbackContext;
const void* cdict;
const void* params;
U64 pledgedSrcSize;
ZSTD_rust_resetCCtxByAttachingCDictReset_f reset;
ZSTD_rust_resetCCtxByAttachingCDictAttach_f attach;
ZSTD_rust_resetCCtxByAttachingCDictState_f copyDictState;
ZSTD_rust_resetCCtxByAttachingCDictState_f copyBlockState;
int zbuff;
} ZSTD_rust_resetCCtxByAttachingCDictState;
size_t ZSTD_rust_resetCCtxByAttachingCDict(
const ZSTD_rust_resetCCtxByAttachingCDictState* state);
typedef char ZSTD_rust_reset_cctx_by_attaching_cdict_state_layout[
(offsetof(ZSTD_rust_resetCCtxByAttachingCDictState, callbackContext) == 0
&& offsetof(ZSTD_rust_resetCCtxByAttachingCDictState, cdict)
== sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxByAttachingCDictState, params)
== 2 * sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxByAttachingCDictState, pledgedSrcSize)
== 3 * sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxByAttachingCDictState, reset)
== 3 * sizeof(void*) + sizeof(U64)
&& offsetof(ZSTD_rust_resetCCtxByAttachingCDictState, zbuff)
== 3 * sizeof(void*) + sizeof(U64) + 4 * sizeof(void*)
&& sizeof(ZSTD_rust_resetCCtxByAttachingCDictState)
== ((offsetof(ZSTD_rust_resetCCtxByAttachingCDictState, zbuff)
+ sizeof(int) + sizeof(void*) - 1) / sizeof(void*))
* 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
@@ -4286,6 +4323,90 @@ void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx) {
assert(!ZSTD_window_hasExtDict(cctx->blockState.matchState.window));
}
static size_t ZSTD_rust_resetCCtx_byAttachingCDict_reset(
void* context, const void* cdictOpaque, const void* paramsOpaque,
U64 pledgedSrcSize, int zbuff)
{
ZSTD_CCtx* const cctx = (ZSTD_CCtx*)context;
const ZSTD_CDict* const cdict = (const ZSTD_CDict*)cdictOpaque;
ZSTD_CCtx_params params = *(const ZSTD_CCtx_params*)paramsOpaque;
ZSTD_compressionParameters adjusted_cdict_cParams =
cdict->matchState.cParams;
unsigned const windowLog = params.cParams.windowLog;
DEBUGLOG(4, "ZSTD_resetCCtx_byAttachingCDict() pledgedSrcSize=%llu",
(unsigned long long)pledgedSrcSize);
assert(windowLog != 0);
/* Resize working context table params for input only, since the dict
* has its own tables. */
if (cdict->matchState.dedicatedDictSearch) {
ZSTD_dedicatedDictSearch_revertCParams(&adjusted_cdict_cParams);
}
params.cParams = ZSTD_adjustCParams_internal(
adjusted_cdict_cParams, pledgedSrcSize,
cdict->dictContentSize, ZSTD_cpm_attachDict,
params.useRowMatchFinder);
params.cParams.windowLog = windowLog;
params.useRowMatchFinder = cdict->useRowMatchFinder;
{ size_t const resetError = ZSTD_resetCCtx_internal(
cctx, &params, pledgedSrcSize,
/* loadedDictSize */ 0,
ZSTDcrp_makeClean,
(ZSTD_buffered_policy_e)zbuff);
if (ZSTD_isError(resetError)) return resetError;
}
assert(cctx->appliedParams.cParams.strategy == adjusted_cdict_cParams.strategy);
return 0;
}
static void ZSTD_rust_resetCCtx_byAttachingCDict_attach(
void* context, const void* cdictOpaque)
{
ZSTD_CCtx* const cctx = (ZSTD_CCtx*)context;
const ZSTD_CDict* const cdict = (const ZSTD_CDict*)cdictOpaque;
U32 const cdictEnd = (U32)(cdict->matchState.window.nextSrc
- cdict->matchState.window.base);
U32 const cdictLen = cdictEnd - cdict->matchState.window.dictLimit;
if (cdictLen == 0) {
DEBUGLOG(4, "skipping attaching empty dictionary");
return;
}
DEBUGLOG(4, "attaching dictionary into context");
cctx->blockState.matchState.dictMatchState = &cdict->matchState;
if (cctx->blockState.matchState.window.dictLimit < cdictEnd) {
cctx->blockState.matchState.window.nextSrc =
cctx->blockState.matchState.window.base + cdictEnd;
ZSTD_rust_windowClear(
(size_t)(cctx->blockState.matchState.window.nextSrc
- cctx->blockState.matchState.window.base),
&cctx->blockState.matchState.window.lowLimit,
&cctx->blockState.matchState.window.dictLimit);
}
/* loadedDictEnd is expressed within the active context referential. */
cctx->blockState.matchState.loadedDictEnd =
cctx->blockState.matchState.window.dictLimit;
}
static void ZSTD_rust_resetCCtx_byAttachingCDict_copy_dict_state(
void* context, const void* cdictOpaque)
{
ZSTD_CCtx* const cctx = (ZSTD_CCtx*)context;
const ZSTD_CDict* const cdict = (const ZSTD_CDict*)cdictOpaque;
cctx->dictID = cdict->dictID;
cctx->dictContentSize = cdict->dictContentSize;
}
static void ZSTD_rust_resetCCtx_byAttachingCDict_copy_block_state(
void* context, const void* cdictOpaque)
{
ZSTD_CCtx* const cctx = (ZSTD_CCtx*)context;
const ZSTD_CDict* const cdict = (const ZSTD_CDict*)cdictOpaque;
ZSTD_memcpy(cctx->blockState.prevCBlock, &cdict->cBlockState,
sizeof(cdict->cBlockState));
}
static size_t
ZSTD_resetCCtx_byAttachingCDict(ZSTD_CCtx* cctx,
const ZSTD_CDict* cdict,
@@ -4293,63 +4414,17 @@ ZSTD_resetCCtx_byAttachingCDict(ZSTD_CCtx* cctx,
U64 pledgedSrcSize,
ZSTD_buffered_policy_e zbuff)
{
DEBUGLOG(4, "ZSTD_resetCCtx_byAttachingCDict() pledgedSrcSize=%llu",
(unsigned long long)pledgedSrcSize);
{
ZSTD_compressionParameters adjusted_cdict_cParams = cdict->matchState.cParams;
unsigned const windowLog = params.cParams.windowLog;
assert(windowLog != 0);
/* Resize working context table params for input only, since the dict
* has its own tables. */
/* pledgedSrcSize == 0 means 0! */
if (cdict->matchState.dedicatedDictSearch) {
ZSTD_dedicatedDictSearch_revertCParams(&adjusted_cdict_cParams);
}
params.cParams = ZSTD_adjustCParams_internal(adjusted_cdict_cParams, pledgedSrcSize,
cdict->dictContentSize, ZSTD_cpm_attachDict,
params.useRowMatchFinder);
params.cParams.windowLog = windowLog;
params.useRowMatchFinder = cdict->useRowMatchFinder; /* cdict overrides */
FORWARD_IF_ERROR(ZSTD_resetCCtx_internal(cctx, &params, pledgedSrcSize,
/* loadedDictSize */ 0,
ZSTDcrp_makeClean, zbuff), "");
assert(cctx->appliedParams.cParams.strategy == adjusted_cdict_cParams.strategy);
}
{ const U32 cdictEnd = (U32)( cdict->matchState.window.nextSrc
- cdict->matchState.window.base);
const U32 cdictLen = cdictEnd - cdict->matchState.window.dictLimit;
if (cdictLen == 0) {
/* don't even attach dictionaries with no contents */
DEBUGLOG(4, "skipping attaching empty dictionary");
} else {
DEBUGLOG(4, "attaching dictionary into context");
cctx->blockState.matchState.dictMatchState = &cdict->matchState;
/* prep working match state so dict matches never have negative indices
* when they are translated to the working context's index space. */
if (cctx->blockState.matchState.window.dictLimit < cdictEnd) {
cctx->blockState.matchState.window.nextSrc =
cctx->blockState.matchState.window.base + cdictEnd;
ZSTD_rust_windowClear(
(size_t)(cctx->blockState.matchState.window.nextSrc -
cctx->blockState.matchState.window.base),
&cctx->blockState.matchState.window.lowLimit,
&cctx->blockState.matchState.window.dictLimit);
}
/* loadedDictEnd is expressed within the referential of the active context */
cctx->blockState.matchState.loadedDictEnd = cctx->blockState.matchState.window.dictLimit;
} }
cctx->dictID = cdict->dictID;
cctx->dictContentSize = cdict->dictContentSize;
/* copy block state */
ZSTD_memcpy(cctx->blockState.prevCBlock, &cdict->cBlockState, sizeof(cdict->cBlockState));
return 0;
ZSTD_rust_resetCCtxByAttachingCDictState state;
state.callbackContext = cctx;
state.cdict = cdict;
state.params = &params;
state.pledgedSrcSize = pledgedSrcSize;
state.reset = ZSTD_rust_resetCCtx_byAttachingCDict_reset;
state.attach = ZSTD_rust_resetCCtx_byAttachingCDict_attach;
state.copyDictState = ZSTD_rust_resetCCtx_byAttachingCDict_copy_dict_state;
state.copyBlockState = ZSTD_rust_resetCCtx_byAttachingCDict_copy_block_state;
state.zbuff = (int)zbuff;
return ZSTD_rust_resetCCtxByAttachingCDict(&state);
}
static void ZSTD_copyCDictTableIntoCCtx(U32* dst, U32 const* src, size_t tableSize,