feat(compress): move dedicated dict param reversion to Rust
Port ZSTD_dedicatedDictSearch_revertCParams to a Rust leaf that accepts and returns the complete repr(C) compression-parameter struct. Keep the existing C pointer wrapper and call order before ZSTD_adjustCParams_internal, while changing only hashLog for greedy, lazy, and lazy2 strategies. Test Plan: - cargo test --manifest-path rust/Cargo.toml --no-default-features --features compression - cargo clippy --manifest-path rust/Cargo.toml --no-default-features --features compression (and --benches/--tests, rerun after fmt) - cargo +nightly fmt --manifest-path rust/Cargo.toml - make -B -C lib -j2 lib - make -B -C tests -j2 fuzzer - tests/fuzzer -s4560 -t47 -i48 -v - tests/fuzzer -s4560 -t57 -i1057 -v - make -B -C tests -j2 test-zstream - git diff --check
This commit is contained in:
@@ -132,7 +132,8 @@ int ZSTD_rust_params_resolveExternalRepcodeSearch(int mode, int cLevel);
|
|||||||
int ZSTD_rust_params_cdictIndicesAreTagged(ZSTD_compressionParameters cParams);
|
int ZSTD_rust_params_cdictIndicesAreTagged(ZSTD_compressionParameters cParams);
|
||||||
int ZSTD_rust_params_dedicatedDictSearchIsSupported(ZSTD_compressionParameters cParams);
|
int ZSTD_rust_params_dedicatedDictSearchIsSupported(ZSTD_compressionParameters cParams);
|
||||||
U32 ZSTD_rust_params_dedicatedDictSearch_getHashLog(U32 hashLog);
|
U32 ZSTD_rust_params_dedicatedDictSearch_getHashLog(U32 hashLog);
|
||||||
U32 ZSTD_rust_params_dedicatedDictSearch_revertHashLog(U32 hashLog);
|
ZSTD_compressionParameters
|
||||||
|
ZSTD_rust_params_dedicatedDictSearch_revertCParams(ZSTD_compressionParameters cParams);
|
||||||
int ZSTD_rust_params_shouldAttachDict(int strategy, int dedicatedDictSearch,
|
int ZSTD_rust_params_shouldAttachDict(int strategy, int dedicatedDictSearch,
|
||||||
U64 pledgedSrcSize, int attachDictPref,
|
U64 pledgedSrcSize, int attachDictPref,
|
||||||
int forceWindow);
|
int forceWindow);
|
||||||
@@ -5678,21 +5679,7 @@ static ZSTD_compressionParameters ZSTD_dedicatedDictSearch_getCParams(int const
|
|||||||
*/
|
*/
|
||||||
static void ZSTD_dedicatedDictSearch_revertCParams(
|
static void ZSTD_dedicatedDictSearch_revertCParams(
|
||||||
ZSTD_compressionParameters* cParams) {
|
ZSTD_compressionParameters* cParams) {
|
||||||
switch (cParams->strategy) {
|
*cParams = ZSTD_rust_params_dedicatedDictSearch_revertCParams(*cParams);
|
||||||
case ZSTD_fast:
|
|
||||||
case ZSTD_dfast:
|
|
||||||
break;
|
|
||||||
case ZSTD_greedy:
|
|
||||||
case ZSTD_lazy:
|
|
||||||
case ZSTD_lazy2:
|
|
||||||
cParams->hashLog = ZSTD_rust_params_dedicatedDictSearch_revertHashLog(cParams->hashLog);
|
|
||||||
break;
|
|
||||||
case ZSTD_btlazy2:
|
|
||||||
case ZSTD_btopt:
|
|
||||||
case ZSTD_btultra:
|
|
||||||
case ZSTD_btultra2:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! ZSTD_getCParams_internal() :
|
/*! ZSTD_getCParams_internal() :
|
||||||
|
|||||||
@@ -455,6 +455,19 @@ fn dedicated_dict_search_revert_hash_log(hash_log: u32) -> u32 {
|
|||||||
.max(ZSTD_HASHLOG_MIN as u32)
|
.max(ZSTD_HASHLOG_MIN as u32)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn dedicated_dict_search_revert_cparams(
|
||||||
|
mut cparams: ZSTD_compressionParameters,
|
||||||
|
) -> ZSTD_compressionParameters {
|
||||||
|
match cparams.strategy {
|
||||||
|
ZSTD_GREEDY | ZSTD_LAZY | ZSTD_LAZY2 => {
|
||||||
|
cparams.hashLog = dedicated_dict_search_revert_hash_log(cparams.hashLog);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
cparams
|
||||||
|
}
|
||||||
|
|
||||||
const ATTACH_DICT_SIZE_CUTOFFS: [u64; 10] = [
|
const ATTACH_DICT_SIZE_CUTOFFS: [u64; 10] = [
|
||||||
8 * 1024, /* unused */
|
8 * 1024, /* unused */
|
||||||
8 * 1024, /* ZSTD_fast */
|
8 * 1024, /* ZSTD_fast */
|
||||||
@@ -531,10 +544,12 @@ pub extern "C" fn ZSTD_rust_params_dedicatedDictSearch_getHashLog(hash_log: u32)
|
|||||||
dedicated_dict_search_get_hash_log(hash_log)
|
dedicated_dict_search_get_hash_log(hash_log)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// C ABI for the hash-log adjustment used by `ZSTD_dedicatedDictSearch_revertCParams()`.
|
/// C ABI for `ZSTD_dedicatedDictSearch_revertCParams()`.
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn ZSTD_rust_params_dedicatedDictSearch_revertHashLog(hash_log: u32) -> u32 {
|
pub extern "C" fn ZSTD_rust_params_dedicatedDictSearch_revertCParams(
|
||||||
dedicated_dict_search_revert_hash_log(hash_log)
|
cparams: ZSTD_compressionParameters,
|
||||||
|
) -> ZSTD_compressionParameters {
|
||||||
|
dedicated_dict_search_revert_cparams(cparams)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// C ABI for `ZSTD_shouldAttachDict()`.
|
/// C ABI for `ZSTD_shouldAttachDict()`.
|
||||||
@@ -1403,7 +1418,7 @@ mod tests {
|
|||||||
assert_eq!(adjusted, if should_adjust { 14 } else { original });
|
assert_eq!(adjusted, if should_adjust { 14 } else { original });
|
||||||
|
|
||||||
let reverted = if should_adjust {
|
let reverted = if should_adjust {
|
||||||
ZSTD_rust_params_dedicatedDictSearch_revertHashLog(adjusted)
|
dedicated_dict_search_revert_hash_log(adjusted)
|
||||||
} else {
|
} else {
|
||||||
adjusted
|
adjusted
|
||||||
};
|
};
|
||||||
@@ -1412,26 +1427,87 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(ZSTD_rust_params_dedicatedDictSearch_getHashLog(u32::MAX), 1);
|
assert_eq!(ZSTD_rust_params_dedicatedDictSearch_getHashLog(u32::MAX), 1);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ZSTD_rust_params_dedicatedDictSearch_revertHashLog(ZSTD_HASHLOG_MIN as u32),
|
dedicated_dict_search_revert_hash_log(ZSTD_HASHLOG_MIN as u32),
|
||||||
ZSTD_HASHLOG_MIN as u32
|
ZSTD_HASHLOG_MIN as u32
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ZSTD_rust_params_dedicatedDictSearch_revertHashLog(ZSTD_HASHLOG_MIN as u32 + 1),
|
dedicated_dict_search_revert_hash_log(ZSTD_HASHLOG_MIN as u32 + 1),
|
||||||
ZSTD_HASHLOG_MIN as u32
|
ZSTD_HASHLOG_MIN as u32
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ZSTD_rust_params_dedicatedDictSearch_revertHashLog(ZSTD_HASHLOG_MIN as u32 + 2),
|
dedicated_dict_search_revert_hash_log(ZSTD_HASHLOG_MIN as u32 + 2),
|
||||||
ZSTD_HASHLOG_MIN as u32
|
ZSTD_HASHLOG_MIN as u32
|
||||||
);
|
);
|
||||||
|
assert_eq!(dedicated_dict_search_revert_hash_log(0), u32::MAX - 1);
|
||||||
|
assert_eq!(dedicated_dict_search_revert_hash_log(1), u32::MAX);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn dedicated_dict_search_revert_cparams_preserves_fields_and_strategy_policy() {
|
||||||
|
let strategies = [
|
||||||
|
ZSTD_FAST,
|
||||||
|
ZSTD_DFAST,
|
||||||
|
ZSTD_GREEDY,
|
||||||
|
ZSTD_LAZY,
|
||||||
|
ZSTD_LAZY2,
|
||||||
|
ZSTD_BTLAZY2,
|
||||||
|
ZSTD_BTOPT,
|
||||||
|
ZSTD_BTULTRA,
|
||||||
|
ZSTD_BTULTRA2,
|
||||||
|
-1,
|
||||||
|
99,
|
||||||
|
];
|
||||||
|
|
||||||
|
for strategy in strategies {
|
||||||
|
let input = ZSTD_compressionParameters {
|
||||||
|
windowLog: 21,
|
||||||
|
chainLog: 19,
|
||||||
|
hashLog: 12,
|
||||||
|
searchLog: 5,
|
||||||
|
minMatch: 4,
|
||||||
|
targetLength: 48,
|
||||||
|
strategy,
|
||||||
|
};
|
||||||
|
let output = dedicated_dict_search_revert_cparams(input);
|
||||||
|
let expected_hash_log = if matches!(strategy, ZSTD_GREEDY | ZSTD_LAZY | ZSTD_LAZY2) {
|
||||||
|
10
|
||||||
|
} else {
|
||||||
|
input.hashLog
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(output.hashLog, expected_hash_log, "strategy {strategy}");
|
||||||
|
assert_eq!(output.windowLog, input.windowLog, "strategy {strategy}");
|
||||||
|
assert_eq!(output.chainLog, input.chainLog, "strategy {strategy}");
|
||||||
|
assert_eq!(output.searchLog, input.searchLog, "strategy {strategy}");
|
||||||
|
assert_eq!(output.minMatch, input.minMatch, "strategy {strategy}");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ZSTD_rust_params_dedicatedDictSearch_revertHashLog(0),
|
output.targetLength, input.targetLength,
|
||||||
|
"strategy {strategy}"
|
||||||
|
);
|
||||||
|
assert_eq!(output.strategy, input.strategy, "strategy {strategy}");
|
||||||
|
}
|
||||||
|
|
||||||
|
for strategy in [ZSTD_GREEDY, ZSTD_LAZY, ZSTD_LAZY2] {
|
||||||
|
let mut input = ZSTD_compressionParameters {
|
||||||
|
hashLog: ZSTD_HASHLOG_MIN as u32,
|
||||||
|
strategy,
|
||||||
|
..ZSTD_compressionParameters::default()
|
||||||
|
};
|
||||||
|
assert_eq!(dedicated_dict_search_revert_cparams(input).hashLog, 6);
|
||||||
|
|
||||||
|
input.hashLog = 0;
|
||||||
|
assert_eq!(
|
||||||
|
dedicated_dict_search_revert_cparams(input).hashLog,
|
||||||
u32::MAX - 1
|
u32::MAX - 1
|
||||||
);
|
);
|
||||||
|
|
||||||
|
input.hashLog = 1;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ZSTD_rust_params_dedicatedDictSearch_revertHashLog(1),
|
dedicated_dict_search_revert_cparams(input).hashLog,
|
||||||
u32::MAX
|
u32::MAX
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn dictionary_attachment_matches_strategy_size_and_preference_boundaries() {
|
fn dictionary_attachment_matches_strategy_size_and_preference_boundaries() {
|
||||||
|
|||||||
Reference in New Issue
Block a user