feat(compress): move window dictionary policy into Rust

Move shared-window max-distance enforcement and dictionary-validity decisions into Rust. C keeps the private window pointer arithmetic, match-state pointer invalidation, and logging while applying the projected limit state returned by Rust.

Test Plan:
- ulimit -v 41943040; CARGO_BUILD_JOBS=1; cargo test --manifest-path rust/Cargo.toml
- ulimit -v 41943040; CARGO_BUILD_JOBS=1; cargo clippy --manifest-path rust/Cargo.toml --all-targets -- -D warnings
- ulimit -v 41943040; make -B -C programs -j1 zstd
- ulimit -v 41943040; make -C tests -j1 test-zstream ZSTREAM_TESTTIME=-T1s
This commit is contained in:
2026-07-19 20:05:29 +02:00
parent f8cabd39a4
commit 2557319583
2 changed files with 121 additions and 8 deletions
+92
View File
@@ -6857,6 +6857,57 @@ pub extern "C" fn ZSTD_rust_windowHasExtDict(dict_limit: u32, low_limit: u32) ->
(low_limit < dict_limit) as c_uint
}
/// Project the dictionary/window limit policy while C retains pointer-owned
/// match-state invalidation and the private window layout.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ZSTD_rust_windowDictState {
pub lowLimit: u32,
pub dictLimit: u32,
pub loadedDictEnd: u32,
pub invalidate: u32,
}
const _: () = {
assert!(offset_of!(ZSTD_rust_windowDictState, lowLimit) == 0);
assert!(offset_of!(ZSTD_rust_windowDictState, dictLimit) == size_of::<u32>());
assert!(offset_of!(ZSTD_rust_windowDictState, loadedDictEnd) == 2 * size_of::<u32>());
assert!(offset_of!(ZSTD_rust_windowDictState, invalidate) == 3 * size_of::<u32>());
assert!(size_of::<ZSTD_rust_windowDictState>() == 4 * size_of::<u32>());
};
#[no_mangle]
pub extern "C" fn ZSTD_rust_windowEnforceMaxDist(
block_end_idx: u32,
max_dist: u32,
mut state: ZSTD_rust_windowDictState,
) -> ZSTD_rust_windowDictState {
state.invalidate = 0;
if block_end_idx > max_dist.wrapping_add(state.loadedDictEnd) {
let new_low_limit = block_end_idx.wrapping_sub(max_dist);
if state.lowLimit < new_low_limit {
state.lowLimit = new_low_limit;
}
if state.dictLimit < state.lowLimit {
state.dictLimit = state.lowLimit;
}
state.loadedDictEnd = 0;
state.invalidate = 1;
}
state
}
#[no_mangle]
pub extern "C" fn ZSTD_rust_windowCheckDictValidity(
block_end_idx: u32,
max_dist: u32,
loaded_dict_end: u32,
dict_limit: u32,
) -> c_uint {
(block_end_idx > loaded_dict_end.wrapping_add(max_dist) || loaded_dict_end != dict_limit)
as c_uint
}
/// Update the rolling prefix/ext-dictionary window for one input segment.
/// C owns the containing match state; Rust owns this five-field transition.
#[no_mangle]
@@ -12674,6 +12725,47 @@ mod tests {
assert_eq!(ZSTD_rust_windowHasExtDict(10, 10), 0);
}
#[test]
fn window_enforce_max_dist_updates_limits_and_invalidates_dictionary() {
let state = ZSTD_rust_windowEnforceMaxDist(
100,
64,
ZSTD_rust_windowDictState {
lowLimit: 2,
dictLimit: 10,
loadedDictEnd: 8,
invalidate: 0,
},
);
assert_eq!(state.lowLimit, 36);
assert_eq!(state.dictLimit, 36);
assert_eq!(state.loadedDictEnd, 0);
assert_eq!(state.invalidate, 1);
}
#[test]
fn window_dictionary_policy_preserves_valid_state_and_detects_mismatch() {
let state = ZSTD_rust_windowEnforceMaxDist(
70,
64,
ZSTD_rust_windowDictState {
lowLimit: 2,
dictLimit: 10,
loadedDictEnd: 8,
invalidate: 1,
},
);
assert_eq!(state.lowLimit, 2);
assert_eq!(state.dictLimit, 10);
assert_eq!(state.loadedDictEnd, 8);
assert_eq!(state.invalidate, 0);
assert_eq!(ZSTD_rust_windowCheckDictValidity(70, 64, 8, 8), 0);
assert_eq!(ZSTD_rust_windowCheckDictValidity(100, 64, 8, 8), 1);
assert_eq!(ZSTD_rust_windowCheckDictValidity(70, 64, 8, 10), 1);
}
#[test]
fn window_update_preserves_contiguous_input_and_clips_overlapping_extdict() {
let storage = [0u8; 64];