refactor(compress): move CCtx parameter init policy to Rust
Move reset, copy, and default policy resolution from ZSTD_CCtxParams_init_internal into Rust through the existing repr(C) parameter mirror. C retains validation and diagnostics, while focused tests cover reset behavior, policy resolution, and compression-level handling. Test Plan: - capped nightly rustfmt check - capped root clippy with all targets and -D warnings - capped native make -j1 - capped upstream make -j1 -C tests test - capped CLI clippy and 185 CLI tests All integrated checks ran at the combined working-tree tip under a serial 40 GiB virtual-memory cap. Standalone root Rust unit linking remains unavailable because the crate imports C-owned bridge symbols.
This commit is contained in:
@@ -17,9 +17,10 @@ use crate::zstd_compress_params::{
|
||||
ZSTD_rust_params_checkCParams, ZSTD_rust_params_dedicatedDictSearchIsSupported,
|
||||
ZSTD_rust_params_dedicatedDictSearch_getCParams, ZSTD_rust_params_getBounds,
|
||||
ZSTD_rust_params_getCParamsFromCCtxParams, ZSTD_rust_params_getCParamsInternal,
|
||||
ZSTD_rust_params_overrideCParams, ZSTD_rust_params_resolveMaxBlockSize,
|
||||
ZSTD_rust_params_overrideCParams, ZSTD_rust_params_resolveExternalRepcodeSearch,
|
||||
ZSTD_rust_params_resolveExternalSequenceValidation, ZSTD_rust_params_resolveMaxBlockSize,
|
||||
ZSTD_rust_params_resolveRowMatchFinderMode, ZSTD_CONTENTSIZE_UNKNOWN,
|
||||
ZSTD_RUST_CPM_CREATE_CDICT, ZSTD_RUST_PS_DISABLE, ZSTD_RUST_PS_ENABLE,
|
||||
ZSTD_RUST_CPM_CREATE_CDICT, ZSTD_RUST_PS_AUTO, ZSTD_RUST_PS_DISABLE, ZSTD_RUST_PS_ENABLE,
|
||||
};
|
||||
use std::mem::size_of;
|
||||
use std::os::raw::{c_int, c_void};
|
||||
@@ -426,6 +427,41 @@ unsafe fn init_impl(params: *mut ZSTD_CCtx_params, compression_level: c_int) ->
|
||||
0
|
||||
}
|
||||
|
||||
unsafe fn init_internal_impl(
|
||||
cctx_params: *mut ZSTD_CCtx_params,
|
||||
zstd_params: *const ZSTD_parameters,
|
||||
compression_level: c_int,
|
||||
) {
|
||||
unsafe {
|
||||
ptr::write_bytes(cctx_params.cast::<u8>(), 0, size_of::<ZSTD_CCtx_params>());
|
||||
(*cctx_params).cParams = (*zstd_params).cParams;
|
||||
(*cctx_params).fParams = (*zstd_params).fParams;
|
||||
/* Keep the level for tracing even when params came from a zstd-params object. */
|
||||
(*cctx_params).compressionLevel = compression_level;
|
||||
(*cctx_params).useRowMatchFinder = resolve_row_match_finder((*zstd_params).cParams);
|
||||
(*cctx_params).postBlockSplitter = resolve_block_splitter((*zstd_params).cParams);
|
||||
(*cctx_params).ldmParams.enableLdm = resolve_ldm((*zstd_params).cParams);
|
||||
(*cctx_params).validateSequences =
|
||||
ZSTD_rust_params_resolveExternalSequenceValidation(ZSTD_RUST_PS_AUTO);
|
||||
(*cctx_params).maxBlockSize = ZSTD_rust_params_resolveMaxBlockSize(0);
|
||||
(*cctx_params).searchForExternalRepcodes =
|
||||
ZSTD_rust_params_resolveExternalRepcodeSearch(ZSTD_RUST_PS_AUTO, compression_level);
|
||||
}
|
||||
}
|
||||
|
||||
/// Initializes the internal parameter object from validated zstd parameters.
|
||||
///
|
||||
/// The C adapter retains validation assertions and diagnostic logging; this
|
||||
/// function owns only the narrow reset, copy, and default-resolution policy.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn ZSTD_rust_CCtxParams_init_internal(
|
||||
cctx_params: *mut ZSTD_CCtx_params,
|
||||
zstd_params: *const ZSTD_parameters,
|
||||
compression_level: c_int,
|
||||
) {
|
||||
unsafe { init_internal_impl(cctx_params, zstd_params, compression_level) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn resolve_row_match_finder(cparams: ZSTD_compressionParameters) -> c_int {
|
||||
if (STRATEGY_GREEDY..=STRATEGY_LAZY2).contains(&cparams.strategy) && cparams.windowLog > 14 {
|
||||
@@ -1483,6 +1519,51 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn init_internal_resets_and_resolves_policy_without_changing_inputs() {
|
||||
let zstd_params = ZSTD_parameters {
|
||||
cParams: ZSTD_compressionParameters {
|
||||
windowLog: 27,
|
||||
chainLog: 27,
|
||||
hashLog: 25,
|
||||
searchLog: 9,
|
||||
minMatch: 4,
|
||||
targetLength: 32,
|
||||
strategy: STRATEGY_BTOPT,
|
||||
},
|
||||
fParams: ZSTD_frameParameters {
|
||||
contentSizeFlag: 1,
|
||||
checksumFlag: 1,
|
||||
noDictIDFlag: 0,
|
||||
},
|
||||
};
|
||||
|
||||
for (compression_level, expected_repcode_search) in
|
||||
[(9, ZSTD_RUST_PS_DISABLE), (10, ZSTD_RUST_PS_ENABLE)]
|
||||
{
|
||||
let mut storage = MaybeUninit::<ZSTD_CCtx_params>::zeroed();
|
||||
let params = storage.as_mut_ptr();
|
||||
unsafe {
|
||||
(*params).format = 1;
|
||||
(*params).customMem.opaque = ptr::dangling_mut::<c_void>();
|
||||
|
||||
ZSTD_rust_CCtxParams_init_internal(params, &zstd_params, compression_level);
|
||||
|
||||
assert_eq!((*params).cParams, zstd_params.cParams);
|
||||
assert_eq!((*params).fParams, zstd_params.fParams);
|
||||
assert_eq!((*params).compressionLevel, compression_level);
|
||||
assert_eq!((*params).useRowMatchFinder, ZSTD_RUST_PS_DISABLE);
|
||||
assert_eq!((*params).postBlockSplitter, ZSTD_RUST_PS_ENABLE);
|
||||
assert_eq!((*params).ldmParams.enableLdm, ZSTD_RUST_PS_ENABLE);
|
||||
assert_eq!((*params).validateSequences, 0);
|
||||
assert_eq!((*params).maxBlockSize, BLOCKSIZE_MAX);
|
||||
assert_eq!((*params).searchForExternalRepcodes, expected_repcode_search);
|
||||
assert_eq!((*params).format, 0);
|
||||
assert!((*params).customMem.opaque.is_null());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn advanced_init_resolves_policy_and_resets_internal_fields() {
|
||||
let cases = [
|
||||
|
||||
Reference in New Issue
Block a user