zrip-encode 0.9.0

zstd encoder for zrip (internal crate)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#![forbid(unsafe_code)]

/// Match-finding strategy.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Strategy {
    /// Single hash table (levels -8 through 2).
    Fast,
    /// Short + long hash tables (levels 3-4).
    DFast,
}

/// Parameters for Long Distance Matching.
#[derive(Debug, Clone, Copy)]
pub struct LdmParams {
    pub hash_log: u32,
    pub bucket_size_log: u32,
    pub min_match_length: u32,
    pub hash_rate_log: u32,
}

impl LdmParams {
    pub fn default_for_window_log(window_log: u32) -> Self {
        let hash_log = 20u32.min(window_log.saturating_sub(1));
        let hash_rate_log = window_log.saturating_sub(hash_log).max(7);
        Self {
            hash_log,
            bucket_size_log: 4,
            min_match_length: 64,
            hash_rate_log,
        }
    }
}

/// Compression parameters for a specific level.
///
/// Obtain via [`level_params`] or construct directly for custom tuning.
/// Pass to [`compress_with_params`](crate::compress_with_params).
#[derive(Debug, Clone, Copy)]
pub struct LevelParams {
    pub strategy: Strategy,
    pub window_log: u32,
    pub hash_log: u32,
    /// DFast short table log. Same as hashLog for Fast strategy.
    pub chain_log: u32,
    pub search_log: u32,
    pub min_match: u32,
    pub target_length: u32,
    pub search_strength: u32,
    pub force_raw_literals: bool,
    #[cfg(feature = "ldm")]
    pub ldm_params: Option<LdmParams>,
}

impl LevelParams {
    #[must_use]
    pub fn with_window_log(mut self, window_log: u32) -> Self {
        self.window_log = window_log;
        self
    }

    #[cfg(feature = "ldm")]
    #[must_use]
    pub fn with_ldm(mut self, params: LdmParams) -> Self {
        self.ldm_params = Some(params);
        self
    }
}

/// Default compression level used when level 0 is requested.
pub const DEFAULT_LEVEL: i32 = 1;

/// Returns the compression parameters for a given level, or `None` if out of range.
///
/// Level 0 is treated as "library default" and maps to level 1.
/// Uses the large-input (>256 KB) parameter tier.
pub fn level_params(level: i32) -> Option<LevelParams> {
    level_params_for_size(level, usize::MAX)
}

/// Returns the compression parameters for a given level, sized for `src_len`.
///
/// Uses fixed parameters per level with log values clamped down for small inputs.
///
/// Level 0 is treated as "library default" and maps to level 1.
pub fn level_params_for_size(level: i32, src_len: usize) -> Option<LevelParams> {
    let mut params = level_params_inner(level)?;
    params.hash_log = params.hash_log.clamp(HASH_LOG_MIN, HASH_LOG_MAX);
    params.chain_log = params.chain_log.clamp(HASH_LOG_MIN, HASH_LOG_MAX);
    params.window_log = params.window_log.clamp(WINDOW_LOG_MIN, WINDOW_LOG_MAX);
    if (2..usize::MAX).contains(&src_len) {
        let src_log = 32 - ((src_len as u32) - 1).leading_zeros();
        params.hash_log = params.hash_log.min(src_log).max(HASH_LOG_MIN);
        params.chain_log = params.chain_log.min(src_log).max(HASH_LOG_MIN);
        params.window_log = params.window_log.min(src_log);
    }
    if raw_literals_limit(level).is_some_and(|max| src_len <= max) {
        params.force_raw_literals = true;
        if let Some((target_length, min_match, search_strength)) = small_input_search(level) {
            params.target_length = target_length;
            params.min_match = min_match;
            params.search_strength = search_strength;
        }
    }
    if level == 3 && (32 * 1024..=128 * 1024).contains(&src_len) {
        params.search_strength = 7;
    }
    Some(params)
}

pub const HASH_LOG_MIN: u32 = 6;
pub const HASH_LOG_MAX: u32 = 30;
pub const WINDOW_LOG_MIN: u32 = 10;
pub const WINDOW_LOG_MAX: u32 = 27;

/// Largest input a negative level encodes with raw literals.
///
/// Building a Huffman table costs a few microseconds regardless of input
/// size. For tiny messages that dominates the encode time, so the negative
/// levels skip it: 2 KiB at L-1, growing 1.5x or 1.33x per level (the size
/// doubles every two levels) to 24 KiB at L-8. `None` for levels that always
/// try Huffman literals.
pub(crate) fn raw_literals_limit(level: i32) -> Option<usize> {
    if !(-8..=-1).contains(&level) {
        return None;
    }
    let rank = (-level - 1) as usize;
    let base = 2048usize << (rank / 2);
    Some(if rank % 2 == 1 { base + base / 2 } else { base })
}

/// Match search for inputs that keep raw literals, as `(target_length,
/// min_match, search_strength)`.
///
/// Without Huffman literals, matches are the only compression, so the
/// negative levels search more densely on these inputs than on large ones.
/// L-8 is a slightly sparser L-7. L-4 shares its step with L-3 and L-2 but
/// grows it faster through regions without matches.
fn small_input_search(level: i32) -> Option<(u32, u32, u32)> {
    Some(match level {
        -8 => (8, 5, 5),
        -7 => (5, 5, 6),
        -6 => (4, 5, 6),
        -5 => (3, 5, 6),
        -4 => (2, 5, 6),
        -3 | -2 => (2, 5, 7),
        -1 => (1, 5, 7),
        _ => return None,
    })
}

/// Whether an input of `input_len` bytes keeps raw literals at these
/// parameters.
pub(crate) fn keeps_raw_literals(params: &LevelParams, _input_len: usize) -> bool {
    params.force_raw_literals
}

/// Per-block encoding choices derived from the level parameters.
#[derive(Clone, Copy, Debug)]
pub(crate) struct BlockPolicy {
    /// Try Huffman literals and custom sequence tables.
    pub custom_tables: bool,
    /// Literals whose sampled order-0 entropy exceeds this many bits per byte
    /// (8.8 fixed point) stay raw. `u32::MAX` disables the check.
    pub max_literal_entropy_fp8: u32,
}

/// Encoding choices for a block of an input of `input_len` bytes.
///
/// The fast negative levels skip Huffman coding for dense literals: on
/// low-compressibility data it costs more time than it saves space. L-8 to
/// L-1 keep literals above 6.25 bits per byte raw. Small inputs keep all
/// literals raw (see `raw_literals_limit`).
pub(crate) fn block_policy(params: &LevelParams, input_len: usize) -> BlockPolicy {
    let max_literal_entropy_fp8 = if params.strategy == Strategy::Fast && params.min_match >= 5 {
        match params.target_length {
            0 | 1 => u32::MAX,
            _ => 6 * 256 + 64,
        }
    } else {
        u32::MAX
    };
    BlockPolicy {
        custom_tables: use_custom_sequence_tables(params, input_len),
        max_literal_entropy_fp8,
    }
}

fn use_custom_sequence_tables(params: &LevelParams, input_len: usize) -> bool {
    // Raw-literal inputs also skip custom sequence tables.
    if keeps_raw_literals(params, input_len) {
        return false;
    }

    if (32768..=zrip_core::frame::MAX_BLOCK_SIZE).contains(&input_len)
        && params.strategy == Strategy::DFast
        && params.min_match == 4
        && params.target_length == 1
        && params.search_strength < 5
    {
        return false;
    }
    true
}

/// Returns the maximum hash_log for a given level.
/// Used by CompressContext to pre-allocate hash tables.
pub fn max_hash_log(level: i32) -> Option<u32> {
    let p = level_params_inner(level)?;
    Some(p.hash_log.max(p.chain_log))
}

fn level_params_inner(level: i32) -> Option<LevelParams> {
    Some(match level {
        0 => return level_params_inner(DEFAULT_LEVEL),
        // zrip's own level: L-7 with a larger step and faster acceleration
        // through regions without matches.
        -8 => LevelParams {
            strategy: Strategy::Fast,
            window_log: 19,
            hash_log: 13,
            chain_log: 13,
            search_log: 0,
            min_match: 6,
            target_length: 21,
            search_strength: 5,
            force_raw_literals: false,
            #[cfg(feature = "ldm")]
            ldm_params: None,
        },
        -7 => LevelParams {
            strategy: Strategy::Fast,
            window_log: 19,
            hash_log: 13,
            chain_log: 13,
            search_log: 0,
            min_match: 6,
            target_length: 17,
            search_strength: 7,
            force_raw_literals: false,
            #[cfg(feature = "ldm")]
            ldm_params: None,
        },
        -6 => LevelParams {
            strategy: Strategy::Fast,
            window_log: 19,
            hash_log: 13,
            chain_log: 13,
            search_log: 0,
            min_match: 6,
            target_length: 13,
            search_strength: 7,
            force_raw_literals: false,
            #[cfg(feature = "ldm")]
            ldm_params: None,
        },
        -5 => LevelParams {
            strategy: Strategy::Fast,
            window_log: 19,
            hash_log: 13,
            chain_log: 13,
            search_log: 0,
            min_match: 6,
            target_length: 9,
            search_strength: 7,
            force_raw_literals: false,
            #[cfg(feature = "ldm")]
            ldm_params: None,
        },
        -4 => LevelParams {
            strategy: Strategy::Fast,
            window_log: 19,
            hash_log: 13,
            chain_log: 13,
            search_log: 0,
            min_match: 6,
            target_length: 5,
            search_strength: 7,
            force_raw_literals: false,
            #[cfg(feature = "ldm")]
            ldm_params: None,
        },
        -3 => LevelParams {
            strategy: Strategy::Fast,
            window_log: 19,
            hash_log: 14,
            chain_log: 14,
            search_log: 0,
            min_match: 6,
            target_length: 4,
            search_strength: 7,
            force_raw_literals: false,
            #[cfg(feature = "ldm")]
            ldm_params: None,
        },
        -2 => LevelParams {
            strategy: Strategy::Fast,
            window_log: 19,
            hash_log: 14,
            chain_log: 14,
            search_log: 0,
            min_match: 6,
            target_length: 3,
            search_strength: 7,
            force_raw_literals: false,
            #[cfg(feature = "ldm")]
            ldm_params: None,
        },
        -1 => LevelParams {
            strategy: Strategy::Fast,
            window_log: 19,
            hash_log: 14,
            chain_log: 14,
            search_log: 0,
            min_match: 6,
            target_length: 2,
            search_strength: 7,
            force_raw_literals: false,
            #[cfg(feature = "ldm")]
            ldm_params: None,
        },
        1 => LevelParams {
            strategy: Strategy::Fast,
            window_log: 19,
            hash_log: 15,
            chain_log: 15,
            search_log: 0,
            min_match: 6,
            target_length: 1,
            search_strength: 7,
            force_raw_literals: false,
            #[cfg(feature = "ldm")]
            ldm_params: None,
        },
        2 => LevelParams {
            strategy: Strategy::Fast,
            window_log: 20,
            hash_log: 16,
            chain_log: 16,
            search_log: 0,
            min_match: 6,
            target_length: 1,
            search_strength: 7,
            force_raw_literals: false,
            #[cfg(feature = "ldm")]
            ldm_params: None,
        },
        3 => LevelParams {
            strategy: Strategy::DFast,
            window_log: 21,
            hash_log: 18,
            chain_log: 18,
            search_log: 1,
            min_match: 4,
            target_length: 1,
            search_strength: 5,
            force_raw_literals: false,
            #[cfg(feature = "ldm")]
            ldm_params: None,
        },
        4 => LevelParams {
            strategy: Strategy::DFast,
            window_log: 24,
            hash_log: 20,
            chain_log: 20,
            search_log: 0,
            min_match: 4,
            target_length: 1,
            search_strength: 8,
            force_raw_literals: false,
            #[cfg(feature = "ldm")]
            ldm_params: None,
        },
        _ => return None,
    })
}

/// Options for large-window and LDM compression, orthogonal to level.
///
/// Pass to [`compress_opts`](crate::compress_opts) or
/// [`FrameEncoder::with_options`](crate::streaming::FrameEncoder::with_options).
#[derive(Debug, Clone, Default)]
pub struct Options {
    pub(crate) window_log: Option<u32>,
    #[cfg_attr(not(feature = "ldm"), allow(dead_code))]
    pub(crate) ldm: bool,
}

impl Options {
    #[must_use]
    pub fn window_log(mut self, log: u32) -> Self {
        self.window_log = Some(log);
        self
    }

    #[cfg(feature = "ldm")]
    #[must_use]
    pub fn ldm(mut self, enable: bool) -> Self {
        self.ldm = enable;
        self
    }
}

pub fn apply_options(params: &mut LevelParams, opts: &Options) {
    if let Some(wl) = opts.window_log {
        params.window_log = wl.clamp(WINDOW_LOG_MIN, WINDOW_LOG_MAX);
    }
    #[cfg(feature = "ldm")]
    if opts.ldm {
        params.ldm_params = Some(LdmParams::default_for_window_log(params.window_log));
    }
}