feat(compress): move block compressor index policy to Rust
Keep ZSTD_selectBlockCompressor's compressor tables, callback pointers, and final dictionary-mode table lookup in C while moving only the scalar choice of row-based versus ordinary table index to the Rust parameter-policy module. The Rust helper reuses the existing row-matchfinder policy, returns 0..2 for row entries, and returns 3 plus the strategy for ordinary entries. C retains its public/internal signature, strategy and row-mode assertions, numeric fast strategy assumption, and all private callback ownership. Test Plan: - Rust clippy normal, benches, and tests before and after nightly formatting -- passed. - `cargo test --manifest-path rust/Cargo.toml --no-default-features --features compression zstd_compress_params` -- 41 passed. - `make -B -C lib -j2 lib` -- passed without new warnings. - `make -C tests test-rust-lib-smoke` -- passed. - `tests/fuzzer -s4560 -t47 -i48 -v` and `-s4560 -t56 -i57 -v` -- passed. - `make -C tests -j2 test-zstream` -- 84 named tests plus 6,708 and 8,980 randomized cases passed. - `git diff --check` and `git diff --cached --check` -- passed. - The pre-existing `tests/zstreamtest.c:1899` unterminated-string warning remains.
This commit is contained in:
@@ -335,6 +335,25 @@ pub extern "C" fn ZSTD_rust_params_rowMatchFinderUsed(strategy: c_int, mode: c_i
|
||||
c_int::from(row_match_finder_used(strategy, mode))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn select_block_compressor(strategy: c_int, mode: c_int) -> c_int {
|
||||
debug_assert!((ZSTD_FAST..=ZSTD_BTULTRA2).contains(&strategy));
|
||||
if row_match_finder_used(strategy, mode) {
|
||||
strategy - ZSTD_GREEDY
|
||||
} else {
|
||||
3 + strategy
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the C block-compressor table index encoding.
|
||||
///
|
||||
/// Row-based entries use indices 0..2. The ordinary table uses the strategy
|
||||
/// index after the C caller subtracts 3 from the returned value.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn ZSTD_rust_params_selectBlockCompressor(strategy: c_int, mode: c_int) -> c_int {
|
||||
select_block_compressor(strategy, mode)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn resolve_row_match_finder(mode: c_int, cparams: ZSTD_compressionParameters) -> c_int {
|
||||
if mode != ZSTD_RUST_PS_AUTO {
|
||||
@@ -1307,6 +1326,64 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn block_compressor_selector_maps_all_strategy_table_indices() {
|
||||
for strategy in ZSTD_FAST..=ZSTD_BTULTRA2 {
|
||||
assert_eq!(
|
||||
ZSTD_rust_params_selectBlockCompressor(strategy, ZSTD_RUST_PS_DISABLE),
|
||||
3 + strategy
|
||||
);
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
ZSTD_rust_params_selectBlockCompressor(ZSTD_GREEDY, ZSTD_RUST_PS_ENABLE),
|
||||
0
|
||||
);
|
||||
assert_eq!(
|
||||
ZSTD_rust_params_selectBlockCompressor(ZSTD_LAZY, ZSTD_RUST_PS_ENABLE),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
ZSTD_rust_params_selectBlockCompressor(ZSTD_LAZY2, ZSTD_RUST_PS_ENABLE),
|
||||
2
|
||||
);
|
||||
|
||||
for strategy in [
|
||||
ZSTD_FAST,
|
||||
ZSTD_DFAST,
|
||||
ZSTD_BTLAZY2,
|
||||
ZSTD_BTOPT,
|
||||
ZSTD_BTULTRA,
|
||||
ZSTD_BTULTRA2,
|
||||
] {
|
||||
assert_eq!(
|
||||
ZSTD_rust_params_selectBlockCompressor(strategy, ZSTD_RUST_PS_ENABLE),
|
||||
3 + strategy
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn block_compressor_selector_rejects_strategy_below_bounds() {
|
||||
select_block_compressor(ZSTD_FAST - 1, ZSTD_RUST_PS_DISABLE);
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn block_compressor_selector_rejects_strategy_above_bounds() {
|
||||
select_block_compressor(ZSTD_BTULTRA2 + 1, ZSTD_RUST_PS_DISABLE);
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn block_compressor_selector_rejects_auto_row_mode() {
|
||||
select_block_compressor(ZSTD_GREEDY, ZSTD_RUST_PS_AUTO);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn block_splitter_and_ldm_policy_match_window_boundaries() {
|
||||
assert_eq!(
|
||||
|
||||
Reference in New Issue
Block a user