From 7174d2ec7185357b06945ad3ca9a5036b7cf071c Mon Sep 17 00:00:00 2001 From: ddidderr Date: Mon, 20 Jul 2026 13:16:26 +0200 Subject: [PATCH] refactor(ldm): expose max sequence bound from Rust The LDM sequence-space helper was already implemented in Rust, but C still owned the public symbol and forwarded through a pointer-based Rust bridge. Make the Rust parameter representation public and ABI-compatible for the original by-value `ldmParams_t` call, then have Rust compare `enableLdm` directly with `ZSTD_ps_enable` before applying the sequence bound. Remove only the redundant C bridge declaration and wrapper; table sizing, state, and sequence generation remain unchanged. Test Plan: - `git diff --check` -- passed. - `git diff --cached --check` -- passed. - `cc -std=c99 -Ilib -Ilib/common -Ilib/compress -Ilib/decompress -Ilib/dict -fsyntax-only lib/compress/zstd_ldm.c` -- passed. - `cargo +nightly fmt --manifest-path rust/Cargo.toml --all -- --check` -- passed. - Cargo build/test, make, and other heavy checks were intentionally not run per the requested lightweight-only scope. --- lib/compress/zstd_ldm.c | 8 -------- rust/src/zstd_ldm.rs | 35 +++++++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/lib/compress/zstd_ldm.c b/lib/compress/zstd_ldm.c index 979c6d4f2..aabf4f17d 100644 --- a/lib/compress/zstd_ldm.c +++ b/lib/compress/zstd_ldm.c @@ -256,8 +256,6 @@ void ZSTD_rust_ldm_adjustParameters( U32 hashLogMax, U32 bucketSizeLogMax, int btultra); size_t ZSTD_rust_ldm_getTableSize( const void* params, int enableLdm, size_t redzoneSize); -size_t ZSTD_rust_ldm_getMaxNbSeq( - const void* params, int enableLdm, size_t maxChunkSize); void ZSTD_rust_ldm_fillHashTable( void* hashTable, BYTE* bucketOffsets, const BYTE* base, const BYTE* ip, const BYTE* iend, const void* params); @@ -357,12 +355,6 @@ size_t ZSTD_ldm_getTableSize(ldmParams_t params) ¶ms, params.enableLdm == ZSTD_ps_enable, redzoneSize); } -size_t ZSTD_ldm_getMaxNbSeq(ldmParams_t params, size_t maxChunkSize) -{ - return ZSTD_rust_ldm_getMaxNbSeq( - ¶ms, params.enableLdm == ZSTD_ps_enable, maxChunkSize); -} - void ZSTD_ldm_fillHashTable( ldmState_t* ldmState, const BYTE* ip, const BYTE* iend, ldmParams_t const* params) diff --git a/rust/src/zstd_ldm.rs b/rust/src/zstd_ldm.rs index 7b028d136..38abe12f6 100644 --- a/rust/src/zstd_ldm.rs +++ b/rust/src/zstd_ldm.rs @@ -27,6 +27,7 @@ const ZSTD_FAST_STRATEGY: c_int = 1; const ZSTD_DFAST_STRATEGY: c_int = 2; const ZSTD_STRATEGY_MAX: c_int = 9; const ZSTD_BTOPT: c_int = 7; +const ZSTD_PS_ENABLE: c_int = 1; const LDM_FAST_TABLES_NONE: c_int = 0; const LDM_FAST_TABLES_FAST: c_int = 1; @@ -58,7 +59,7 @@ struct RawSeqStore { } #[repr(C)] -struct LdmParams { +pub struct LdmParams { enable_ldm: c_int, hash_log: u32, bucket_size_log: u32, @@ -715,13 +716,8 @@ pub unsafe extern "C" fn ZSTD_rust_ldm_getTableSize( } #[no_mangle] -pub unsafe extern "C" fn ZSTD_rust_ldm_getMaxNbSeq( - params: *const c_void, - enable_ldm: c_int, - max_chunk_size: usize, -) -> usize { - let params = unsafe { &*params.cast::() }; - if enable_ldm != 0 { +pub extern "C" fn ZSTD_ldm_getMaxNbSeq(params: LdmParams, max_chunk_size: usize) -> usize { + if params.enable_ldm == ZSTD_PS_ENABLE { max_chunk_size / params.min_match_length as usize } else { 0 @@ -1090,6 +1086,29 @@ mod tests { assert_eq!(params.min_match_length, 64); } + #[test] + fn max_nb_seq_only_applies_when_ldm_is_enabled() { + let params = LdmParams { + enable_ldm: ZSTD_PS_ENABLE, + hash_log: 0, + bucket_size_log: 0, + min_match_length: 64, + hash_rate_log: 0, + window_log: 0, + }; + assert_eq!(ZSTD_ldm_getMaxNbSeq(params, 640), 10); + + let params = LdmParams { + enable_ldm: 0, + hash_log: 0, + bucket_size_log: 0, + min_match_length: 64, + hash_rate_log: 0, + window_log: 0, + }; + assert_eq!(ZSTD_ldm_getMaxNbSeq(params, 640), 0); + } + #[test] fn optimal_parser_starts_at_btopt_strategy() { assert!(!use_optimal_parser(ZSTD_BTOPT - 1));