feat(compress): move hash and window reset policies to Rust

Move hash-salt mixing and context-reset overflow predicates behind scalar Rust ABI shims. Keep the C-owned match state and window arithmetic in the C wrapper while preserving exact architecture-dependent thresholds.

Test Plan:

- cargo test --manifest-path rust/Cargo.toml --no-default-features --features compression (186 tests)

- cargo clippy for library, benches, tests, and CLI all-targets

- make -B -C lib -j2 lib

- make -B -C tests -j2 test-zstream (84 deterministic tests; fuzzers 6451 and 9879 cases)
This commit is contained in:
2026-07-18 05:06:32 +02:00
parent 52deffe66c
commit cba0373a2b
2 changed files with 87 additions and 13 deletions
+7 -13
View File
@@ -28,7 +28,7 @@
#include "zstd_opt.h"
#include "zstd_ldm.h"
#include "zstd_compress_superblock.h"
#include "../common/bits.h" /* ZSTD_highbit32, ZSTD_rotateRight_U64 */
#include "../common/bits.h" /* ZSTD_highbit32 */
/* Frame serialization lives in Rust. Keep its interface scalar so the
* large, configuration-sensitive CCtx parameter structure stays in C. */
@@ -54,6 +54,9 @@ int ZSTD_rust_simpleCompress2Level(const void* cctx);
int ZSTD_rust_simpleCompressStream2Level(const void* cctx);
void ZSTD_rust_reduceTable(U32* table, U32 size, U32 reducerValue,
int preserveMark);
U64 ZSTD_rust_advanceHashSalt(U64 hashSalt, U64 hashSaltEntropy);
int ZSTD_rust_indexTooCloseToMax(size_t nextSrcBaseOffset);
int ZSTD_rust_dictTooBig(size_t loadedDictSize);
/* Context-free compression-parameter selection and sizing leaves live in
* Rust (rust/src/zstd_compress_params.rs). This file retains
@@ -1445,18 +1448,9 @@ typedef enum {
ZSTD_resetTarget_CCtx
} ZSTD_resetTarget_e;
/* Mixes bits in a 64 bits in a value, based on XXH3_rrmxmx */
static U64 ZSTD_bitmix(U64 val, U64 len) {
val ^= ZSTD_rotateRight_U64(val, 49) ^ ZSTD_rotateRight_U64(val, 24);
val *= 0x9FB21C651E98DF25ULL;
val ^= (val >> 35) + len ;
val *= 0x9FB21C651E98DF25ULL;
return val ^ (val >> 28);
}
/* Mixes in the hashSalt and hashSaltEntropy to create a new hashSalt */
static void ZSTD_advanceHashSalt(ZSTD_MatchState_t* ms) {
ms->hashSalt = ZSTD_bitmix(ms->hashSalt, 8) ^ ZSTD_bitmix((U64) ms->hashSaltEntropy, 4);
ms->hashSalt = ZSTD_rust_advanceHashSalt(ms->hashSalt, (U64)ms->hashSaltEntropy);
}
static size_t
@@ -1556,7 +1550,7 @@ ZSTD_reset_matchState(ZSTD_MatchState_t* ms,
#define ZSTD_INDEXOVERFLOW_MARGIN (16 MB)
static int ZSTD_indexTooCloseToMax(ZSTD_window_t w)
{
return (size_t)(w.nextSrc - w.base) > (ZSTD_CURRENT_MAX - ZSTD_INDEXOVERFLOW_MARGIN);
return ZSTD_rust_indexTooCloseToMax((size_t)(w.nextSrc - w.base));
}
/** ZSTD_dictTooBig():
@@ -1566,7 +1560,7 @@ static int ZSTD_indexTooCloseToMax(ZSTD_window_t w)
*/
static int ZSTD_dictTooBig(size_t const loadedDictSize)
{
return loadedDictSize > ZSTD_CHUNKSIZE_MAX;
return ZSTD_rust_dictTooBig(loadedDictSize);
}
/*! ZSTD_resetCCtx_internal() :