refactor(mt): move resize policy to Rust

Move MT resize callback ordering and short-circuit failure policy into Rust.
C retains the private factory, jobs table, buffer pool, CCtx pool, sequence pool,
and parameter storage behind narrow callbacks, including the original ignored
worker-parameter setter result.

Test Plan:
- ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo +nightly fmt --manifest-path rust/Cargo.toml -- --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 (784 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 (179 passed)
- ulimit -v 41943040; make -j1
- ulimit -v 41943040; make -j1 -C tests test (all tests completed successfully)
This commit is contained in:
2026-07-20 07:08:36 +02:00
parent 2b2f9df5de
commit dc3c9c6c11
2 changed files with 263 additions and 10 deletions
+68 -10
View File
@@ -695,6 +695,25 @@ size_t ZSTDMT_rust_initCStream(
ZSTDMT_initResetStreamFn resetStream,
ZSTDMT_initDictionaryFn updateDictionary,
ZSTDMT_initSerialResetFn serialReset);
typedef struct {
void* callbackContext;
unsigned nbWorkers;
} ZSTDMT_RustResizeProjection;
typedef char ZSTDMT_rust_resize_projection_layout[
(offsetof(ZSTDMT_RustResizeProjection, callbackContext) == 0
&& offsetof(ZSTDMT_RustResizeProjection, nbWorkers) == sizeof(void*)
&& sizeof(ZSTDMT_RustResizeProjection)
== (sizeof(void*) == 8 ? 16 : 8)) ? 1 : -1];
typedef size_t (*ZSTDMT_resizeStepFn)(void* opaque, unsigned nbWorkers);
typedef void* (*ZSTDMT_resizeResourceFn)(void* opaque, unsigned nbWorkers);
size_t ZSTDMT_rust_resize(
const ZSTDMT_RustResizeProjection* projection,
ZSTDMT_resizeStepFn resizeFactory,
ZSTDMT_resizeStepFn expandJobs,
ZSTDMT_resizeResourceFn expandBufferPool,
ZSTDMT_resizeResourceFn expandCCtxPool,
ZSTDMT_resizeResourceFn expandSeqPool,
ZSTDMT_resizeStepFn setNbWorkers);
typedef struct {
size_t consumed;
size_t cSize;
@@ -2156,20 +2175,59 @@ size_t ZSTDMT_sizeof_CCtx(ZSTDMT_CCtx* mtctx)
}
static size_t ZSTDMT_rust_resizeFactory(void* opaque, unsigned nbWorkers)
{
ZSTDMT_CCtx* const mtctx = (ZSTDMT_CCtx*)opaque;
return POOL_resize(mtctx->factory, nbWorkers) ? ERROR(memory_allocation) : 0;
}
static size_t ZSTDMT_rust_resizeJobs(void* opaque, unsigned nbWorkers)
{
return ZSTDMT_expandJobsTable((ZSTDMT_CCtx*)opaque, nbWorkers);
}
static void* ZSTDMT_rust_resizeBufferPool(void* opaque, unsigned nbWorkers)
{
ZSTDMT_CCtx* const mtctx = (ZSTDMT_CCtx*)opaque;
mtctx->bufPool = ZSTDMT_expandBufferPool(
mtctx->bufPool, BUF_POOL_MAX_NB_BUFFERS(nbWorkers));
return mtctx->bufPool;
}
static void* ZSTDMT_rust_resizeCCtxPool(void* opaque, unsigned nbWorkers)
{
ZSTDMT_CCtx* const mtctx = (ZSTDMT_CCtx*)opaque;
mtctx->cctxPool = ZSTDMT_expandCCtxPool(mtctx->cctxPool, nbWorkers);
return mtctx->cctxPool;
}
static void* ZSTDMT_rust_resizeSeqPool(void* opaque, unsigned nbWorkers)
{
ZSTDMT_CCtx* const mtctx = (ZSTDMT_CCtx*)opaque;
mtctx->seqPool = ZSTDMT_expandSeqPool(mtctx->seqPool, nbWorkers);
return mtctx->seqPool;
}
static size_t ZSTDMT_rust_resizeSetNbWorkers(void* opaque, unsigned nbWorkers)
{
ZSTDMT_CCtx* const mtctx = (ZSTDMT_CCtx*)opaque;
(void)ZSTDMT_CCtxParam_setNbWorkers(&mtctx->params, nbWorkers);
return 0;
}
/* ZSTDMT_resize() :
* @return : error code if fails, 0 on success */
static size_t ZSTDMT_resize(ZSTDMT_CCtx* mtctx, unsigned nbWorkers)
{
if (POOL_resize(mtctx->factory, nbWorkers)) return ERROR(memory_allocation);
FORWARD_IF_ERROR( ZSTDMT_expandJobsTable(mtctx, nbWorkers) , "");
mtctx->bufPool = ZSTDMT_expandBufferPool(mtctx->bufPool, BUF_POOL_MAX_NB_BUFFERS(nbWorkers));
if (mtctx->bufPool == NULL) return ERROR(memory_allocation);
mtctx->cctxPool = ZSTDMT_expandCCtxPool(mtctx->cctxPool, nbWorkers);
if (mtctx->cctxPool == NULL) return ERROR(memory_allocation);
mtctx->seqPool = ZSTDMT_expandSeqPool(mtctx->seqPool, nbWorkers);
if (mtctx->seqPool == NULL) return ERROR(memory_allocation);
ZSTDMT_CCtxParam_setNbWorkers(&mtctx->params, nbWorkers);
return 0;
ZSTDMT_RustResizeProjection const projection = { mtctx, nbWorkers };
return ZSTDMT_rust_resize(
&projection,
ZSTDMT_rust_resizeFactory,
ZSTDMT_rust_resizeJobs,
ZSTDMT_rust_resizeBufferPool,
ZSTDMT_rust_resizeCCtxPool,
ZSTDMT_rust_resizeSeqPool,
ZSTDMT_rust_resizeSetNbWorkers);
}