refactor(mt): move stream dictionary branch policy to Rust

The MT stream initializer and its later dictionary update both encoded the
same three-way choice in C: copy a supplied dictionary, attach a borrowed
CDict, or install a raw prefix. The old code also embedded the required
release/clear-before-attach ordering in each callback wrapper. Project the
presence and raw-content flags into Rust, where the branch and ordering are
now explicit and tested. C retains only the private CDict allocation,
prefix-storage, context publication, and destruction callbacks, so the
configured C layouts and allocator behavior remain unchanged.

Test Plan:
- `ulimit -v 41943040; cargo +nightly fmt --manifest-path rust/Cargo.toml --all -- --check` -- passed
- `ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo clippy --manifest-path rust/Cargo.toml --all-targets -- -D warnings` -- passed
- `git diff --check` and `rustfmt --edition 2021 --check` -- passed before commit
- Capped native and original-test verification remains pending for the complete batch.
This commit is contained in:
2026-07-20 19:47:51 +02:00
parent ee6953b1d7
commit b0e90c39e2
2 changed files with 390 additions and 37 deletions
+88 -31
View File
@@ -742,6 +742,27 @@ typedef void (*ZSTDMT_initSetBufferSizeFn)(void* opaque, size_t size);
typedef size_t (*ZSTDMT_initResizeRoundBufferFn)(void* opaque, size_t capacity);
typedef void (*ZSTDMT_initResetStreamFn)(void* opaque);
typedef size_t (*ZSTDMT_initSerialResetFn)(void* opaque, size_t targetSectionSize);
typedef struct {
unsigned hasDictionary;
unsigned rawContent;
} ZSTDMT_RustDictionaryProjection;
typedef char ZSTDMT_rust_dictionary_projection_layout[
(offsetof(ZSTDMT_RustDictionaryProjection, hasDictionary) == 0
&& offsetof(ZSTDMT_RustDictionaryProjection, rawContent)
== sizeof(unsigned)
&& sizeof(ZSTDMT_RustDictionaryProjection) == 2 * sizeof(unsigned))
? 1 : -1];
size_t ZSTDMT_rust_prepareCStreamDictionary(
const ZSTDMT_RustDictionaryProjection* projection, void* opaque,
ZSTDMT_initResetStreamFn releaseLocal,
ZSTDMT_initDictionaryFn createCopied,
ZSTDMT_initResetStreamFn attachBorrowed);
size_t ZSTDMT_rust_updateCStreamDictionary(
const ZSTDMT_RustDictionaryProjection* projection, void* opaque,
ZSTDMT_initResetStreamFn clearLocal,
ZSTDMT_initResetStreamFn attachBorrowed,
ZSTDMT_initResetStreamFn setRawPrefix,
ZSTDMT_initDictionaryFn createReferenced);
typedef void (*ZSTDMT_serialResetVoidFn)(void* opaque);
typedef void (*ZSTDMT_serialResetSetNbSeqFn)(void* opaque, size_t nbSeq);
typedef int (*ZSTDMT_serialResetResizeFn)(void* opaque);
@@ -2634,23 +2655,42 @@ static void ZSTDMT_initCStreamApplyParameters(void* opaque, size_t jobSize)
state->mtctx->frameContentSize = state->pledgedSrcSize;
}
static size_t ZSTDMT_initCStreamPrepareDictionary(void* opaque)
static void ZSTDMT_initCStreamReleaseLocalCDict(void* opaque)
{
ZSTDMT_initCStreamState* const state = (ZSTDMT_initCStreamState*)opaque;
ZSTD_freeCDict(state->mtctx->cdictLocal);
}
static size_t ZSTDMT_initCStreamCreateCopiedCDict(void* opaque)
{
ZSTDMT_initCStreamState* const state = (ZSTDMT_initCStreamState*)opaque;
ZSTDMT_CCtx* const mtctx = state->mtctx;
mtctx->cdictLocal = ZSTD_createCDict_advanced(
state->dict, state->dictSize, ZSTD_dlm_byCopy,
state->dictContentType, state->params.cParams, mtctx->cMem);
mtctx->cdict = mtctx->cdictLocal;
return mtctx->cdictLocal == NULL ? ERROR(memory_allocation) : 0;
}
ZSTD_freeCDict(mtctx->cdictLocal);
if (state->dict) {
mtctx->cdictLocal = ZSTD_createCDict_advanced(
state->dict, state->dictSize, ZSTD_dlm_byCopy,
state->dictContentType, state->params.cParams, mtctx->cMem);
mtctx->cdict = mtctx->cdictLocal;
if (mtctx->cdictLocal == NULL) return ERROR(memory_allocation);
} else {
mtctx->cdictLocal = NULL;
mtctx->cdict = state->cdict;
}
return 0;
static void ZSTDMT_initCStreamAttachBorrowedCDict(void* opaque)
{
ZSTDMT_initCStreamState* const state = (ZSTDMT_initCStreamState*)opaque;
state->mtctx->cdictLocal = NULL;
state->mtctx->cdict = state->cdict;
}
static size_t ZSTDMT_initCStreamPrepareDictionary(void* opaque)
{
ZSTDMT_initCStreamState* const state = (ZSTDMT_initCStreamState*)opaque;
ZSTDMT_RustDictionaryProjection const projection = {
state->dict != NULL,
state->dictContentType == ZSTD_dct_rawContent
};
return ZSTDMT_rust_prepareCStreamDictionary(
&projection, state,
ZSTDMT_initCStreamReleaseLocalCDict,
ZSTDMT_initCStreamCreateCopiedCDict,
ZSTDMT_initCStreamAttachBorrowedCDict);
}
static void ZSTDMT_initCStreamSetTargetPrefixSize(void* opaque, size_t size)
@@ -2753,30 +2793,47 @@ static void ZSTDMT_initCStreamResetStream(void* opaque)
ZSTDMT_rust_resetStream(&resetState);
}
static size_t ZSTDMT_initCStreamUpdateDictionary(void* opaque)
static void ZSTDMT_initCStreamClearDictionary(void* opaque)
{
ZSTDMT_initCStreamState* const state = (ZSTDMT_initCStreamState*)opaque;
ZSTDMT_CCtx* const mtctx = state->mtctx;
ZSTD_freeCDict(mtctx->cdictLocal);
mtctx->cdictLocal = NULL;
mtctx->cdict = NULL;
if (state->dict) {
if (state->dictContentType == ZSTD_dct_rawContent) {
mtctx->inBuff.prefix.start = (const BYTE*)state->dict;
mtctx->inBuff.prefix.size = state->dictSize;
} else {
/* note : a loadPrefix becomes an internal CDict */
mtctx->cdictLocal = ZSTD_createCDict_advanced(
state->dict, state->dictSize, ZSTD_dlm_byRef,
state->dictContentType, state->params.cParams, mtctx->cMem);
mtctx->cdict = mtctx->cdictLocal;
if (mtctx->cdictLocal == NULL) return ERROR(memory_allocation);
}
} else {
mtctx->cdict = state->cdict;
}
return 0;
}
static void ZSTDMT_initCStreamSetRawPrefix(void* opaque)
{
ZSTDMT_initCStreamState* const state = (ZSTDMT_initCStreamState*)opaque;
state->mtctx->inBuff.prefix.start = (const BYTE*)state->dict;
state->mtctx->inBuff.prefix.size = state->dictSize;
}
static size_t ZSTDMT_initCStreamCreateReferencedCDict(void* opaque)
{
ZSTDMT_initCStreamState* const state = (ZSTDMT_initCStreamState*)opaque;
ZSTDMT_CCtx* const mtctx = state->mtctx;
/* note : a loadPrefix becomes an internal CDict */
mtctx->cdictLocal = ZSTD_createCDict_advanced(
state->dict, state->dictSize, ZSTD_dlm_byRef,
state->dictContentType, state->params.cParams, mtctx->cMem);
mtctx->cdict = mtctx->cdictLocal;
return mtctx->cdictLocal == NULL ? ERROR(memory_allocation) : 0;
}
static size_t ZSTDMT_initCStreamUpdateDictionary(void* opaque)
{
ZSTDMT_initCStreamState* const state = (ZSTDMT_initCStreamState*)opaque;
ZSTDMT_RustDictionaryProjection const projection = {
state->dict != NULL,
state->dictContentType == ZSTD_dct_rawContent
};
return ZSTDMT_rust_updateCStreamDictionary(
&projection, state,
ZSTDMT_initCStreamClearDictionary,
ZSTDMT_initCStreamAttachBorrowedCDict,
ZSTDMT_initCStreamSetRawPrefix,
ZSTDMT_initCStreamCreateReferencedCDict);
}
static size_t ZSTDMT_initCStreamSerialReset(void* opaque, size_t targetSectionSize)