refactor(compress): expose parameter policy leaves from Rust

Remove the C forwarding wrappers around compression-parameter policy leaves
that are now implemented directly under their caller-facing ABI symbols.
Keep the private C parameter construction and resource logic in C, while Rust
continues to own the pure row-matchfinder, block-splitter, LDM, external
sequence/repcode, dictionary-tagging, block-size, and overflow predicates.

Test Plan: Pending capped full verification after the companion CLI leaf commit.
This commit is contained in:
2026-07-20 11:00:57 +02:00
parent 5ccc6f89fb
commit 03ac61063d
3 changed files with 99 additions and 188 deletions
+41 -97
View File
@@ -798,8 +798,8 @@ typedef char ZSTD_rust_overflow_correct_state_layout[
void ZSTD_rust_copyCDictTableIntoCCtx(U32* dst, U32 const* src,
size_t tableSize, int tagged);
U64 ZSTD_rust_advanceHashSalt(U64 hashSalt, U64 hashSaltEntropy);
int ZSTD_rust_indexTooCloseToMax(size_t nextSrcBaseOffset);
int ZSTD_rust_dictTooBig(size_t loadedDictSize);
int ZSTD_indexTooCloseToMax(size_t nextSrcBaseOffset);
int ZSTD_dictTooBig(size_t loadedDictSize);
/* Match-state reset orchestration lives in Rust. The callbacks keep the
* workspace implementation, window representation, and private pointer
@@ -1964,20 +1964,20 @@ ZSTD_compressionParameters ZSTD_rust_params_getCParamsFromCCtxParams(
ZSTD_parameters ZSTD_rust_params_makeParams(ZSTD_compressionParameters cParams);
size_t ZSTD_rust_params_maxNbSeq(size_t blockSize, U32 minMatch,
int useSequenceProducer);
size_t ZSTD_rust_params_resolveMaxBlockSize(size_t maxBlockSize);
size_t ZSTD_resolveMaxBlockSize(size_t maxBlockSize);
size_t ZSTD_rust_params_getBlockSize(size_t maxBlockSize, U32 windowLog);
int ZSTD_rust_params_resolveExternalSequenceValidation(int mode);
int ZSTD_rust_params_rowMatchFinderSupported(int strategy);
int ZSTD_resolveExternalSequenceValidation(int mode);
int ZSTD_rowMatchFinderSupported(int strategy);
int ZSTD_rust_params_rowMatchFinderUsed(int strategy, int mode);
int ZSTD_rust_params_resolveRowMatchFinderMode(
int ZSTD_resolveRowMatchFinderMode(
int mode, ZSTD_compressionParameters cParams);
int ZSTD_rust_params_resolveBlockSplitterMode(
int ZSTD_resolveBlockSplitterMode(
int mode, ZSTD_compressionParameters cParams);
int ZSTD_rust_params_allocateChainTable(int strategy, int mode, int forDDSDict);
int ZSTD_rust_params_resolveEnableLdm(
int ZSTD_resolveEnableLdm(
int mode, ZSTD_compressionParameters cParams);
int ZSTD_rust_params_resolveExternalRepcodeSearch(int mode, int cLevel);
int ZSTD_rust_params_cdictIndicesAreTagged(ZSTD_compressionParameters cParams);
int ZSTD_resolveExternalRepcodeSearch(int mode, int cLevel);
int ZSTD_CDictIndicesAreTagged(ZSTD_compressionParameters cParams);
ZSTD_compressionParameters
ZSTD_rust_params_dedicatedDictSearch_revertCParams(ZSTD_compressionParameters cParams);
int ZSTD_rust_params_getCParamMode(int cdict_present, int cdict_strategy,
@@ -3565,54 +3565,8 @@ size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs)
/* private API call, for dictBuilder only */
const SeqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx) { return &(ctx->seqStore); }
/* Returns true if the strategy supports using a row based matchfinder */
static int ZSTD_rowMatchFinderSupported(const ZSTD_strategy strategy) {
return ZSTD_rust_params_rowMatchFinderSupported((int)strategy);
}
/* Returns row matchfinder usage given an initial mode and cParams */
static ZSTD_ParamSwitch_e ZSTD_resolveRowMatchFinderMode(ZSTD_ParamSwitch_e mode,
const ZSTD_compressionParameters* const cParams) {
return (ZSTD_ParamSwitch_e)ZSTD_rust_params_resolveRowMatchFinderMode(
(int)mode, *cParams);
}
/* Returns block splitter usage (generally speaking, when using slower/stronger compression modes) */
static ZSTD_ParamSwitch_e ZSTD_resolveBlockSplitterMode(ZSTD_ParamSwitch_e mode,
const ZSTD_compressionParameters* const cParams) {
return (ZSTD_ParamSwitch_e)ZSTD_rust_params_resolveBlockSplitterMode(
(int)mode, *cParams);
}
/* Returns ZSTD_ps_enable if compression parameters are such that we should
* enable long distance matching (wlog >= 27, strategy >= btopt).
* Returns ZSTD_ps_disable otherwise.
*/
static ZSTD_ParamSwitch_e ZSTD_resolveEnableLdm(ZSTD_ParamSwitch_e mode,
const ZSTD_compressionParameters* const cParams) {
return (ZSTD_ParamSwitch_e)ZSTD_rust_params_resolveEnableLdm(
(int)mode, *cParams);
}
static int ZSTD_resolveExternalSequenceValidation(int mode) {
return ZSTD_rust_params_resolveExternalSequenceValidation(mode);
}
/* Resolves maxBlockSize to the default if no value is present. */
static size_t ZSTD_resolveMaxBlockSize(size_t maxBlockSize) {
return ZSTD_rust_params_resolveMaxBlockSize(maxBlockSize);
}
static ZSTD_ParamSwitch_e ZSTD_resolveExternalRepcodeSearch(ZSTD_ParamSwitch_e value, int cLevel) {
return (ZSTD_ParamSwitch_e)ZSTD_rust_params_resolveExternalRepcodeSearch(
(int)value, cLevel);
}
/* Returns 1 if compression parameters are such that CDict hashtable and chaintable indices are tagged.
* If so, the tags need to be removed in ZSTD_resetCCtx_byCopyingCDict. */
static int ZSTD_CDictIndicesAreTagged(const ZSTD_compressionParameters* const cParams) {
return ZSTD_rust_params_cdictIndicesAreTagged(*cParams);
}
/* These pure policy leaves are implemented in Rust under their caller symbols.
* C retains the surrounding private-state and configuration-sensitive logic. */
static ZSTD_CCtx_params ZSTD_makeCCtxParamsFromCParams(
ZSTD_compressionParameters cParams)
@@ -4093,8 +4047,8 @@ size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params)
{
ZSTD_compressionParameters const cParams =
ZSTD_getCParamsFromCCtxParams(params, ZSTD_CONTENTSIZE_UNKNOWN, 0, ZSTD_cpm_noAttachDict);
ZSTD_ParamSwitch_e const useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(params->useRowMatchFinder,
&cParams);
ZSTD_ParamSwitch_e const useRowMatchFinder = (ZSTD_ParamSwitch_e)
ZSTD_resolveRowMatchFinderMode((int)params->useRowMatchFinder, cParams);
RETURN_ERROR_IF(params->nbWorkers > 0, GENERIC, "Estimate CCtx size is supported for single-threaded compression only.");
/* estimateCCtxSize is for one-shot compression. So no buffers should
@@ -4107,7 +4061,7 @@ size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params)
size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams)
{
ZSTD_CCtx_params initialParams = ZSTD_makeCCtxParamsFromCParams(cParams);
if (ZSTD_rowMatchFinderSupported(cParams.strategy)) {
if (ZSTD_rowMatchFinderSupported((int)cParams.strategy)) {
/* Pick bigger of not using and using row-based matchfinder for greedy and lazy strategies */
size_t noRowCCtxSize;
size_t rowCCtxSize;
@@ -4171,7 +4125,9 @@ size_t ZSTD_estimateCStreamSize_usingCCtxParams(const ZSTD_CCtx_params* params)
size_t const outBuffSize = (params->outBufferMode == ZSTD_bm_buffered)
? ZSTD_compressBound(blockSize) + 1
: 0;
ZSTD_ParamSwitch_e const useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(params->useRowMatchFinder, &params->cParams);
ZSTD_ParamSwitch_e const useRowMatchFinder = (ZSTD_ParamSwitch_e)
ZSTD_resolveRowMatchFinderMode(
(int)params->useRowMatchFinder, params->cParams);
return ZSTD_estimateCCtxSize_usingCCtxParams_internal(
&cParams, &params->ldmParams, 1, useRowMatchFinder, inBuffSize, outBuffSize,
@@ -4182,7 +4138,7 @@ size_t ZSTD_estimateCStreamSize_usingCCtxParams(const ZSTD_CCtx_params* params)
size_t ZSTD_estimateCStreamSize_usingCParams(ZSTD_compressionParameters cParams)
{
ZSTD_CCtx_params initialParams = ZSTD_makeCCtxParamsFromCParams(cParams);
if (ZSTD_rowMatchFinderSupported(cParams.strategy)) {
if (ZSTD_rowMatchFinderSupported((int)cParams.strategy)) {
/* Pick bigger of not using and using row-based matchfinder for greedy and lazy strategies */
size_t noRowCCtxSize;
size_t rowCCtxSize;
@@ -4501,28 +4457,8 @@ ZSTD_reset_matchState(ZSTD_MatchState_t* ms,
return ZSTD_rust_resetMatchState(&state);
}
/* ZSTD_indexTooCloseToMax() :
* minor optimization : prefer memset() rather than reduceIndex()
* which is measurably slow in some circumstances (reported for Visual Studio).
* Works when re-using a context for a lot of smallish inputs :
* if all inputs are smaller than ZSTD_INDEXOVERFLOW_MARGIN,
* memset() will be triggered before reduceIndex().
*/
#define ZSTD_INDEXOVERFLOW_MARGIN (16 MB)
static int ZSTD_indexTooCloseToMax(ZSTD_window_t w)
{
return ZSTD_rust_indexTooCloseToMax((size_t)(w.nextSrc - w.base));
}
/** ZSTD_dictTooBig():
* When dictionaries are larger than ZSTD_CHUNKSIZE_MAX they can't be loaded in
* one go generically. So we ensure that in that case we reset the tables to zero,
* so that we can load as much of the dictionary as possible.
*/
static int ZSTD_dictTooBig(size_t const loadedDictSize)
{
return ZSTD_rust_dictTooBig(loadedDictSize);
}
/* ZSTD_indexTooCloseToMax() and ZSTD_dictTooBig() are Rust-owned scalar policy
* leaves. C retains the window and dictionary state used to supply inputs. */
typedef struct {
ZSTD_CCtx* cctx;
@@ -4877,7 +4813,9 @@ static size_t ZSTD_resetCCtx_internal(ZSTD_CCtx* zc,
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.indexTooClose = ZSTD_indexTooCloseToMax((size_t)(
zc->blockState.matchState.window.nextSrc
- zc->blockState.matchState.window.base));
resetState.dictTooBig = ZSTD_dictTooBig(loadedDictSize);
resetState.pledgedSrcSize = pledgedSrcSize;
resetState.maxBlockSize = params->maxBlockSize;
@@ -5108,7 +5046,7 @@ static size_t ZSTD_resetCCtx_byCopyingCDict(ZSTD_CCtx* cctx,
int const sourceStrategy = (int)cdict_cParams->strategy;
int const sourceUseRowMatchFinder = (int)cdict->useRowMatchFinder;
int const sourceIndicesTagged =
ZSTD_CDictIndicesAreTagged(cdict_cParams);
ZSTD_CDictIndicesAreTagged(*cdict_cParams);
ZSTD_rust_resetCCtxByCopyingCDictState state;
state.callbackContext = cctx;
state.cdict = cdict;
@@ -6404,7 +6342,7 @@ static size_t ZSTD_loadDictionaryContent_callback(
state.hashReadSize = HASH_READ_SIZE;
state.hashLog = cctxParams->cParams.hashLog;
state.chainLog = cctxParams->cParams.chainLog;
state.cdictIndicesTagged = ZSTD_CDictIndicesAreTagged(&cctxParams->cParams);
state.cdictIndicesTagged = ZSTD_CDictIndicesAreTagged(cctxParams->cParams);
state.ldmEnabled = cctxParams->ldmParams.enableLdm == ZSTD_ps_enable;
state.hasLdmState = ls != NULL;
state.forceWindow = cctxParams->forceWindow;
@@ -7374,7 +7312,8 @@ const ZSTD_CDict* ZSTD_initStaticCDict(
ZSTD_dictContentType_e dictContentType,
ZSTD_compressionParameters cParams)
{
ZSTD_ParamSwitch_e const useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(ZSTD_ps_auto, &cParams);
ZSTD_ParamSwitch_e const useRowMatchFinder = (ZSTD_ParamSwitch_e)
ZSTD_resolveRowMatchFinderMode((int)ZSTD_ps_auto, cParams);
ZSTD_rustCDictSizing const sizing = {
sizeof(ZSTD_CDict),
HUF_WORKSPACE_SIZE,
@@ -8075,16 +8014,19 @@ static void ZSTD_rust_compressStreamInit_resolveParams(void* params, int operati
ZSTD_CCtx_params* const cctxParams = (ZSTD_CCtx_params*)params;
switch (operation) {
case ZSTD_RUST_INIT_RESOLVE_BLOCK_SPLITTER:
cctxParams->postBlockSplitter = ZSTD_resolveBlockSplitterMode(
cctxParams->postBlockSplitter, &cctxParams->cParams);
cctxParams->postBlockSplitter = (ZSTD_ParamSwitch_e)
ZSTD_resolveBlockSplitterMode(
(int)cctxParams->postBlockSplitter, cctxParams->cParams);
break;
case ZSTD_RUST_INIT_RESOLVE_LDM:
cctxParams->ldmParams.enableLdm = ZSTD_resolveEnableLdm(
cctxParams->ldmParams.enableLdm, &cctxParams->cParams);
cctxParams->ldmParams.enableLdm = (ZSTD_ParamSwitch_e)
ZSTD_resolveEnableLdm(
(int)cctxParams->ldmParams.enableLdm, cctxParams->cParams);
break;
case ZSTD_RUST_INIT_RESOLVE_ROW_MATCH_FINDER:
cctxParams->useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(
cctxParams->useRowMatchFinder, &cctxParams->cParams);
cctxParams->useRowMatchFinder = (ZSTD_ParamSwitch_e)
ZSTD_resolveRowMatchFinderMode(
(int)cctxParams->useRowMatchFinder, cctxParams->cParams);
break;
case ZSTD_RUST_INIT_RESOLVE_VALIDATE_SEQUENCES:
cctxParams->validateSequences = ZSTD_resolveExternalSequenceValidation(
@@ -8094,8 +8036,10 @@ static void ZSTD_rust_compressStreamInit_resolveParams(void* params, int operati
cctxParams->maxBlockSize = ZSTD_resolveMaxBlockSize(cctxParams->maxBlockSize);
break;
case ZSTD_RUST_INIT_RESOLVE_EXTERNAL_REPCODE_SEARCH:
cctxParams->searchForExternalRepcodes = ZSTD_resolveExternalRepcodeSearch(
cctxParams->searchForExternalRepcodes, cctxParams->compressionLevel);
cctxParams->searchForExternalRepcodes = (ZSTD_ParamSwitch_e)
ZSTD_resolveExternalRepcodeSearch(
(int)cctxParams->searchForExternalRepcodes,
cctxParams->compressionLevel);
break;
default:
assert(0);