refactor(compress): move MT input hint dispatch to Rust

Project the scalar single-thread hint inputs and keep the multithreaded
context behind a C callback so Rust owns the worker-count dispatch. Preserve
the existing buffer-mode assertion and both hint arithmetic leaves, with
focused tests covering MT selection and single-thread fallback.

All heavy verification was run serially with a 40 GiB virtual-memory cap and
one build job.

Test Plan:
- git diff --cached --check
- ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo clippy --manifest-path rust/Cargo.toml --all-targets -- -D warnings
- ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/Cargo.toml --all-targets (790 passed)
- ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo clippy --manifest-path rust/cli/Cargo.toml --all-targets -- -D warnings
- ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/cli/Cargo.toml --all-targets (184 passed)
- ulimit -v 41943040; cargo +nightly fmt --manifest-path rust/Cargo.toml --all -- --check
- ulimit -v 41943040; make -j1
- ulimit -v 41943040; make -j1 -C tests test
This commit is contained in:
2026-07-20 08:21:06 +02:00
parent 22cb347f6e
commit 69425cb8a4
2 changed files with 187 additions and 23 deletions
+63 -23
View File
@@ -1521,6 +1521,39 @@ size_t ZSTD_rust_nextInputSizeHint(int inBufferMode,
size_t stableInNotConsumed,
size_t inBuffTarget,
size_t inBuffPos);
typedef size_t (*ZSTD_rust_nextInputSizeHintMTorST_f)(void* context);
typedef struct {
size_t blockSizeMax;
size_t stableInNotConsumed;
size_t inBuffTarget;
size_t inBuffPos;
int nbWorkers;
int inBufferMode;
void* mtContext;
ZSTD_rust_nextInputSizeHintMTorST_f mtNextInputSizeHint;
} ZSTD_rust_nextInputSizeHintMTorSTState;
typedef char ZSTD_rust_next_input_size_hint_mt_or_st_state_layout[
(sizeof(ZSTD_rust_nextInputSizeHintMTorST_f) == sizeof(void*)
&& offsetof(ZSTD_rust_nextInputSizeHintMTorSTState, blockSizeMax) == 0
&& offsetof(ZSTD_rust_nextInputSizeHintMTorSTState, stableInNotConsumed)
== sizeof(size_t)
&& offsetof(ZSTD_rust_nextInputSizeHintMTorSTState, inBuffTarget)
== 2 * sizeof(size_t)
&& offsetof(ZSTD_rust_nextInputSizeHintMTorSTState, inBuffPos)
== 3 * sizeof(size_t)
&& offsetof(ZSTD_rust_nextInputSizeHintMTorSTState, nbWorkers)
== 4 * sizeof(size_t)
&& offsetof(ZSTD_rust_nextInputSizeHintMTorSTState, inBufferMode)
== 4 * sizeof(size_t) + sizeof(int)
&& offsetof(ZSTD_rust_nextInputSizeHintMTorSTState, mtContext)
== 4 * sizeof(size_t) + 2 * sizeof(int)
&& offsetof(ZSTD_rust_nextInputSizeHintMTorSTState, mtNextInputSizeHint)
== 4 * sizeof(size_t) + 2 * sizeof(int) + sizeof(void*)
&& sizeof(ZSTD_rust_nextInputSizeHintMTorSTState)
== 4 * sizeof(size_t) + 2 * sizeof(int) + 2 * sizeof(void*))
? 1 : -1];
size_t ZSTD_rust_nextInputSizeHintMTorST(
const ZSTD_rust_nextInputSizeHintMTorSTState* state);
size_t ZSTD_rust_CStreamInSize(void);
size_t ZSTD_rust_CStreamOutSize(void);
size_t ZSTD_rust_sizeofCDict(size_t objectSize, size_t workspaceSize);
@@ -7788,24 +7821,6 @@ size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel)
/*====== Compression ======*/
static size_t ZSTD_nextInputSizeHint(const ZSTD_CCtx* cctx)
{
int const inBufferMode = (int)cctx->appliedParams.inBufferMode;
if (inBufferMode == ZSTD_bm_stable) {
return ZSTD_rust_nextInputSizeHint(inBufferMode,
cctx->blockSizeMax,
cctx->stableIn_notConsumed,
cctx->inBuffTarget,
cctx->inBuffPos);
}
assert(inBufferMode == ZSTD_bm_buffered);
return ZSTD_rust_nextInputSizeHint(inBufferMode,
cctx->blockSizeMax,
cctx->stableIn_notConsumed,
cctx->inBuffTarget,
cctx->inBuffPos);
}
/** ZSTD_compressStream_generic():
* internal function for all *compressStream*() variants
* @return : hint size for next input to complete ongoing block */
@@ -7860,16 +7875,41 @@ static size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs,
&state, output, input, (int)flushMode);
}
/* Keep the MT context opaque to Rust; only its existing C hint leaf crosses
* this callback boundary. */
#ifdef ZSTD_MULTITHREAD
static size_t ZSTD_nextInputSizeHint_MT(void* context)
{
return ZSTDMT_nextInputSizeHint((const ZSTDMT_CCtx*)context);
}
#endif
static size_t ZSTD_nextInputSizeHint_MTorST(const ZSTD_CCtx* cctx)
{
int const inBufferMode = (int)cctx->appliedParams.inBufferMode;
ZSTD_rust_nextInputSizeHintMTorSTState state = {
cctx->blockSizeMax,
cctx->stableIn_notConsumed,
cctx->inBuffTarget,
cctx->inBuffPos,
0,
inBufferMode,
NULL,
NULL
};
if (inBufferMode != ZSTD_bm_stable) {
assert(inBufferMode == ZSTD_bm_buffered);
}
#ifdef ZSTD_MULTITHREAD
if (cctx->appliedParams.nbWorkers >= 1) {
assert(cctx->mtctx != NULL);
return ZSTDMT_nextInputSizeHint(cctx->mtctx);
state.nbWorkers = cctx->appliedParams.nbWorkers;
state.mtContext = cctx->mtctx;
state.mtNextInputSizeHint = ZSTD_nextInputSizeHint_MT;
if (state.nbWorkers >= 1) {
assert(state.mtContext != NULL);
}
#endif
return ZSTD_nextInputSizeHint(cctx);
return ZSTD_rust_nextInputSizeHintMTorST(&state);
}
size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input)