feat(compress): move overflow policy arithmetic to Rust

The window overflow gate mixed two scalar decisions with C-owned pointer and
window state. The early correction test uses the block-start index and must
wait for dictionary invalidation, while the normal fallback uses the block-end
index and the platform-selected ZSTD_CURRENT_MAX threshold. Keeping those
subtractions and the ZSTD_window_t wrapper in C preserves the existing call
contract without making Rust depend on pointer width or private state.

Move the U32 cycle/MAX arithmetic, correction-count scaling, dictionary gate,
and frequent-policy decision into Rust. C passes the two pointer-derived
indices only through scalar results, and supplies the current-max threshold
and active build policy explicitly so 32/64-bit and fuzzing configurations
remain authoritative.

Test Plan:
- `cargo test --manifest-path rust/Cargo.toml --no-default-features
  --features compression` -- 348 passed.
- Compression clippy for the library, benches, and tests, followed by
  nightly fmt and the repeated three clippy checks -- passed.
- `make -C lib -j2 lib-mt` and `make -C lib -j2 lib-nomt` -- passed.
- `make -C tests -j2 fuzzer` and `./tests/fuzzer -s4142 -t63 -i64 -v` --
  passed all 64 cases.
- `make -C tests -j2 test-zstream` -- passed 84 deterministic, 5,477 first
  randomized, and 6,902 new-API randomized cases; the existing unterminated
  string initializer warning remains.
This commit is contained in:
2026-07-18 15:30:19 +02:00
parent 23450d8c3b
commit 293cf96892
2 changed files with 142 additions and 27 deletions
+16 -27
View File
@@ -655,6 +655,13 @@ size_t ZSTD_rust_noCompressBlock(void* dst, size_t dstCapacity,
size_t ZSTD_rust_rleCompressBlock(void* dst, size_t dstCapacity, BYTE src,
size_t srcSize, U32 lastBlock);
U32 ZSTD_rust_windowCorrectOverflow(U32 curr, U32 cycleLog, U32 maxDist);
U32 ZSTD_rust_windowCanOverflowCorrect(U32 curr, U32 cycleLog, U32 maxDist,
U32 loadedDictEnd,
U32 nbOverflowCorrections);
U32 ZSTD_rust_windowNeedOverflowCorrection(U32 curr,
U32 canOverflowCorrect,
U32 currentMax,
int overflowCorrectFrequently);
/* ZSTD_minGain() :
@@ -1084,28 +1091,10 @@ MEM_STATIC U32 ZSTD_window_canOverflowCorrect(ZSTD_window_t const window,
U32 loadedDictEnd,
void const* src)
{
U32 const cycleSize = 1u << cycleLog;
U32 const curr = (U32)((BYTE const*)src - window.base);
U32 const minIndexToOverflowCorrect = cycleSize
+ MAX(maxDist, cycleSize)
+ ZSTD_WINDOW_START_INDEX;
/* Adjust the min index to backoff the overflow correction frequency,
* so we don't waste too much CPU in overflow correction. If this
* computation overflows we don't really care, we just need to make
* sure it is at least minIndexToOverflowCorrect.
*/
U32 const adjustment = window.nbOverflowCorrections + 1;
U32 const adjustedIndex = MAX(minIndexToOverflowCorrect * adjustment,
minIndexToOverflowCorrect);
U32 const indexLargeEnough = curr > adjustedIndex;
/* Only overflow correct early if the dictionary is invalidated already,
* so we don't hurt compression ratio.
*/
U32 const dictionaryInvalidated = curr > maxDist + loadedDictEnd;
return indexLargeEnough && dictionaryInvalidated;
return ZSTD_rust_windowCanOverflowCorrect(
curr, cycleLog, maxDist, loadedDictEnd,
window.nbOverflowCorrections);
}
/**
@@ -1121,12 +1110,12 @@ MEM_STATIC U32 ZSTD_window_needOverflowCorrection(ZSTD_window_t const window,
void const* srcEnd)
{
U32 const curr = (U32)((BYTE const*)srcEnd - window.base);
if (ZSTD_WINDOW_OVERFLOW_CORRECT_FREQUENTLY) {
if (ZSTD_window_canOverflowCorrect(window, cycleLog, maxDist, loadedDictEnd, src)) {
return 1;
}
}
return curr > ZSTD_CURRENT_MAX;
U32 const canOverflowCorrect =
ZSTD_window_canOverflowCorrect(window, cycleLog, maxDist,
loadedDictEnd, src);
return ZSTD_rust_windowNeedOverflowCorrection(
curr, canOverflowCorrect, ZSTD_CURRENT_MAX,
ZSTD_WINDOW_OVERFLOW_CORRECT_FREQUENTLY);
}
/**