feat(compress): move Fast dictionary table load to Rust

The dictionary-content orchestrator still routed the Fast hash-table fill
through a C callback even though the matching leaf already lived in Rust.
Replace that callback slot with a pointer-sized C/Rust projection containing
only the Fast leaf inputs, including a by-value copy of nextToUpdate and the
normalized CDict/full-load flags.  Keep the dtlm/tfp invariant assertion in C
and retain all bridge offsets and the double-Fast callback.

A broader MatchState projection would expose private layout and adding fields
would change the established 34-word bridge.  The narrow projection keeps
that ABI stable while making the Fast branch call ZSTD_rust_fillHashTable
directly.

Test Plan:
- `git diff --cached --check`
- `rustfmt --check --edition 2021 rust/src/zstd_compress_dictionary.rs` (reports only pre-existing formatting drift outside this change)
- Cargo, make, and full tests intentionally not run per task instructions.
This commit is contained in:
2026-07-21 20:42:04 +02:00
parent cc43ebac4e
commit a3ffd36a63
2 changed files with 152 additions and 20 deletions
+47 -12
View File
@@ -2776,6 +2776,37 @@ typedef void (*ZSTD_rust_loadDictionaryContent_fillLdm_f)(
void* context, const void* ip, const void* iend);
typedef void (*ZSTD_rust_loadDictionaryContent_overflowCorrect_f)(
void* context, const void* ip, const void* iend);
/* The Fast dictionary-table leaf is implemented in Rust. Keep only the
* fields needed by that leaf in the ABI projection; the complete
* ZSTD_MatchState_t layout remains private to C. */
typedef struct {
U32* hashTable;
const BYTE* base;
U32 nextToUpdate;
U32 hashLog;
U32 minMatch;
int fullTableLoad;
int forCDict;
} ZSTD_rust_loadDictionaryContentFastTableState;
typedef char ZSTD_rust_load_dictionary_fast_table_layout[
(offsetof(ZSTD_rust_loadDictionaryContentFastTableState, hashTable) == 0
&& offsetof(ZSTD_rust_loadDictionaryContentFastTableState, base)
== sizeof(void*)
&& offsetof(ZSTD_rust_loadDictionaryContentFastTableState, nextToUpdate)
== 2 * sizeof(void*)
&& offsetof(ZSTD_rust_loadDictionaryContentFastTableState, hashLog)
== 2 * sizeof(void*) + sizeof(U32)
&& offsetof(ZSTD_rust_loadDictionaryContentFastTableState, minMatch)
== 2 * sizeof(void*) + 2 * sizeof(U32)
&& offsetof(ZSTD_rust_loadDictionaryContentFastTableState, fullTableLoad)
== 2 * sizeof(void*) + 3 * sizeof(U32)
&& offsetof(ZSTD_rust_loadDictionaryContentFastTableState, forCDict)
== 2 * sizeof(void*) + 3 * sizeof(U32) + sizeof(int)
&& sizeof(ZSTD_rust_loadDictionaryContentFastTableState)
== ((offsetof(ZSTD_rust_loadDictionaryContentFastTableState, forCDict)
+ sizeof(int) + sizeof(void*) - 1)
/ sizeof(void*) * sizeof(void*)))
? 1 : -1];
typedef void (*ZSTD_rust_loadDictionaryContent_fillTable_f)(
void* context, const void* iend, int dtlm, int tfp);
typedef void (*ZSTD_rust_loadDictionaryContent_loadMatch_f)(
@@ -2811,7 +2842,7 @@ typedef struct {
ZSTD_rust_loadDictionaryContent_publishMatchState_f publishMatchState;
ZSTD_rust_loadDictionaryContent_fillLdm_f fillLdm;
ZSTD_rust_loadDictionaryContent_overflowCorrect_f overflowCorrect;
ZSTD_rust_loadDictionaryContent_fillTable_f fillHashTable;
const ZSTD_rust_loadDictionaryContentFastTableState* fastTable;
ZSTD_rust_loadDictionaryContent_fillTable_f fillDoubleHashTable;
ZSTD_rust_loadDictionaryContent_loadMatch_f loadDedicated;
ZSTD_rust_loadDictionaryContent_loadMatch_f loadRow;
@@ -2833,6 +2864,9 @@ ZSTD_RUST_LOAD_DICT_ASSERT(ZSTD_rust_load_dict_scalar_offset,
ZSTD_RUST_LOAD_DICT_ASSERT(ZSTD_rust_load_dict_callback_offset,
offsetof(ZSTD_rust_loadDictionaryContentState,
assertCParams) == 18 * sizeof(void*));
ZSTD_RUST_LOAD_DICT_ASSERT(ZSTD_rust_load_dict_fast_table_offset,
offsetof(ZSTD_rust_loadDictionaryContentState,
fastTable) == 25 * sizeof(void*));
ZSTD_RUST_LOAD_DICT_ASSERT(ZSTD_rust_load_dict_final_callback_offset,
offsetof(ZSTD_rust_loadDictionaryContentState,
publishFinalIndex) == 31 * sizeof(void*));
@@ -6858,16 +6892,6 @@ static void ZSTD_loadDictionaryContent_overflowCorrect(
context->params, ip, iend);
}
static void ZSTD_loadDictionaryContent_fillHashTable(
void* opaque, const void* iend, int dtlm, int tfp)
{
ZSTD_loadDictionaryContent_context const* const context =
(const ZSTD_loadDictionaryContent_context*)opaque;
ZSTD_fillHashTable(context->matchState, (const BYTE*)iend,
(ZSTD_dictTableLoadMethod_e)dtlm,
(ZSTD_tableFillPurpose_e)tfp);
}
static void ZSTD_loadDictionaryContent_fillDoubleHashTable(
void* opaque, const void* iend, int dtlm, int tfp)
{
@@ -6978,6 +7002,7 @@ static size_t ZSTD_loadDictionaryContent_callback(
int dtlm, int tfp)
{
ZSTD_loadDictionaryContent_context context;
ZSTD_rust_loadDictionaryContentFastTableState fastTable;
ZSTD_rust_loadDictionaryContentState state;
ZSTD_MatchState_t* const ms = (ZSTD_MatchState_t*)matchState;
ldmState_t* const ls = (ldmState_t*)ldmState;
@@ -6989,6 +7014,16 @@ static size_t ZSTD_loadDictionaryContent_callback(
context.workspace = ws;
context.params = cctxParams;
assert((tfp == ZSTD_tfp_forCDict && dtlm == ZSTD_dtlm_full)
|| (tfp != ZSTD_tfp_forCDict && dtlm == ZSTD_dtlm_fast));
fastTable.hashTable = ms->hashTable;
fastTable.base = ms->window.base;
fastTable.nextToUpdate = ms->nextToUpdate;
fastTable.hashLog = ms->cParams.hashLog;
fastTable.minMatch = ms->cParams.minMatch;
fastTable.fullTableLoad = dtlm == ZSTD_dtlm_full;
fastTable.forCDict = tfp == ZSTD_tfp_forCDict;
state.callbackContext = &context;
state.currentMax = ZSTD_CURRENT_MAX;
state.windowStartIndex = ZSTD_WINDOW_START_INDEX;
@@ -7014,7 +7049,7 @@ static size_t ZSTD_loadDictionaryContent_callback(
state.publishMatchState = ZSTD_loadDictionaryContent_publishMatchState;
state.fillLdm = ZSTD_loadDictionaryContent_fillLdm;
state.overflowCorrect = ZSTD_loadDictionaryContent_overflowCorrect;
state.fillHashTable = ZSTD_loadDictionaryContent_fillHashTable;
state.fastTable = &fastTable;
state.fillDoubleHashTable = ZSTD_loadDictionaryContent_fillDoubleHashTable;
state.loadDedicated = ZSTD_loadDictionaryContent_loadDedicated;
state.loadRow = ZSTD_loadDictionaryContent_loadRow;