feat(compress): move CCtx reset planning into Rust
Move the scalar planning portion of ZSTD_resetCCtx_internal across an explicit C/Rust boundary. Rust now computes window and block sizes, sequence capacities, buffered I/O sizes, LDM and external-sequence reservations, the index-reset policy, and the CCtx workspace estimate. C retains private workspace resizing, reservations, pointer publication, and layout-dependent callbacks. Add ABI layout assertions and focused planner tests for buffered sizing, LDM and index-reset policy, external sequence capacity, and invalid unadjusted LDM inputs. Keep the migration boundary documented so the hybrid state remains explicit while deeper CCtx reset and matchfinder operations stay in C. Test Plan: - `cargo fmt --manifest-path rust/Cargo.toml -- --check` - capped focused planner tests: 3 passed - capped full Rust library tests: 680 passed, 0 failed - capped `cargo clippy --manifest-path rust/Cargo.toml --all-targets -- -D warnings`: passed - capped native `make -B -C programs -j1 zstd`: passed; only known fileio const-cast warnings - capped `make -C tests -j1 test-zstream ZSTREAM_TESTTIME=-T1s`: 84 tests and both short fuzzer rounds passed; only known zstream initializer warning
This commit is contained in:
+130
-28
@@ -1195,6 +1195,85 @@ typedef struct {
|
||||
size_t optimalTSize;
|
||||
size_t asanRedzoneSize;
|
||||
} ZSTD_rustCCtxWorkspaceSizing;
|
||||
typedef struct {
|
||||
size_t windowSize;
|
||||
size_t blockSize;
|
||||
size_t maxNbSeq;
|
||||
size_t buffInSize;
|
||||
size_t buffOutSize;
|
||||
size_t maxNbLdmSeq;
|
||||
size_t maxNbExternalSeq;
|
||||
size_t neededSpace;
|
||||
int needsIndexReset;
|
||||
} ZSTD_rustCCtxResetPlan;
|
||||
typedef char ZSTD_rust_cctx_workspace_sizing_layout[
|
||||
(offsetof(ZSTD_rustCCtxWorkspaceSizing, cctxSize) == 0
|
||||
&& offsetof(ZSTD_rustCCtxWorkspaceSizing, compressedBlockStateSize)
|
||||
== sizeof(size_t)
|
||||
&& offsetof(ZSTD_rustCCtxWorkspaceSizing, seqDefSize)
|
||||
== 2 * sizeof(size_t)
|
||||
&& offsetof(ZSTD_rustCCtxWorkspaceSizing, rawSeqSize)
|
||||
== 3 * sizeof(size_t)
|
||||
&& offsetof(ZSTD_rustCCtxWorkspaceSizing, externalSequenceSize)
|
||||
== 4 * sizeof(size_t)
|
||||
&& offsetof(ZSTD_rustCCtxWorkspaceSizing, tmpWorkspaceSize)
|
||||
== 5 * sizeof(size_t)
|
||||
&& offsetof(ZSTD_rustCCtxWorkspaceSizing, wildcopyOverlength)
|
||||
== 6 * sizeof(size_t)
|
||||
&& offsetof(ZSTD_rustCCtxWorkspaceSizing, matchTSize)
|
||||
== 7 * sizeof(size_t)
|
||||
&& offsetof(ZSTD_rustCCtxWorkspaceSizing, optimalTSize)
|
||||
== 8 * sizeof(size_t)
|
||||
&& offsetof(ZSTD_rustCCtxWorkspaceSizing, asanRedzoneSize)
|
||||
== 9 * sizeof(size_t)
|
||||
&& sizeof(ZSTD_rustCCtxWorkspaceSizing) == 10 * sizeof(size_t))
|
||||
? 1 : -1];
|
||||
typedef struct {
|
||||
ZSTD_compressionParameters cParams;
|
||||
int ldmEnable;
|
||||
U32 ldmHashLog;
|
||||
U32 ldmBucketSizeLog;
|
||||
U32 ldmMinMatchLength;
|
||||
int isStatic;
|
||||
int useRowMatchFinder;
|
||||
int inBufferBuffered;
|
||||
int outBufferBuffered;
|
||||
int useSequenceProducer;
|
||||
int initialized;
|
||||
int indexTooClose;
|
||||
int dictTooBig;
|
||||
U64 pledgedSrcSize;
|
||||
size_t maxBlockSize;
|
||||
const ZSTD_rustCCtxWorkspaceSizing* sizing;
|
||||
ZSTD_rustCCtxResetPlan* plan;
|
||||
} ZSTD_rustCCtxResetState;
|
||||
typedef char ZSTD_rust_cctx_reset_state_layout[
|
||||
(offsetof(ZSTD_rustCCtxResetState, cParams) == 0
|
||||
&& offsetof(ZSTD_rustCCtxResetState, ldmEnable)
|
||||
== sizeof(ZSTD_compressionParameters)
|
||||
&& offsetof(ZSTD_rustCCtxResetState, pledgedSrcSize)
|
||||
> offsetof(ZSTD_rustCCtxResetState, dictTooBig)
|
||||
&& offsetof(ZSTD_rustCCtxResetState, maxBlockSize)
|
||||
== offsetof(ZSTD_rustCCtxResetState, pledgedSrcSize) + sizeof(U64)
|
||||
&& offsetof(ZSTD_rustCCtxResetState, sizing)
|
||||
== offsetof(ZSTD_rustCCtxResetState, maxBlockSize) + sizeof(size_t)
|
||||
&& offsetof(ZSTD_rustCCtxResetState, plan)
|
||||
== offsetof(ZSTD_rustCCtxResetState, sizing) + sizeof(void*)
|
||||
&& sizeof(ZSTD_rustCCtxResetState)
|
||||
== offsetof(ZSTD_rustCCtxResetState, plan) + sizeof(void*))
|
||||
? 1 : -1];
|
||||
typedef char ZSTD_rust_cctx_reset_plan_layout[
|
||||
(offsetof(ZSTD_rustCCtxResetPlan, windowSize) == 0
|
||||
&& offsetof(ZSTD_rustCCtxResetPlan, blockSize) == sizeof(size_t)
|
||||
&& offsetof(ZSTD_rustCCtxResetPlan, maxNbSeq) == 2 * sizeof(size_t)
|
||||
&& offsetof(ZSTD_rustCCtxResetPlan, buffInSize) == 3 * sizeof(size_t)
|
||||
&& offsetof(ZSTD_rustCCtxResetPlan, buffOutSize) == 4 * sizeof(size_t)
|
||||
&& offsetof(ZSTD_rustCCtxResetPlan, maxNbLdmSeq) == 5 * sizeof(size_t)
|
||||
&& offsetof(ZSTD_rustCCtxResetPlan, maxNbExternalSeq) == 6 * sizeof(size_t)
|
||||
&& offsetof(ZSTD_rustCCtxResetPlan, neededSpace) == 7 * sizeof(size_t)
|
||||
&& offsetof(ZSTD_rustCCtxResetPlan, needsIndexReset) == 8 * sizeof(size_t)
|
||||
&& sizeof(ZSTD_rustCCtxResetPlan) == 9 * sizeof(size_t))
|
||||
? 1 : -1];
|
||||
size_t ZSTD_rust_estimateCCtxWorkspaceSize(
|
||||
ZSTD_compressionParameters cParams,
|
||||
int ldmEnable, U32 ldmHashLog, U32 ldmBucketSizeLog,
|
||||
@@ -1204,6 +1283,7 @@ size_t ZSTD_rust_estimateCCtxWorkspaceSize(
|
||||
U64 pledgedSrcSize, int useSequenceProducer,
|
||||
size_t maxBlockSize,
|
||||
const ZSTD_rustCCtxWorkspaceSizing* sizing);
|
||||
size_t ZSTD_rust_planCCtxReset(const ZSTD_rustCCtxResetState* state);
|
||||
size_t ZSTD_rust_maxEstimateCCtxSize(size_t estimate0, size_t estimate1,
|
||||
size_t estimate2, size_t estimate3);
|
||||
ZSTD_inBuffer ZSTD_rust_inBufferForEndFlush(int inBufferMode,
|
||||
@@ -3115,12 +3195,6 @@ ZSTD_sizeof_matchState(const ZSTD_compressionParameters* const cParams,
|
||||
forCCtx, &sizing);
|
||||
}
|
||||
|
||||
/* Helper function for calculating memory requirements.
|
||||
* Gives a tighter bound than ZSTD_sequenceBound() by taking minMatch into account. */
|
||||
static size_t ZSTD_maxNbSeq(size_t blockSize, unsigned minMatch, int useSequenceProducer) {
|
||||
return ZSTD_rust_params_maxNbSeq(blockSize, minMatch, useSequenceProducer);
|
||||
}
|
||||
|
||||
static size_t ZSTD_estimateCCtxSize_usingCCtxParams_internal(
|
||||
const ZSTD_compressionParameters* cParams,
|
||||
const ldmParams_t* ldmParams,
|
||||
@@ -3613,28 +3687,56 @@ static size_t ZSTD_resetCCtx_internal(ZSTD_CCtx* zc,
|
||||
assert(params->ldmParams.hashRateLog < 32);
|
||||
}
|
||||
|
||||
{ size_t const windowSize = MAX(1, (size_t)MIN(((U64)1 << params->cParams.windowLog), pledgedSrcSize));
|
||||
size_t const blockSize = MIN(params->maxBlockSize, windowSize);
|
||||
size_t const maxNbSeq = ZSTD_maxNbSeq(blockSize, params->cParams.minMatch, ZSTD_hasExtSeqProd(params));
|
||||
size_t const buffOutSize = (zbuff == ZSTDb_buffered && params->outBufferMode == ZSTD_bm_buffered)
|
||||
? ZSTD_compressBound(blockSize) + 1
|
||||
: 0;
|
||||
size_t const buffInSize = (zbuff == ZSTDb_buffered && params->inBufferMode == ZSTD_bm_buffered)
|
||||
? windowSize + blockSize
|
||||
: 0;
|
||||
size_t const maxNbLdmSeq = ZSTD_ldm_getMaxNbSeq(params->ldmParams, blockSize);
|
||||
{ ZSTD_rustCCtxWorkspaceSizing const sizing = {
|
||||
sizeof(ZSTD_CCtx),
|
||||
sizeof(ZSTD_compressedBlockState_t),
|
||||
sizeof(SeqDef),
|
||||
sizeof(rawSeq),
|
||||
sizeof(ZSTD_Sequence),
|
||||
TMP_WORKSPACE_SIZE,
|
||||
WILDCOPY_OVERLENGTH,
|
||||
sizeof(ZSTD_match_t),
|
||||
sizeof(ZSTD_optimal_t),
|
||||
ZSTD_RUST_ASAN_REDZONE_SIZE
|
||||
};
|
||||
ZSTD_rustCCtxResetPlan resetPlan;
|
||||
ZSTD_rustCCtxResetState resetState;
|
||||
size_t blockSize;
|
||||
size_t maxNbSeq;
|
||||
size_t buffOutSize;
|
||||
size_t buffInSize;
|
||||
size_t maxNbLdmSeq;
|
||||
size_t neededSpace;
|
||||
ZSTD_indexResetPolicy_e needsIndexReset;
|
||||
resetState.cParams = params->cParams;
|
||||
resetState.ldmEnable = (int)params->ldmParams.enableLdm;
|
||||
resetState.ldmHashLog = params->ldmParams.hashLog;
|
||||
resetState.ldmBucketSizeLog = params->ldmParams.bucketSizeLog;
|
||||
resetState.ldmMinMatchLength = params->ldmParams.minMatchLength;
|
||||
resetState.isStatic = zc->staticSize != 0;
|
||||
resetState.useRowMatchFinder = (int)params->useRowMatchFinder;
|
||||
resetState.inBufferBuffered =
|
||||
zbuff == ZSTDb_buffered && params->inBufferMode == ZSTD_bm_buffered;
|
||||
resetState.outBufferBuffered =
|
||||
zbuff == ZSTDb_buffered && params->outBufferMode == ZSTD_bm_buffered;
|
||||
resetState.useSequenceProducer = ZSTD_hasExtSeqProd(params);
|
||||
resetState.initialized = zc->initialized != 0;
|
||||
resetState.indexTooClose = ZSTD_indexTooCloseToMax(zc->blockState.matchState.window);
|
||||
resetState.dictTooBig = ZSTD_dictTooBig(loadedDictSize);
|
||||
resetState.pledgedSrcSize = pledgedSrcSize;
|
||||
resetState.maxBlockSize = params->maxBlockSize;
|
||||
resetState.sizing = &sizing;
|
||||
resetState.plan = &resetPlan;
|
||||
|
||||
int const indexTooClose = ZSTD_indexTooCloseToMax(zc->blockState.matchState.window);
|
||||
int const dictTooBig = ZSTD_dictTooBig(loadedDictSize);
|
||||
ZSTD_indexResetPolicy_e needsIndexReset =
|
||||
(indexTooClose || dictTooBig || !zc->initialized) ? ZSTDirp_reset : ZSTDirp_continue;
|
||||
FORWARD_IF_ERROR(ZSTD_rust_planCCtxReset(&resetState), "cctx reset plan failed!");
|
||||
|
||||
size_t const neededSpace =
|
||||
ZSTD_estimateCCtxSize_usingCCtxParams_internal(
|
||||
¶ms->cParams, ¶ms->ldmParams, zc->staticSize != 0, params->useRowMatchFinder,
|
||||
buffInSize, buffOutSize, pledgedSrcSize, ZSTD_hasExtSeqProd(params), params->maxBlockSize);
|
||||
|
||||
FORWARD_IF_ERROR(neededSpace, "cctx size estimate failed!");
|
||||
blockSize = resetPlan.blockSize;
|
||||
maxNbSeq = resetPlan.maxNbSeq;
|
||||
buffOutSize = resetPlan.buffOutSize;
|
||||
buffInSize = resetPlan.buffInSize;
|
||||
maxNbLdmSeq = resetPlan.maxNbLdmSeq;
|
||||
neededSpace = resetPlan.neededSpace;
|
||||
needsIndexReset = (ZSTD_indexResetPolicy_e)resetPlan.needsIndexReset;
|
||||
|
||||
if (!zc->staticSize) ZSTD_cwksp_bump_oversized_duration(ws, 0);
|
||||
|
||||
@@ -3643,7 +3745,7 @@ static size_t ZSTD_resetCCtx_internal(ZSTD_CCtx* zc,
|
||||
int const workspaceWasteful = ZSTD_cwksp_check_wasteful(ws, neededSpace);
|
||||
int resizeWorkspace = workspaceTooSmall || workspaceWasteful;
|
||||
DEBUGLOG(4, "Need %zu B workspace", neededSpace);
|
||||
DEBUGLOG(4, "windowSize: %zu - blockSize: %zu", windowSize, blockSize);
|
||||
DEBUGLOG(4, "windowSize: %zu - blockSize: %zu", resetPlan.windowSize, blockSize);
|
||||
|
||||
if (resizeWorkspace) {
|
||||
DEBUGLOG(4, "Resize workspaceSize from %zuKB to %zuKB",
|
||||
@@ -3718,7 +3820,7 @@ static size_t ZSTD_resetCCtx_internal(ZSTD_CCtx* zc,
|
||||
|
||||
/* reserve space for block-level external sequences */
|
||||
if (ZSTD_hasExtSeqProd(params)) {
|
||||
size_t const maxNbExternalSeq = ZSTD_sequenceBound(blockSize);
|
||||
size_t const maxNbExternalSeq = resetPlan.maxNbExternalSeq;
|
||||
zc->extSeqBufCapacity = maxNbExternalSeq;
|
||||
zc->extSeqBuf =
|
||||
(ZSTD_Sequence*)ZSTD_cwksp_reserve_aligned64(ws, maxNbExternalSeq * sizeof(ZSTD_Sequence));
|
||||
|
||||
Reference in New Issue
Block a user