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

Move the prepared-CDict-to-CCtx reset sequence into a Rust projection with
explicit reset, table, hashTable3, dictionary, and block-state ordering.  C
retains private layouts, table-tag removal, row-matchfinder metadata, and all
pointer arithmetic behind callbacks, while reset allocation errors now stop
the sequence before table mutation.

Test Plan:
- ulimit -v 41943040 && CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/Cargo.toml (715 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 (305 cases)
- ulimit -v 41943040 && make -j1 -C tests test-zstream ZSTREAM_TESTTIME=-T2s (84 deterministic plus 248 randomized cases)
This commit is contained in:
2026-07-19 20:37:25 +02:00
parent 1046c18b0d
commit 28becbe6ee
2 changed files with 415 additions and 46 deletions
+155 -46
View File
@@ -2361,6 +2361,46 @@ typedef char ZSTD_rust_reset_cctx_using_cdict_state_layout[
== 10 * sizeof(void*)
&& sizeof(ZSTD_rust_resetCCtxUsingCDictState) == 11 * sizeof(void*))
? 1 : -1];
typedef size_t (*ZSTD_rust_resetCCtxByCopyingCDictReset_f)(
void* context, const void* cdict, const void* params,
U64 pledgedSrcSize, int zbuff);
typedef void (*ZSTD_rust_resetCCtxByCopyingCDictState_f)(
void* context, const void* cdict);
typedef void (*ZSTD_rust_resetCCtxByCopyingCDictMarkTables_f)(void* context);
typedef struct {
void* callbackContext;
const void* cdict;
const void* params;
U64 pledgedSrcSize;
ZSTD_rust_resetCCtxByCopyingCDictReset_f reset;
ZSTD_rust_resetCCtxByCopyingCDictMarkTables_f markTablesDirty;
ZSTD_rust_resetCCtxByCopyingCDictState_f copyTables;
ZSTD_rust_resetCCtxByCopyingCDictState_f zeroHashTable3;
ZSTD_rust_resetCCtxByCopyingCDictMarkTables_f markTablesClean;
ZSTD_rust_resetCCtxByCopyingCDictState_f copyMatchState;
ZSTD_rust_resetCCtxByCopyingCDictState_f copyDictState;
ZSTD_rust_resetCCtxByCopyingCDictState_f copyBlockState;
int zbuff;
} ZSTD_rust_resetCCtxByCopyingCDictState;
size_t ZSTD_rust_resetCCtxByCopyingCDict(
const ZSTD_rust_resetCCtxByCopyingCDictState* state);
typedef char ZSTD_rust_reset_cctx_by_copying_cdict_state_layout[
(offsetof(ZSTD_rust_resetCCtxByCopyingCDictState, callbackContext) == 0
&& offsetof(ZSTD_rust_resetCCtxByCopyingCDictState, cdict)
== sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxByCopyingCDictState, params)
== 2 * sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxByCopyingCDictState, pledgedSrcSize)
== 3 * sizeof(void*)
&& offsetof(ZSTD_rust_resetCCtxByCopyingCDictState, reset)
== 3 * sizeof(void*) + sizeof(U64)
&& offsetof(ZSTD_rust_resetCCtxByCopyingCDictState, zbuff)
== 3 * sizeof(void*) + sizeof(U64) + 8 * sizeof(void*)
&& sizeof(ZSTD_rust_resetCCtxByCopyingCDictState)
== ((offsetof(ZSTD_rust_resetCCtxByCopyingCDictState, 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
@@ -4319,17 +4359,19 @@ static void ZSTD_copyCDictTableIntoCCtx(U32* dst, U32 const* src, size_t tableSi
ZSTD_CDictIndicesAreTagged(cParams));
}
static size_t ZSTD_resetCCtx_byCopyingCDict(ZSTD_CCtx* cctx,
const ZSTD_CDict* cdict,
ZSTD_CCtx_params params,
U64 pledgedSrcSize,
ZSTD_buffered_policy_e zbuff)
static size_t ZSTD_rust_resetCCtx_byCopyingCDict_reset(
void* context, const void* cdictOpaque, const void* paramsOpaque,
U64 pledgedSrcSize, int zbuff)
{
const ZSTD_compressionParameters *cdict_cParams = &cdict->matchState.cParams;
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;
const ZSTD_compressionParameters* const cdict_cParams =
&cdict->matchState.cParams;
assert(!cdict->matchState.dedicatedDictSearch);
DEBUGLOG(4, "ZSTD_resetCCtx_byCopyingCDict() pledgedSrcSize=%llu",
(unsigned long long)pledgedSrcSize);
(unsigned long long)pledgedSrcSize);
{ unsigned const windowLog = params.cParams.windowLog;
assert(windowLog != 0);
@@ -4337,68 +4379,135 @@ static size_t ZSTD_resetCCtx_byCopyingCDict(ZSTD_CCtx* cctx,
params.cParams = *cdict_cParams;
params.cParams.windowLog = windowLog;
params.useRowMatchFinder = cdict->useRowMatchFinder;
FORWARD_IF_ERROR(ZSTD_resetCCtx_internal(cctx, &params, pledgedSrcSize,
/* loadedDictSize */ 0,
ZSTDcrp_leaveDirty, zbuff), "");
{ size_t const resetError = ZSTD_resetCCtx_internal(
cctx, &params, pledgedSrcSize,
/* loadedDictSize */ 0,
ZSTDcrp_leaveDirty,
(ZSTD_buffered_policy_e)zbuff);
if (ZSTD_isError(resetError)) return resetError;
}
assert(cctx->appliedParams.cParams.strategy == cdict_cParams->strategy);
assert(cctx->appliedParams.cParams.hashLog == cdict_cParams->hashLog);
assert(cctx->appliedParams.cParams.chainLog == cdict_cParams->chainLog);
}
return 0;
}
static void ZSTD_rust_resetCCtx_byCopyingCDict_mark_tables_dirty(void* context)
{
ZSTD_CCtx* const cctx = (ZSTD_CCtx*)context;
ZSTD_cwksp_mark_tables_dirty(&cctx->workspace);
assert(params.useRowMatchFinder != ZSTD_ps_auto);
}
/* copy tables */
{ size_t const chainSize = ZSTD_allocateChainTable(cdict_cParams->strategy, cdict->useRowMatchFinder, 0 /* DDS guaranteed disabled */)
? ((size_t)1 << cdict_cParams->chainLog)
: 0;
size_t const hSize = (size_t)1 << cdict_cParams->hashLog;
static void ZSTD_rust_resetCCtx_byCopyingCDict_copy_tables(
void* context, const void* cdictOpaque)
{
ZSTD_CCtx* const cctx = (ZSTD_CCtx*)context;
const ZSTD_CDict* const cdict = (const ZSTD_CDict*)cdictOpaque;
const ZSTD_compressionParameters* const cdict_cParams =
&cdict->matchState.cParams;
size_t const chainSize = ZSTD_allocateChainTable(
cdict_cParams->strategy, cdict->useRowMatchFinder,
0 /* DDS guaranteed disabled */)
? ((size_t)1 << cdict_cParams->chainLog)
: 0;
size_t const hSize = (size_t)1 << cdict_cParams->hashLog;
ZSTD_copyCDictTableIntoCCtx(cctx->blockState.matchState.hashTable,
ZSTD_copyCDictTableIntoCCtx(cctx->blockState.matchState.hashTable,
cdict->matchState.hashTable,
hSize, cdict_cParams);
/* Do not copy cdict's chainTable if cctx has parameters such that it would not use chainTable */
if (ZSTD_allocateChainTable(cctx->appliedParams.cParams.strategy, cctx->appliedParams.useRowMatchFinder, 0 /* forDDSDict */)) {
ZSTD_copyCDictTableIntoCCtx(cctx->blockState.matchState.chainTable,
/* Do not copy cdict's chainTable if cctx will not use a chainTable. */
if (ZSTD_allocateChainTable(cctx->appliedParams.cParams.strategy,
cctx->appliedParams.useRowMatchFinder,
0 /* forDDSDict */)) {
ZSTD_copyCDictTableIntoCCtx(cctx->blockState.matchState.chainTable,
cdict->matchState.chainTable,
chainSize, cdict_cParams);
}
/* copy tag table */
if (ZSTD_rowMatchFinderUsed(cdict_cParams->strategy, cdict->useRowMatchFinder)) {
size_t const tagTableSize = hSize;
ZSTD_memcpy(cctx->blockState.matchState.tagTable,
cdict->matchState.tagTable,
tagTableSize);
cctx->blockState.matchState.hashSalt = cdict->matchState.hashSalt;
}
}
/* Zero the hashTable3, since the cdict never fills it */
assert(cctx->blockState.matchState.hashLog3 <= 31);
{ U32 const h3log = cctx->blockState.matchState.hashLog3;
size_t const h3Size = h3log ? ((size_t)1 << h3log) : 0;
assert(cdict->matchState.hashLog3 == 0);
ZSTD_memset(cctx->blockState.matchState.hashTable3, 0, h3Size * sizeof(U32));
if (ZSTD_rowMatchFinderUsed(cdict_cParams->strategy,
cdict->useRowMatchFinder)) {
ZSTD_memcpy(cctx->blockState.matchState.tagTable,
cdict->matchState.tagTable, hSize);
cctx->blockState.matchState.hashSalt = cdict->matchState.hashSalt;
}
}
static void ZSTD_rust_resetCCtx_byCopyingCDict_zero_hash_table3(
void* context, const void* cdictOpaque)
{
ZSTD_CCtx* const cctx = (ZSTD_CCtx*)context;
const ZSTD_CDict* const cdict = (const ZSTD_CDict*)cdictOpaque;
U32 const h3log = cctx->blockState.matchState.hashLog3;
assert(h3log <= 31);
assert(cdict->matchState.hashLog3 == 0);
ZSTD_memset(cctx->blockState.matchState.hashTable3, 0,
(h3log ? ((size_t)1 << h3log) : 0) * sizeof(U32));
}
static void ZSTD_rust_resetCCtx_byCopyingCDict_mark_tables_clean(void* context)
{
ZSTD_CCtx* const cctx = (ZSTD_CCtx*)context;
ZSTD_cwksp_mark_tables_clean(&cctx->workspace);
}
/* copy dictionary offsets */
{ ZSTD_MatchState_t const* srcMatchState = &cdict->matchState;
ZSTD_MatchState_t* dstMatchState = &cctx->blockState.matchState;
dstMatchState->window = srcMatchState->window;
dstMatchState->nextToUpdate = srcMatchState->nextToUpdate;
dstMatchState->loadedDictEnd= srcMatchState->loadedDictEnd;
}
static void ZSTD_rust_resetCCtx_byCopyingCDict_copy_match_state(
void* context, const void* cdictOpaque)
{
ZSTD_CCtx* const cctx = (ZSTD_CCtx*)context;
const ZSTD_CDict* const cdict = (const ZSTD_CDict*)cdictOpaque;
const ZSTD_MatchState_t* const src = &cdict->matchState;
ZSTD_MatchState_t* const dst = &cctx->blockState.matchState;
dst->window = src->window;
dst->nextToUpdate = src->nextToUpdate;
dst->loadedDictEnd = src->loadedDictEnd;
}
static void ZSTD_rust_resetCCtx_byCopyingCDict_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;
}
/* copy block state */
ZSTD_memcpy(cctx->blockState.prevCBlock, &cdict->cBlockState, sizeof(cdict->cBlockState));
static void ZSTD_rust_resetCCtx_byCopyingCDict_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));
}
return 0;
static size_t ZSTD_resetCCtx_byCopyingCDict(ZSTD_CCtx* cctx,
const ZSTD_CDict* cdict,
ZSTD_CCtx_params params,
U64 pledgedSrcSize,
ZSTD_buffered_policy_e zbuff)
{
ZSTD_rust_resetCCtxByCopyingCDictState state;
state.callbackContext = cctx;
state.cdict = cdict;
state.params = &params;
state.pledgedSrcSize = pledgedSrcSize;
state.reset = ZSTD_rust_resetCCtx_byCopyingCDict_reset;
state.markTablesDirty =
ZSTD_rust_resetCCtx_byCopyingCDict_mark_tables_dirty;
state.copyTables = ZSTD_rust_resetCCtx_byCopyingCDict_copy_tables;
state.zeroHashTable3 =
ZSTD_rust_resetCCtx_byCopyingCDict_zero_hash_table3;
state.markTablesClean =
ZSTD_rust_resetCCtx_byCopyingCDict_mark_tables_clean;
state.copyMatchState =
ZSTD_rust_resetCCtx_byCopyingCDict_copy_match_state;
state.copyDictState =
ZSTD_rust_resetCCtx_byCopyingCDict_copy_dict_state;
state.copyBlockState =
ZSTD_rust_resetCCtx_byCopyingCDict_copy_block_state;
state.zbuff = (int)zbuff;
return ZSTD_rust_resetCCtxByCopyingCDict(&state);
}
static size_t ZSTD_rust_resetCCtxUsingCDict_attach(