zstd-complete 0.3.0

A pure Rust Zstandard compressor and decompressor with bounded streaming I/O
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
use core::convert::TryInto;

use super::*;

impl MatchGenerator {
    /// Process bytes and add the suffixes to the suffix store up to a specific index
    #[inline(always)]
    pub(super) fn add_suffixes_till(&mut self, idx: usize) {
        if self.last_entry().data.len() < MIN_MATCH_LEN {
            return;
        }
        if !self.should_track_second_newest_for_current_entry() {
            let suffix_idx = self.suffix_idx;
            let last_entry = self.last_entry_mut();
            let slice = &last_entry.data[suffix_idx..idx];
            for (key_index, key) in slice.windows(MIN_MATCH_LEN).enumerate() {
                last_entry.suffixes.insert(key, suffix_idx + key_index);
            }
            return;
        }

        for insert_idx in self.suffix_idx..idx {
            self.add_suffix_at(insert_idx);
        }
    }

    #[cfg(test)]
    #[inline(always)]
    pub(super) fn add_suffixes_for_match(&mut self, idx: usize) {
        self.add_suffixes_for_match_with_dense_limit(idx, DENSE_MATCH_INDEX_LIMIT);
    }

    #[inline(always)]
    pub(super) fn add_suffixes_for_match_with_dense_limit(
        &mut self,
        idx: usize,
        dense_limit: usize,
    ) {
        if idx - self.suffix_idx <= dense_limit {
            self.add_suffixes_till(idx);
            return;
        }

        let suffix_idx = self.suffix_idx;
        self.add_suffix_at(suffix_idx);
        self.add_suffix_at(suffix_idx + 2);
        if self.use_complementary_end_insertion_for_current_block() {
            self.add_suffix_at(idx.saturating_sub(1));
        }
        self.add_suffix_at(idx.saturating_sub(SPARSE_MATCH_END_INDEX_BACKOFF));
    }

    #[inline(always)]
    pub(super) fn add_suffixes_for_sparse_best_match(&mut self, idx: usize) {
        let suffix_idx = self.suffix_idx;
        self.add_suffix_at(suffix_idx);
        self.add_suffix_at(suffix_idx + 2);
        if self.use_complementary_end_insertion_for_current_block() {
            self.add_suffix_at(idx.saturating_sub(1));
        }
        self.add_suffix_at(idx.saturating_sub(SPARSE_MATCH_END_INDEX_BACKOFF));
    }

    #[inline(always)]
    pub(super) fn emit_candidate(
        &mut self,
        candidate: MatchCandidate,
        handle_sequence: &mut impl for<'a> FnMut(Sequence<'a>),
    ) {
        let MatchCandidate {
            start_idx,
            offset,
            match_len,
            repeat_offset,
            ..
        } = candidate;
        let literals_empty = start_idx == self.last_idx_in_sequence;
        let sparse_match = start_idx + match_len - self.suffix_idx > DENSE_MATCH_INDEX_LIMIT;
        if sparse_match && start_idx > self.suffix_idx {
            self.add_suffix_at(start_idx);
        }
        if self.use_complementary_end_insertion_for_current_block()
            && literals_empty
            && repeat_offset
        {
            self.add_suffixes_for_sparse_best_match(start_idx + match_len);
        } else {
            self.add_suffixes_for_match_with_dense_limit(
                start_idx + match_len,
                DENSE_MATCH_INDEX_LIMIT,
            );
        }

        let last_entry_idx = self.last_entry_index();
        let last_entry = &self.window[last_entry_idx];
        let literals = &last_entry.data[self.last_idx_in_sequence..start_idx];
        #[cfg(test)]
        self.diagnostics
            .borrow_mut()
            .record(candidate, literals_empty);
        let offset_value = Self::bounded_u32(offset);
        self.offset_history
            .update_after_match(offset_value, !literals_empty);

        self.suffix_idx = start_idx + match_len;
        self.last_idx_in_sequence = self.suffix_idx;
        handle_sequence(Sequence::Triple {
            literals,
            offset,
            match_len,
        });
    }

    #[inline(always)]
    pub(super) fn add_suffix_at(&mut self, idx: usize) {
        let key_value = {
            let last_entry = self.last_entry();
            let Some(key) = last_entry.data.get(idx..idx + MIN_MATCH_LEN) else {
                return;
            };
            SuffixStore::key_value(key)
        };
        let track_second_newest = self.should_track_second_newest_for_current_entry();
        let track_third_newest = self.uses_dictionary_lockfile_second_newest_path();
        let sidecar_update = if track_second_newest {
            let last_entry = self.last_entry();
            let slot_index = last_entry.suffixes.slot_key(key_value).index;
            let previous_newest = last_entry.suffixes.slots[slot_index]
                .and_then(|slot| (slot.oldest != slot.newest).then_some(slot.newest));
            let previous_second_newest = if track_third_newest {
                self.current_second_newest_sidecar[slot_index]
            } else {
                None
            };
            let previous_third_newest = if track_third_newest {
                self.current_third_newest_sidecar[slot_index]
            } else {
                None
            };
            let previous_fourth_newest = if track_third_newest {
                self.current_fourth_newest_sidecar[slot_index]
            } else {
                None
            };
            let previous_fifth_newest = if track_third_newest {
                self.current_fifth_newest_sidecar[slot_index]
            } else {
                None
            };
            let previous_sixth_newest = if track_third_newest {
                self.current_sixth_newest_sidecar[slot_index]
            } else {
                None
            };
            let previous_seventh_newest = if track_third_newest {
                self.current_seventh_newest_sidecar[slot_index]
            } else {
                None
            };
            let previous_eighth_newest = if track_third_newest {
                self.current_eighth_newest_sidecar[slot_index]
            } else {
                None
            };
            let previous_ninth_newest = if track_third_newest {
                self.current_ninth_newest_sidecar[slot_index]
            } else {
                None
            };
            let previous_tenth_newest = if track_third_newest {
                self.current_tenth_newest_sidecar[slot_index]
            } else {
                None
            };
            let previous_eleventh_newest = if track_third_newest {
                self.current_eleventh_newest_sidecar[slot_index]
            } else {
                None
            };
            let previous_twelfth_newest = if track_third_newest {
                self.current_twelfth_newest_sidecar[slot_index]
            } else {
                None
            };
            Some((
                slot_index,
                previous_newest,
                previous_second_newest,
                previous_third_newest,
                previous_fourth_newest,
                previous_fifth_newest,
                previous_sixth_newest,
                previous_seventh_newest,
                previous_eighth_newest,
                previous_ninth_newest,
                previous_tenth_newest,
                previous_eleventh_newest,
                previous_twelfth_newest,
            ))
        } else {
            None
        };
        let long_hash_update = if self.should_track_current_long_hash() {
            let last_entry = self.last_entry();
            last_entry.data.get(idx..idx + 8).and_then(|key| {
                let key = u64::from_le_bytes(key.try_into().ok()?);
                Some((
                    last_entry.suffixes.slot_key(key).index,
                    SuffixStore::stored_index(idx),
                ))
            })
        } else {
            None
        };
        if let Some((
            slot_index,
            previous_newest,
            previous_second_newest,
            previous_third_newest,
            previous_fourth_newest,
            previous_fifth_newest,
            previous_sixth_newest,
            previous_seventh_newest,
            previous_eighth_newest,
            previous_ninth_newest,
            previous_tenth_newest,
            previous_eleventh_newest,
            previous_twelfth_newest,
        )) = sidecar_update
        {
            self.current_second_newest_sidecar[slot_index] = previous_newest;
            if track_third_newest {
                self.current_third_newest_sidecar[slot_index] = previous_second_newest;
                self.current_fourth_newest_sidecar[slot_index] = previous_third_newest;
                self.current_fifth_newest_sidecar[slot_index] = previous_fourth_newest;
                self.current_sixth_newest_sidecar[slot_index] = previous_fifth_newest;
                self.current_seventh_newest_sidecar[slot_index] = previous_sixth_newest;
                self.current_eighth_newest_sidecar[slot_index] = previous_seventh_newest;
                self.current_ninth_newest_sidecar[slot_index] = previous_eighth_newest;
                self.current_tenth_newest_sidecar[slot_index] = previous_ninth_newest;
                self.current_eleventh_newest_sidecar[slot_index] = previous_tenth_newest;
                self.current_twelfth_newest_sidecar[slot_index] = previous_eleventh_newest;
                self.current_thirteenth_newest_sidecar[slot_index] = previous_twelfth_newest;
            }
        }
        if let Some((slot_index, stored_index)) = long_hash_update {
            self.current_long_hash[slot_index] = Some(stored_index);
        }
        let last_entry = self.last_entry_mut();
        let Some(key) = last_entry.data.get(idx..idx + MIN_MATCH_LEN) else {
            return;
        };
        last_entry.suffixes.insert(key, idx);
    }

    /// Skip matching for the whole current window entry
    pub(super) fn skip_matching(&mut self) {
        let len = self.last_entry().data.len();
        self.add_suffixes_till(len);
        self.suffix_idx = len;
        self.last_idx_in_sequence = len;
    }

    pub(super) fn skip_matching_for_incompressible(&mut self) {
        let len = self.last_entry().data.len();
        self.suffix_idx = len;
        self.last_idx_in_sequence = len;
    }

    pub(super) fn skip_matching_for_rle(&mut self) {
        let len = self.last_entry().data.len();
        if len >= MIN_MATCH_LEN {
            let first_suffix = self.suffix_idx;
            self.add_suffix_at(first_suffix);
            let last_suffix = len - MIN_MATCH_LEN;
            if last_suffix != first_suffix {
                self.add_suffix_at(last_suffix);
            }
        }
        self.suffix_idx = len;
        self.last_idx_in_sequence = len;
    }

    /// Add a new window entry. Will panic if the last window entry hasn't been processed properly.
    /// If any resources are released by pushing the new entry they are returned via the callback
    pub(super) fn add_data(
        &mut self,
        data: Vec<u8>,
        suffixes: SuffixStore,
        reuse_space: impl FnMut(Vec<u8>, SuffixStore),
    ) {
        assert!(self.window.is_empty() || self.suffix_idx == self.last_entry().data.len());
        assert!(data.len() <= u32::MAX as usize);
        let len = data.len();
        let is_text_block = Self::likely_text(&data);
        let is_short_line_text = is_text_block && Self::likely_short_line_text(&data);
        let min_non_repeat_match_len = self.min_non_repeat_match_len_for_text(&data, is_text_block);
        let active_window_size = self.active_window_size_for_text_kind(is_text_block);
        self.reserve(data.len(), active_window_size, reuse_space);
        #[cfg(debug_assertions)]
        self.concat_window.extend_from_slice(&data);

        if let Some(last_len) = self.window.last().map(|last| last.data.len()) {
            for entry in self.window.iter_mut() {
                entry.base_offset += last_len;
            }
        }

        self.window.push(WindowEntry {
            data,
            suffixes,
            base_offset: 0,
        });
        let dictionary_lockfile_text =
            matches!(self.file_type_hint, CompressionFileType::DictionaryText)
                && is_text_block
                && min_non_repeat_match_len == MIN_MATCH_LEN
                && (matches!(self.file_profile_hint, CompressionFileProfile::CargoLock)
                    || Self::likely_lockfile_text(&self.last_entry().data));
        let composer_dictionary_text =
            matches!(self.file_type_hint, CompressionFileType::DictionaryText)
                && is_text_block
                && is_short_line_text
                && (matches!(self.file_profile_hint, CompressionFileProfile::ComposerLock)
                    || Self::likely_composer_dictionary_text(&self.last_entry().data));
        self.current_block_is_dictionary_lockfile_text = dictionary_lockfile_text;
        self.current_block_is_composer_dictionary_text =
            composer_dictionary_text && !dictionary_lockfile_text;
        self.current_block_is_structured_json_config_text =
            matches!(self.file_type_hint, CompressionFileType::ConfigText)
                && is_text_block
                && is_short_line_text
                && Self::likely_structured_json_config_text(&self.last_entry().data);
        self.current_block_is_tsconfig_json_config_text =
            matches!(self.file_type_hint, CompressionFileType::ConfigText)
                && is_text_block
                && is_short_line_text
                && Self::likely_tsconfig_json_config_text(&self.last_entry().data);
        let dictionary_current_entry_second_newest =
            matches!(self.file_type_hint, CompressionFileType::DictionaryText)
                && is_text_block
                && min_non_repeat_match_len == MIN_MATCH_LEN
                && len >= BEST_SECOND_NEWEST_MIN_BLOCK_LEN;
        let code_text_current_entry_second_newest =
            matches!(self.file_type_hint, CompressionFileType::CodeText)
                && is_short_line_text
                && (BEST_SECOND_NEWEST_MIN_BLOCK_LEN..=CODE_TEXT_SECOND_NEWEST_MAX_BLOCK_LEN)
                    .contains(&len);
        let track_second_newest = (self.use_second_newest_probe
            && min_non_repeat_match_len == MIN_MATCH_LEN
            && len >= BEST_SECOND_NEWEST_MIN_BLOCK_LEN)
            || dictionary_current_entry_second_newest
            || code_text_current_entry_second_newest
            || dictionary_lockfile_text
            || (self.use_fast_binary_small_second_newest
                && !is_text_block
                && min_non_repeat_match_len == MIN_MATCH_LEN
                && (len <= FASTEST_SECOND_NEWEST_MAX_BLOCK_LEN
                    || (matches!(self.file_type_hint, CompressionFileType::Unknown)
                        && len <= FASTEST_UNKNOWN_SECOND_NEWEST_MAX_BLOCK_LEN)));
        if track_second_newest {
            let sidecar_len = self.last_entry().suffixes.slots.len();
            self.current_second_newest_sidecar = alloc::vec![None; sidecar_len];
            if dictionary_lockfile_text {
                self.current_third_newest_sidecar = alloc::vec![None; sidecar_len];
                self.current_fourth_newest_sidecar = alloc::vec![None; sidecar_len];
                self.current_fifth_newest_sidecar = alloc::vec![None; sidecar_len];
                self.current_sixth_newest_sidecar = alloc::vec![None; sidecar_len];
                self.current_seventh_newest_sidecar = alloc::vec![None; sidecar_len];
                self.current_eighth_newest_sidecar = alloc::vec![None; sidecar_len];
                self.current_ninth_newest_sidecar = alloc::vec![None; sidecar_len];
                self.current_tenth_newest_sidecar = alloc::vec![None; sidecar_len];
                self.current_eleventh_newest_sidecar = alloc::vec![None; sidecar_len];
                self.current_twelfth_newest_sidecar = alloc::vec![None; sidecar_len];
                self.current_thirteenth_newest_sidecar = alloc::vec![None; sidecar_len];
            } else {
                self.current_third_newest_sidecar.clear();
                self.current_fourth_newest_sidecar.clear();
                self.current_fifth_newest_sidecar.clear();
                self.current_sixth_newest_sidecar.clear();
                self.current_seventh_newest_sidecar.clear();
                self.current_eighth_newest_sidecar.clear();
                self.current_ninth_newest_sidecar.clear();
                self.current_tenth_newest_sidecar.clear();
                self.current_eleventh_newest_sidecar.clear();
                self.current_twelfth_newest_sidecar.clear();
                self.current_thirteenth_newest_sidecar.clear();
            }
            if self.should_track_current_long_hash() {
                self.current_long_hash = alloc::vec![None; sidecar_len];
            } else {
                self.current_long_hash.clear();
            }
        } else {
            self.current_second_newest_sidecar.clear();
            self.current_third_newest_sidecar.clear();
            self.current_fourth_newest_sidecar.clear();
            self.current_fifth_newest_sidecar.clear();
            self.current_sixth_newest_sidecar.clear();
            self.current_seventh_newest_sidecar.clear();
            self.current_eighth_newest_sidecar.clear();
            self.current_ninth_newest_sidecar.clear();
            self.current_tenth_newest_sidecar.clear();
            self.current_eleventh_newest_sidecar.clear();
            self.current_twelfth_newest_sidecar.clear();
            self.current_thirteenth_newest_sidecar.clear();
            self.current_long_hash.clear();
        }
        let last_suffix_len_log = self.last_entry().suffixes.len_log;
        self.uniform_suffix_len_log = match (self.uniform_suffix_len_log, self.window.len()) {
            (_, 1) => Some(last_suffix_len_log),
            (Some(len_log), _) if len_log == last_suffix_len_log => Some(len_log),
            _ => None,
        };
        self.window_size += len;
        self.suffix_idx = 0;
        self.last_idx_in_sequence = 0;
        self.is_text_block = is_text_block;
        self.is_short_line_text = is_short_line_text;
        self.min_non_repeat_match_len = min_non_repeat_match_len;
    }

    /// Reserve space for a new window entry
    /// If any resources are released by pushing the new entry they are returned via the callback
    pub(super) fn reserve(
        &mut self,
        amount: usize,
        max_window_size: usize,
        mut reuse_space: impl FnMut(Vec<u8>, SuffixStore),
    ) {
        assert!(self.max_window_size >= amount);
        assert!(max_window_size >= amount);
        while self.window_size + amount > max_window_size {
            let removed = self.window.remove(0);
            self.window_size -= removed.data.len();
            #[cfg(debug_assertions)]
            self.concat_window.drain(0..removed.data.len());

            let WindowEntry {
                suffixes,
                data: leaked_vec,
                base_offset: _,
            } = removed;
            reuse_space(leaked_vec, suffixes);
        }
    }
}