From fe3587c637ba0b7a2090a9061c36f24d366181ac Mon Sep 17 00:00:00 2001 From: ddidderr Date: Mon, 20 Jul 2026 13:57:15 +0200 Subject: [PATCH] refactor(ldm): expose parameter adjustment from Rust Move the pure LDM parameter-defaulting policy behind the original ZSTD_ldm_adjustParameters() ABI symbol. Rust now receives the existing C parameter structs directly, applies the same 30/8/8 build constants, and keeps the ASAN-sensitive table-size projection in C. Test Plan: - `cc -fsyntax-only -Ilib -Ilib/common -Ilib/compress -Ilib/decompress -Ilib/dict -Ilib/legacy lib/compress/zstd_ldm.c` -- passed. - `cargo +nightly fmt --manifest-path rust/Cargo.toml --all -- --check` -- passed. - `git diff --check` and `git diff --cached --check` -- passed. - Heavy verification intentionally deferred until the combined seam set is ready. --- lib/compress/zstd_ldm.c | 11 ----------- rust/src/zstd_ldm.rs | 33 +++++++++++++++++++-------------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/lib/compress/zstd_ldm.c b/lib/compress/zstd_ldm.c index aabf4f17d..c97512fc5 100644 --- a/lib/compress/zstd_ldm.c +++ b/lib/compress/zstd_ldm.c @@ -251,9 +251,6 @@ typedef char ZSTD_rust_ldm_rep_count[(ZSTD_REP_NUM == 3) ? 1 : -1]; typedef char ZSTD_rust_ldm_strategy_values_check[ (ZSTD_fast == 1 && ZSTD_dfast == 2 && ZSTD_btultra2 == 9) ? 1 : -1]; -void ZSTD_rust_ldm_adjustParameters( - void* params, U32 windowLog, int strategy, - U32 hashLogMax, U32 bucketSizeLogMax, int btultra); size_t ZSTD_rust_ldm_getTableSize( const void* params, int enableLdm, size_t redzoneSize); void ZSTD_rust_ldm_fillHashTable( @@ -336,14 +333,6 @@ void ZSTD_ldm_rust_setLdmSeqStore(void* blockContext, const void* rawSeqStore) context->ms->ldmSeqStore = (const RawSeqStore_t*)rawSeqStore; } -void ZSTD_ldm_adjustParameters(ldmParams_t* params, - const ZSTD_compressionParameters* cParams) -{ - ZSTD_rust_ldm_adjustParameters( - params, cParams->windowLog, (int)cParams->strategy, - ZSTD_HASHLOG_MAX, ZSTD_LDM_BUCKETSIZELOG_MAX, (int)ZSTD_btultra); -} - size_t ZSTD_ldm_getTableSize(ldmParams_t params) { #if ZSTD_ADDRESS_SANITIZER && !defined(ZSTD_ASAN_DONT_POISON_WORKSPACE) diff --git a/rust/src/zstd_ldm.rs b/rust/src/zstd_ldm.rs index 38abe12f6..0ec11914a 100644 --- a/rust/src/zstd_ldm.rs +++ b/rust/src/zstd_ldm.rs @@ -12,6 +12,7 @@ use crate::errors::{ERR_isError, ZstdErrorCode, ERROR}; use crate::mem::{MEM_64bits, MEM_isLittleEndian, MEM_read16, MEM_read32, MEM_readST}; use crate::xxhash::XXH64; +use crate::zstd_compress_params::ZSTD_compressionParameters; use crate::zstd_fast::store_seq_opaque; use std::ffi::c_void; use std::mem::size_of; @@ -651,17 +652,14 @@ unsafe fn generate_sequences_internal( unsafe { iend.offset_from(anchor) as usize } } -/// Rust implementation called by the C ABI wrapper for parameter adjustment. -#[no_mangle] -pub unsafe extern "C" fn ZSTD_rust_ldm_adjustParameters( - params: *mut c_void, +fn adjust_parameters( + params: &mut LdmParams, window_log: u32, strategy: c_int, hash_log_max: u32, bucket_size_log_max: u32, btultra: c_int, ) { - let params = unsafe { &mut *params.cast::() }; params.window_log = window_log; if params.hash_rate_log == 0 { if params.hash_log > 0 { @@ -691,6 +689,17 @@ pub unsafe extern "C" fn ZSTD_rust_ldm_adjustParameters( params.bucket_size_log = params.bucket_size_log.min(params.hash_log); } +/// Adjust LDM parameters using the original C ABI. +#[no_mangle] +pub unsafe extern "C" fn ZSTD_ldm_adjustParameters( + params: *mut LdmParams, + c_params: *const ZSTD_compressionParameters, +) { + let params = unsafe { &mut *params }; + let c_params = unsafe { &*c_params }; + adjust_parameters(params, c_params.windowLog, c_params.strategy, 30, 8, 8); +} + #[no_mangle] pub unsafe extern "C" fn ZSTD_rust_ldm_getTableSize( params: *const c_void, @@ -1069,16 +1078,12 @@ mod tests { hash_rate_log: 0, window_log: 0, }; - unsafe { - ZSTD_rust_ldm_adjustParameters( - (&mut params as *mut LdmParams).cast::(), - 20, - 3, - 30, - 8, - 8, - ) + let c_params = ZSTD_compressionParameters { + windowLog: 20, + strategy: 3, + ..Default::default() }; + unsafe { ZSTD_ldm_adjustParameters(&mut params, &c_params) }; assert_eq!(params.window_log, 20); assert_eq!(params.hash_rate_log, 6); assert_eq!(params.hash_log, 14);