zrip-encode 0.9.8

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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#![forbid(unsafe_code)]

/// Match-finding strategy.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Strategy {
    /// Single hash table (levels -7 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.
/// L3 and L4 keep denser searches through 256 KiB to preserve matches on short 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)?;
    if let Some(search_strength) = short_input_search_strength(level, src_len) {
        params.search_strength = search_strength;
    }
    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 = src_log(src_len);
        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;
        }
    }
    Some(params)
}

/// Returns the bits that address `src_len` bytes: its log2, rounded up.
pub(crate) fn src_log(src_len: usize) -> u32 {
    usize::BITS - src_len.saturating_sub(1).leading_zeros()
}

/// Returns the compression parameters for `input_len` bytes after a
/// `prefix_len`-byte dictionary. The tables and window cover both, while the
/// search density follows the input alone, as in a frame without a dictionary.
pub(crate) fn level_params_with_prefix(
    level: i32,
    prefix_len: usize,
    input_len: usize,
) -> Option<LevelParams> {
    let mut params = level_params_for_size(level, prefix_len.saturating_add(input_len))?;
    if let Some(search_strength) = short_input_search_strength(level, input_len) {
        params.search_strength = search_strength;
    }
    Some(params)
}

/// Search strength for inputs of up to two blocks at the levels that search
/// sparsely on larger ones. Sparse initial tables cost too much ratio there.
fn short_input_search_strength(level: i32, input_len: usize) -> Option<u32> {
    if input_len > 256 * 1024 {
        return None;
    }
    match level {
        3 => Some(4),
        4 => Some(6),
        _ => None,
    }
}

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;
#[cfg(feature = "ldm")]
const LDM_DEFAULT_WINDOW_LOG: 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 12 KiB at L-6. `None` for L-7, which keeps
/// literals raw at every size, and for levels that always try Huffman
/// literals.
pub(crate) fn raw_literals_limit(level: i32) -> Option<usize> {
    if !(-6..=-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-7 keeps its own search, which always runs on raw literals. 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 {
        -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-6 to
/// L-1 keep literals above 6.25 bits per byte raw. Small inputs keep all
/// literals raw (see `raw_literals_limit`), and L-7 keeps them raw always.
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.
    !keeps_raw_literals(params, input_len)
}

/// 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),
        // The fastest level, for LZ4's niche: raw literals, as in C zstd,
        // and only matches of eight bytes or more (`min_match` 8 hashes and
        // confirms eight bytes), so frames decode with few sequences. Blocks
        // that are almost all literals try Huffman literals instead, so
        // low-compressibility data still shrinks a little. It steps 3 bytes
        // and grows the step every 16 bytes without a match. L-8, a separate
        // level before, runs as L-7.
        -8 | -7 => LevelParams {
            strategy: Strategy::Fast,
            window_log: 19,
            hash_log: 13,
            chain_log: 13,
            search_log: 0,
            min_match: 8,
            target_length: 2,
            search_strength: 4,
            force_raw_literals: true,
            #[cfg(feature = "ldm")]
            ldm_params: None,
        },
        // L-6 to L-4 hash and confirm 8 bytes. With Huffman literals, longer
        // matches at short steps beat shorter matches at long steps on both
        // ratio and speed, as fewer bytes are left to code as literals and
        // frames decode with fewer sequences. The step grows every 8 or 16
        // bytes without a match, so sparse regions cost few probes.
        -6 => LevelParams {
            strategy: Strategy::Fast,
            window_log: 19,
            hash_log: 13,
            chain_log: 13,
            search_log: 0,
            min_match: 8,
            target_length: 5,
            search_strength: 3,
            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: 8,
            target_length: 4,
            search_strength: 4,
            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: 8,
            target_length: 2,
            search_strength: 4,
            force_raw_literals: false,
            #[cfg(feature = "ldm")]
            ldm_params: None,
        },
        // L-3 through L-1 hash seven bytes. L-3 and L-2 grow the step every
        // 16 bytes without a match, L-1 every 32, so frequent matches keep
        // the initial step while long literal runs need few probes.
        -3 => LevelParams {
            strategy: Strategy::Fast,
            window_log: 19,
            hash_log: 14,
            chain_log: 14,
            search_log: 0,
            min_match: 7,
            target_length: 3,
            search_strength: 4,
            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: 7,
            target_length: 2,
            search_strength: 4,
            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: 7,
            target_length: 2,
            search_strength: 5,
            force_raw_literals: false,
            #[cfg(feature = "ldm")]
            ldm_params: None,
        },
        // L1 and L2 hash seven bytes to favor longer matches and grow the
        // step every 16 bytes without a match. Dense matches keep the
        // initial step, while long literal runs need fewer probes. L1 hashes
        // into a table of C zstd's size, L2 into half of C's, which trades
        // a little ratio for speed.
        1 => LevelParams {
            strategy: Strategy::Fast,
            window_log: 19,
            hash_log: 14,
            chain_log: 14,
            search_log: 0,
            min_match: 7,
            target_length: 1,
            search_strength: 4,
            force_raw_literals: false,
            #[cfg(feature = "ldm")]
            ldm_params: None,
        },
        2 => LevelParams {
            strategy: Strategy::Fast,
            window_log: 20,
            hash_log: 15,
            chain_log: 15,
            search_log: 0,
            min_match: 7,
            target_length: 1,
            search_strength: 4,
            force_raw_literals: false,
            #[cfg(feature = "ldm")]
            ldm_params: None,
        },
        // C zstd's level 3 tables. On large inputs, grow the step after each
        // unsuccessful probe so long literal runs do not dominate the search.
        3 => LevelParams {
            strategy: Strategy::DFast,
            window_log: 21,
            hash_log: 17,
            chain_log: 16,
            search_log: 1,
            min_match: 5,
            target_length: 1,
            search_strength: 0,
            force_raw_literals: false,
            #[cfg(feature = "ldm")]
            ldm_params: None,
        },
        // C zstd's level 4 tables, with a 16 MiB window and faster skip
        // growth on large inputs.
        4 => LevelParams {
            strategy: Strategy::DFast,
            window_log: 24,
            hash_log: 18,
            chain_log: 18,
            search_log: 0,
            min_match: 5,
            target_length: 1,
            search_strength: 3,
            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
    }

    /// Enables long distance matching. Unless `window_log` is set, the window
    /// defaults to 2^27 bytes. One-shot calls shrink it to the input.
    #[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 {
        if opts.window_log.is_none() {
            params.window_log = LDM_DEFAULT_WINDOW_LOG;
        }
        params.ldm_params = Some(LdmParams::default_for_window_log(params.window_log));
    }
}