refactor(cdict): move advanced workspace sizing into Rust

The advanced-CDict path previously kept a C callback that rebuilt the
ZSTD_rustCDictSizing projection and delegated the actual arithmetic back to
Rust. Replace that callback with a pointer to the explicit sizing projection,
so Rust owns the workspace-size policy while C continues to own private
zstd_cwksp allocation, object reservation, initialization, and teardown. The
projection replaces the old callback slot, preserving every later callback
offset and the synchronous lifetime of the C sizing values.

Update both sides' layout assertions, remove the redundant C helper, and make
the lifecycle tests assert that Rust's computed size reaches allocation and
workspace creation before the existing C-owned callbacks run. Custom-memory
validation and all other CDict callbacks remain unchanged.

Test Plan:
- `cc -fsyntax-only -Werror=incompatible-pointer-types -Ilib -Ilib/common -Ilib/compress -Ilib/decompress -Ilib/dict -Ilib/legacy lib/compress/zstd_compress.c` -- passed.
- `rustfmt +nightly --edition 2021 --check rust/src/zstd_compress_dictionary.rs` -- passed.
- `git diff --check` and `git diff --cached --check` -- passed.
- Cargo build/test/clippy, Make, fuzzers, and other heavy verification were intentionally not run per request.
This commit is contained in:
2026-07-20 15:00:52 +02:00
parent c2bed1242c
commit 0e93557879
2 changed files with 136 additions and 104 deletions
+25 -29
View File
@@ -2094,6 +2094,20 @@ typedef struct {
size_t optimalTSize;
size_t asanRedzoneSize;
} ZSTD_rustCDictSizing;
typedef char zstd_rust_cdict_sizing_layout[
(offsetof(ZSTD_rustCDictSizing, cdictSize) == 0
&& offsetof(ZSTD_rustCDictSizing, hufWorkspaceSize)
== sizeof(size_t)
&& offsetof(ZSTD_rustCDictSizing, hashLog3Max)
== 2 * sizeof(size_t)
&& offsetof(ZSTD_rustCDictSizing, matchTSize)
== 3 * sizeof(size_t)
&& offsetof(ZSTD_rustCDictSizing, optimalTSize)
== 4 * sizeof(size_t)
&& offsetof(ZSTD_rustCDictSizing, asanRedzoneSize)
== 5 * sizeof(size_t)
&& sizeof(ZSTD_rustCDictSizing) == 6 * sizeof(size_t))
? 1 : -1];
size_t ZSTD_rust_params_estimateCDictSizeFromCParams(
size_t dictSize, ZSTD_compressionParameters cParams,
int dictLoadMethod, const ZSTD_rustCDictSizing* sizing);
@@ -2658,10 +2672,6 @@ typedef char ZSTD_rust_init_cdict_state_layout[
? 1 : -1];
typedef int (*ZSTD_rust_createCDictAdvancedValidateCustomMem_f)(void* context);
typedef size_t (*ZSTD_rust_createCDictAdvancedWorkspaceSize_f)(
void* context, size_t dictSize, int dictLoadMethod,
const ZSTD_compressionParameters* cParams,
int useRowMatchFinder, int enableDedicatedDictSearch);
typedef void* (*ZSTD_rust_createCDictAdvancedAllocate_f)(
void* context, size_t workspaceSize);
typedef void (*ZSTD_rust_createCDictAdvancedCreateWorkspace_f)(
@@ -2689,7 +2699,7 @@ typedef struct {
U32 exclusionMask;
U32 ldmDefaultWindowLog;
ZSTD_rust_createCDictAdvancedValidateCustomMem_f validateCustomMem;
ZSTD_rust_createCDictAdvancedWorkspaceSize_f workspaceSize;
const ZSTD_rustCDictSizing* sizing;
ZSTD_rust_createCDictAdvancedAllocate_f allocate;
ZSTD_rust_createCDictAdvancedCreateWorkspace_f createWorkspace;
ZSTD_rust_createCDictAdvancedReserveObject_f reserveObject;
@@ -2719,7 +2729,7 @@ typedef char ZSTD_rust_create_cdict_advanced_state_layout[
== 5 * sizeof(void*) + sizeof(U32)
&& offsetof(ZSTD_rust_createCDictAdvancedState, validateCustomMem)
== 5 * sizeof(void*) + 2 * sizeof(U32)
&& offsetof(ZSTD_rust_createCDictAdvancedState, workspaceSize)
&& offsetof(ZSTD_rust_createCDictAdvancedState, sizing)
== 6 * sizeof(void*) + 2 * sizeof(U32)
&& offsetof(ZSTD_rust_createCDictAdvancedState, allocate)
== 7 * sizeof(void*) + 2 * sizeof(U32)
@@ -6880,28 +6890,6 @@ static int ZSTD_rust_createCDictAdvanced_validateCustomMem(void* context)
return ((!customMem->customAlloc) ^ (!customMem->customFree)) == 0;
}
static size_t ZSTD_rust_createCDictAdvanced_workspaceSize(
void* context, size_t dictSize, int dictLoadMethod,
const ZSTD_compressionParameters* cParams,
int useRowMatchFinder, int enableDedicatedDictSearch)
{
(void)context;
if (cParams == NULL) return 0;
{ ZSTD_rustCDictSizing const sizing = {
sizeof(ZSTD_CDict),
HUF_WORKSPACE_SIZE,
ZSTD_HASHLOG3_MAX,
sizeof(ZSTD_match_t),
sizeof(ZSTD_optimal_t),
ZSTD_RUST_ASAN_REDZONE_SIZE
};
return ZSTD_rust_params_estimateCDictWorkspaceSize(
dictSize, *cParams, dictLoadMethod,
useRowMatchFinder, enableDedicatedDictSearch,
&sizing);
}
}
static void* ZSTD_rust_createCDictAdvanced_allocate(
void* context, size_t workspaceSize)
{
@@ -7035,6 +7023,14 @@ ZSTD_CDict* ZSTD_createCDict_advanced2(
ZSTD_CCtx_params cctxParams;
ZSTD_rust_createCDictAdvancedContext context;
ZSTD_rust_createCDictAdvancedState state;
ZSTD_rustCDictSizing const sizing = {
sizeof(ZSTD_CDict),
HUF_WORKSPACE_SIZE,
ZSTD_HASHLOG3_MAX,
sizeof(ZSTD_match_t),
sizeof(ZSTD_optimal_t),
ZSTD_RUST_ASAN_REDZONE_SIZE
};
DEBUGLOG(3, "ZSTD_createCDict_advanced2, dictSize=%u, mode=%u", (unsigned)dictSize, (unsigned)dictContentType);
if (originalCctxParams == NULL) return NULL;
@@ -7049,7 +7045,7 @@ ZSTD_CDict* ZSTD_createCDict_advanced2(
state.exclusionMask = ZSTD_getCParamsExclusionMask();
state.ldmDefaultWindowLog = ZSTD_LDM_DEFAULT_WINDOW_LOG;
state.validateCustomMem = ZSTD_rust_createCDictAdvanced_validateCustomMem;
state.workspaceSize = ZSTD_rust_createCDictAdvanced_workspaceSize;
state.sizing = &sizing;
state.allocate = ZSTD_rust_createCDictAdvanced_allocate;
state.createWorkspace = ZSTD_rust_createCDictAdvanced_createWorkspace;
state.reserveObject = ZSTD_rust_createCDictAdvanced_reserveObject;