Skip to main content

zrip_encode/
context.rs

1#[cfg(feature = "alloc")]
2use alloc::borrow::Cow;
3#[cfg(feature = "alloc")]
4use alloc::vec;
5#[cfg(feature = "alloc")]
6use alloc::vec::Vec;
7
8use crate::block_encoder::{self, BlockEncodeWorkspace};
9use crate::fast::AttachedDict;
10use crate::output::{OutputSink, SliceSink};
11use crate::rebase::{self, PositionBase};
12use crate::strategy::{self, LevelParams, Strategy};
13use crate::{dfast, fast, skip_match_search, write_frame_header_with_checksum};
14use zrip_core::Sequence;
15use zrip_core::dict::Dictionary;
16use zrip_core::error::CompressError;
17use zrip_core::frame::MAX_BLOCK_SIZE;
18use zrip_core::huffman::encode::HuffmanEncodeTable;
19use zrip_core::xxhash::{Xxh64State, xxh64};
20
21/// Largest Fast-strategy input that keeps the dictionary in its own table
22/// instead of copying the prefilled table, as C zstd does for its fast
23/// strategy.
24const ATTACH_THRESHOLD: usize = 8192;
25
26/// Pre-computed dictionary state for hot-loop compression.
27///
28/// Built once from a [`Dictionary`] + [`LevelParams`]. Caches the pre-filled
29/// hash table(s) and a combined buffer with the dict prefix already loaded,
30/// plus encode-side entropy tables built from the dict's decode tables.
31pub(crate) struct PreparedDict {
32    combined: Vec<u8>,
33    hash_snapshot: Vec<u32>,
34    hash_long_snapshot: Vec<u32>,
35    /// Fast-strategy dictionary tables for attached inputs, one per minimum
36    /// match the level uses, sized to the dictionary content.
37    attached: Vec<(u32, Vec<u32>)>,
38    attached_hash_log: u32,
39    prefix_len: usize,
40    rep_offsets: [u32; 3],
41    dict_id: u32,
42    huf_table: Option<HuffmanEncodeTable>,
43    ll_table: Option<block_encoder::FseEncodeTable>,
44    of_table: Option<block_encoder::FseEncodeTable>,
45    ml_table: Option<block_encoder::FseEncodeTable>,
46}
47
48impl PreparedDict {
49    pub fn new(dict: &Dictionary, level: i32, params: &LevelParams) -> Self {
50        let prefix = dict.content();
51        let prefix_len = prefix.len();
52
53        let mut combined = Vec::with_capacity(prefix_len + MAX_BLOCK_SIZE);
54        combined.extend_from_slice(prefix);
55
56        let attached_hash_log = if prefix_len >= 2 {
57            let src_log = strategy::src_log(prefix_len);
58            (src_log + 1).clamp(
59                strategy::HASH_LOG_MIN,
60                params.hash_log.max(strategy::HASH_LOG_MIN),
61            )
62        } else {
63            strategy::HASH_LOG_MIN
64        };
65        let mut attached: Vec<(u32, Vec<u32>)> = Vec::new();
66        if params.strategy == Strategy::Fast {
67            // Small inputs at negative levels search with a shorter match.
68            for size in [1, ATTACH_THRESHOLD, MAX_BLOCK_SIZE] {
69                let min_match = strategy::level_params_for_size(level, prefix_len + size)
70                    .expect("level validated by the caller")
71                    .min_match;
72                if attached.iter().all(|(mls, _)| *mls != min_match) {
73                    let mut table = vec![0u32; 1 << attached_hash_log];
74                    fast::prefill_hash_table(
75                        &combined,
76                        prefix_len,
77                        attached_hash_log,
78                        min_match,
79                        &mut table,
80                    );
81                    attached.push((min_match, table));
82                }
83            }
84        }
85
86        let (hash_snapshot, hash_long_snapshot) = match params.strategy {
87            Strategy::Fast => {
88                let hash_size = 1usize << params.hash_log;
89                let mut hash_table = vec![0u32; hash_size];
90                fast::prefill_hash_table(
91                    &combined,
92                    prefix_len,
93                    params.hash_log,
94                    params.min_match,
95                    &mut hash_table,
96                );
97                (hash_table, Vec::new())
98            }
99            Strategy::DFast => {
100                let short_size = 1usize << params.chain_log;
101                let long_size = 1usize << params.hash_log;
102                let mut hash_short = vec![0u32; short_size];
103                let mut hash_long = vec![0u32; long_size];
104                dfast::prefill_hash_tables(
105                    &combined,
106                    prefix_len,
107                    params.hash_log,
108                    params.chain_log,
109                    params.min_match,
110                    &mut hash_short,
111                    &mut hash_long,
112                );
113                (hash_short, hash_long)
114            }
115        };
116
117        let huf_table = dict
118            .huf_table()
119            .and_then(|(dt, tl)| HuffmanEncodeTable::from_decode_table(dt, tl));
120
121        let ll_table = dict
122            .ll_table()
123            .map(|(dt, al)| block_encoder::FseEncodeTable::from_decode_table(dt, al, 35));
124        let of_table = dict
125            .of_table()
126            .map(|(dt, al)| block_encoder::FseEncodeTable::from_decode_table(dt, al, 31));
127        let ml_table = dict
128            .ml_table()
129            .map(|(dt, al)| block_encoder::FseEncodeTable::from_decode_table(dt, al, 52));
130
131        Self {
132            combined,
133            hash_snapshot,
134            hash_long_snapshot,
135            attached,
136            attached_hash_log,
137            prefix_len,
138            rep_offsets: *dict.rep_offsets(),
139            dict_id: dict.id(),
140            huf_table,
141            ll_table,
142            of_table,
143            ml_table,
144        }
145    }
146
147    /// The attached dictionary table hashed for `min_match`.
148    fn attached_table(&self, min_match: u32) -> Option<&[u32]> {
149        self.attached
150            .iter()
151            .find(|(mls, _)| *mls == min_match)
152            .map(|(_, table)| table.as_slice())
153    }
154}
155
156/// Reusable compression context that amortizes hash table and buffer allocations.
157///
158/// Holds internal state (hash tables, output buffer, block encoder workspace)
159/// across calls. Useful when compressing many small inputs in a loop.
160///
161/// ```
162/// let mut ctx = zrip::CompressContext::new(1).unwrap();
163/// for i in 0..10 {
164///     let data = format!("message {i}").repeat(100);
165///     let compressed = ctx.compress(data.as_bytes()).unwrap();
166///     assert!(compressed.len() < data.len());
167/// }
168/// ```
169pub struct CompressContext {
170    level: i32,
171    content_checksum: bool,
172    /// The dictionary and expected input size to prepare it again on a
173    /// level change.
174    dict: Option<(Dictionary, usize)>,
175    prepared: Option<PreparedDict>,
176    hash_table: Vec<u32>,
177    hash_long: Vec<u32>,
178    dict_hash: Vec<u32>,
179    small_hash: Vec<u32>,
180    sequences: Vec<Sequence>,
181    output: Vec<u8>,
182    workspace: BlockEncodeWorkspace,
183    combined: Vec<u8>,
184}
185
186impl CompressContext {
187    /// Creates a new context for the given compression level (-7..=4; -8 runs as -7).
188    ///
189    /// It allocates nothing up front: the first frame sizes the hash tables
190    /// and buffers for its input.
191    pub fn new(level: i32) -> Result<Self, CompressError> {
192        strategy::level_params(level).ok_or(CompressError::InvalidLevel(level))?;
193        Ok(Self {
194            level,
195            content_checksum: true,
196            dict: None,
197            prepared: None,
198            hash_table: Vec::new(),
199            hash_long: Vec::new(),
200            dict_hash: Vec::new(),
201            small_hash: Vec::new(),
202            sequences: Vec::new(),
203            output: Vec::new(),
204            workspace: BlockEncodeWorkspace::new(),
205            combined: Vec::new(),
206        })
207    }
208
209    /// Creates a new context with a pre-loaded dictionary.
210    ///
211    /// The prepared hash table snapshot is built for the T0 (>256 KB)
212    /// parameter tier. Inputs whose tiered params match T0's hash sizes
213    /// use the fast snapshot-restore path; others fall back to per-call
214    /// prefix hashing.
215    ///
216    /// Use [`with_dict_for_size`] to build the snapshot for a specific
217    /// input size tier.
218    pub fn with_dict(level: i32, dict: Dictionary) -> Result<Self, CompressError> {
219        Self::with_dict_for_size(level, dict, usize::MAX)
220    }
221
222    /// Creates a new context with a pre-loaded dictionary, optimized for
223    /// inputs of approximately `expected_size` bytes.
224    ///
225    /// The prepared hash table snapshot is built for the parameter tier
226    /// matching `expected_size`. Inputs in the same tier use the fast
227    /// snapshot-restore path.
228    pub fn with_dict_for_size(
229        level: i32,
230        dict: Dictionary,
231        expected_size: usize,
232    ) -> Result<Self, CompressError> {
233        let total_window = dict.content().len().saturating_add(expected_size);
234        let params = strategy::level_params_for_size(level, total_window)
235            .ok_or(CompressError::InvalidLevel(level))?;
236        let prepared = PreparedDict::new(&dict, level, &params);
237        // Frames restore the tables from the prepared snapshots before use.
238        Ok(Self {
239            level,
240            content_checksum: true,
241            dict: Some((dict, expected_size)),
242            prepared: Some(prepared),
243            hash_table: Vec::new(),
244            hash_long: Vec::new(),
245            dict_hash: Vec::new(),
246            small_hash: Vec::new(),
247            sequences: Vec::new(),
248            output: Vec::new(),
249            workspace: BlockEncodeWorkspace::new(),
250            combined: Vec::new(),
251        })
252    }
253
254    /// The level of the next frame.
255    pub const fn level(&self) -> i32 {
256        self.level
257    }
258
259    /// Switches later frames to `level` (-7..=4; -8 runs as -7).
260    ///
261    /// Hash tables and buffers stay allocated and resize on use. A context
262    /// with a dictionary prepares the dictionary again for the new level.
263    pub fn set_level(&mut self, level: i32) -> Result<(), CompressError> {
264        if level == self.level {
265            return Ok(());
266        }
267        strategy::level_params(level).ok_or(CompressError::InvalidLevel(level))?;
268        if let Some((dict, expected_size)) = &self.dict {
269            let total_window = dict.content().len().saturating_add(*expected_size);
270            let params = strategy::level_params_for_size(level, total_window)
271                .expect("level validated above");
272            self.prepared = Some(PreparedDict::new(dict, level, &params));
273        }
274        self.level = level;
275        Ok(())
276    }
277
278    /// Enables or disables the standard Zstandard frame content checksum.
279    ///
280    /// Checksums are enabled by default. Disabling them is useful when an
281    /// enclosing format already authenticates the decoded content.
282    pub const fn set_content_checksum(&mut self, enabled: bool) {
283        self.content_checksum = enabled;
284    }
285
286    /// Whether newly compressed frames include a content checksum.
287    pub const fn content_checksum(&self) -> bool {
288        self.content_checksum
289    }
290
291    /// Compresses `input` using the context's level and optional dictionary.
292    pub fn compress(&mut self, input: &[u8]) -> Result<Cow<'_, [u8]>, CompressError> {
293        let mut output = core::mem::take(&mut self.output);
294        output.clear();
295        let result = self.compress_to(input, &mut output);
296        self.output = output;
297        result?;
298        Ok(self.take_or_borrow_output())
299    }
300
301    /// Compresses `input` into a caller-owned buffer while reusing this
302    /// context's hash tables, workspace, and dictionary.
303    ///
304    /// Writes one frame to the start of `output` and returns its length.
305    /// Unlike [`Self::compress`], the result can outlive the next use of this
306    /// context without copying. `zrip::compress_bound(input.len())` bytes are
307    /// always enough. Returns [`CompressError::OutputTooSmall`] if the frame
308    /// does not fit; `output` then holds a partial frame.
309    pub fn compress_into(
310        &mut self,
311        input: &[u8],
312        output: &mut [u8],
313    ) -> Result<usize, CompressError> {
314        let mut sink = SliceSink::new(output);
315        self.compress_to(input, &mut sink)?;
316        Ok(sink.pos())
317    }
318
319    /// Writes one frame for `input` to `output`.
320    fn compress_to<S: OutputSink>(
321        &mut self,
322        input: &[u8],
323        output: &mut S,
324    ) -> Result<(), CompressError> {
325        if self.prepared.is_some() {
326            return self.compress_with_prepared(input, output);
327        }
328        let params = strategy::level_params_for_size(self.level, input.len())
329            .expect("level validated at construction");
330        compress_core(
331            input,
332            params,
333            None,
334            &[],
335            [1u32, 4, 8],
336            &mut self.hash_table,
337            &mut self.hash_long,
338            &mut self.dict_hash,
339            &mut self.sequences,
340            output,
341            &mut self.workspace,
342            &mut self.combined,
343            self.content_checksum,
344        )
345    }
346
347    /// Compresses `input` using an ad-hoc dictionary (overrides the stored one).
348    pub fn compress_with_dict(
349        &mut self,
350        input: &[u8],
351        dict: &Dictionary,
352    ) -> Result<Cow<'_, [u8]>, CompressError> {
353        let params =
354            strategy::level_params_with_prefix(self.level, dict.content().len(), input.len())
355                .expect("level validated at construction");
356        self.workspace.prev_ll = dict
357            .ll_table()
358            .map(|(dt, al)| block_encoder::FseEncodeTable::from_decode_table(dt, al, 35));
359        self.workspace.prev_of = dict
360            .of_table()
361            .map(|(dt, al)| block_encoder::FseEncodeTable::from_decode_table(dt, al, 31));
362        self.workspace.prev_ml = dict
363            .ml_table()
364            .map(|(dt, al)| block_encoder::FseEncodeTable::from_decode_table(dt, al, 52));
365        self.workspace.prev_huffman = dict
366            .huf_table()
367            .and_then(|(dt, tl)| HuffmanEncodeTable::from_decode_table(dt, tl));
368        self.output.clear();
369        compress_core(
370            input,
371            params,
372            Some(dict.id()),
373            dict.content(),
374            *dict.rep_offsets(),
375            &mut self.hash_table,
376            &mut self.hash_long,
377            &mut self.dict_hash,
378            &mut self.sequences,
379            &mut self.output,
380            &mut self.workspace,
381            &mut self.combined,
382            self.content_checksum,
383        )?;
384        Ok(self.take_or_borrow_output())
385    }
386
387    fn compress_with_prepared<S: OutputSink>(
388        &mut self,
389        input: &[u8],
390        output: &mut S,
391    ) -> Result<(), CompressError> {
392        let prep = self.prepared.as_ref().unwrap();
393        let params = strategy::level_params_with_prefix(self.level, prep.prefix_len, input.len())
394            .expect("level validated at construction");
395
396        let dict_id = prep.dict_id;
397        let prefix_len = prep.prefix_len;
398        let snapshot_matches = match params.strategy {
399            Strategy::Fast => (1usize << params.hash_log) == prep.hash_snapshot.len(),
400            Strategy::DFast => {
401                (1usize << params.chain_log) == prep.hash_snapshot.len()
402                    && (1usize << params.hash_log) == prep.hash_long_snapshot.len()
403            }
404        };
405        // A one-block input also attaches when the snapshot is sized for
406        // another tier: that beats prefilling a table per call.
407        let attached = (params.strategy == Strategy::Fast
408            && !input.is_empty()
409            && input.len() <= MAX_BLOCK_SIZE
410            && (input.len() <= ATTACH_THRESHOLD || !snapshot_matches))
411            .then(|| prep.attached_table(params.min_match))
412            .flatten();
413        let use_attached = attached.is_some();
414
415        if !use_attached && !snapshot_matches {
416            return self.compress_with_dict_fallback(input, dict_id, prefix_len, output);
417        }
418
419        {
420            let prep = self.prepared.as_mut().unwrap();
421            if !use_attached {
422                // The per-call prefix path resizes these tables for its own
423                // parameter tier, so restore length and contents together.
424                self.hash_table.clear();
425                self.hash_table.extend_from_slice(&prep.hash_snapshot);
426                if !prep.hash_long_snapshot.is_empty() {
427                    self.hash_long.clear();
428                    self.hash_long.extend_from_slice(&prep.hash_long_snapshot);
429                }
430            }
431            prep.combined.truncate(prep.prefix_len);
432            prep.combined.extend_from_slice(input);
433        }
434
435        let prep = self.prepared.as_ref().unwrap();
436
437        if use_attached {
438            self.workspace.prev_huffman = if params.force_raw_literals {
439                None
440            } else {
441                prep.huf_table.clone()
442            };
443        } else if let Some(ref huf) = prep.huf_table {
444            self.workspace.prev_huffman = Some(huf.clone());
445        } else {
446            self.workspace.prev_huffman = None;
447        }
448        self.workspace.prev_ll = prep.ll_table.clone();
449        self.workspace.prev_of = prep.of_table.clone();
450        self.workspace.prev_ml = prep.ml_table.clone();
451
452        output.reserve(input.len() + 32);
453        write_frame_header_with_checksum(
454            output,
455            input.len(),
456            Some(dict_id),
457            params.window_log,
458            self.content_checksum,
459        )?;
460
461        if input.is_empty() {
462            block_encoder::encode_raw_block(&[], true, output)?;
463        } else if use_attached {
464            let input_hash_log = if input.len() >= 2 {
465                let src_log = strategy::src_log(input.len());
466                params.hash_log.min(src_log).max(strategy::HASH_LOG_MIN)
467            } else {
468                strategy::HASH_LOG_MIN
469            };
470            let input_hash_size = 1usize << input_hash_log;
471            self.small_hash.resize(input_hash_size, 0);
472            self.small_hash.fill(0);
473
474            let mut rep_offsets = prep.rep_offsets;
475            let dict = AttachedDict {
476                table: prep.attached_table(params.min_match).unwrap(),
477                hash_log: prep.attached_hash_log,
478                prefix_len,
479            };
480            let input_params = LevelParams {
481                hash_log: input_hash_log,
482                ..params
483            };
484
485            fast::compress_fast_block_attached(
486                &prep.combined,
487                prefix_len + input.len(),
488                &input_params,
489                &prep.rep_offsets,
490                dict,
491                &mut self.small_hash,
492                &mut self.sequences,
493            );
494
495            if params.force_raw_literals {
496                block_encoder::encode_compressed_block_raw(
497                    input,
498                    &self.sequences,
499                    &mut rep_offsets,
500                    true,
501                    output,
502                    &mut self.workspace,
503                )?;
504            } else {
505                block_encoder::encode_compressed_block(
506                    input,
507                    &self.sequences,
508                    &mut rep_offsets,
509                    true,
510                    output,
511                    &mut self.workspace,
512                    strategy::block_policy(&params, input.len()),
513                )?;
514            }
515        } else {
516            let combined = &prep.combined;
517            let mut rep_offsets = prep.rep_offsets;
518
519            if input.len() <= MAX_BLOCK_SIZE {
520                match params.strategy {
521                    Strategy::Fast => {
522                        fast::compress_fast_block(
523                            combined,
524                            prefix_len,
525                            prefix_len + input.len(),
526                            &params,
527                            &rep_offsets,
528                            &mut self.hash_table,
529                            &mut self.sequences,
530                        );
531                    }
532                    Strategy::DFast => {
533                        dfast::compress_dfast_block(
534                            combined,
535                            prefix_len,
536                            prefix_len + input.len(),
537                            &params,
538                            &rep_offsets,
539                            &mut self.hash_table,
540                            &mut self.hash_long,
541                            &mut self.sequences,
542                        );
543                    }
544                }
545                if params.force_raw_literals {
546                    block_encoder::encode_compressed_block_raw(
547                        input,
548                        &self.sequences,
549                        &mut rep_offsets,
550                        true,
551                        output,
552                        &mut self.workspace,
553                    )?;
554                } else {
555                    block_encoder::encode_compressed_block(
556                        input,
557                        &self.sequences,
558                        &mut rep_offsets,
559                        true,
560                        output,
561                        &mut self.workspace,
562                        strategy::block_policy(&params, input.len()),
563                    )?;
564                }
565            } else {
566                let mut positions = PositionBase::new(params.window_log);
567                let mut offset = 0;
568                while offset < input.len() {
569                    let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
570                    let is_last = offset + chunk_size >= input.len();
571                    let (start, end) = (prefix_len + offset, prefix_len + offset + chunk_size);
572                    if let Some(shift) = positions.advance(start, end) {
573                        rebase::reduce_hash_table(&mut self.hash_table, shift);
574                        rebase::reduce_hash_table(&mut self.hash_long, shift);
575                    }
576                    let base = positions.base();
577                    let src = &combined[base..];
578                    match params.strategy {
579                        Strategy::Fast => {
580                            fast::compress_fast_block(
581                                src,
582                                start - base,
583                                end - base,
584                                &params,
585                                &rep_offsets,
586                                &mut self.hash_table,
587                                &mut self.sequences,
588                            );
589                        }
590                        Strategy::DFast => {
591                            dfast::compress_dfast_block(
592                                src,
593                                start - base,
594                                end - base,
595                                &params,
596                                &rep_offsets,
597                                &mut self.hash_table,
598                                &mut self.hash_long,
599                                &mut self.sequences,
600                            );
601                        }
602                    }
603                    if params.force_raw_literals {
604                        block_encoder::encode_compressed_block_raw(
605                            &input[offset..offset + chunk_size],
606                            &self.sequences,
607                            &mut rep_offsets,
608                            is_last,
609                            output,
610                            &mut self.workspace,
611                        )?;
612                    } else {
613                        block_encoder::encode_compressed_block(
614                            &input[offset..offset + chunk_size],
615                            &self.sequences,
616                            &mut rep_offsets,
617                            is_last,
618                            output,
619                            &mut self.workspace,
620                            strategy::block_policy(&params, input.len()),
621                        )?;
622                    }
623                    offset += chunk_size;
624                }
625            }
626        }
627
628        if self.content_checksum {
629            let hash = xxh64(input, 0);
630            let checksum = (hash & 0xFFFF_FFFF) as u32;
631            output.extend_from_slice(&checksum.to_le_bytes())?;
632        }
633
634        Ok(())
635    }
636
637    fn compress_with_dict_fallback<S: OutputSink>(
638        &mut self,
639        input: &[u8],
640        dict_id: u32,
641        prefix_len: usize,
642        output: &mut S,
643    ) -> Result<(), CompressError> {
644        let prep = self.prepared.as_ref().unwrap();
645        let rep_offsets = prep.rep_offsets;
646        let prefix = &prep.combined[..prefix_len];
647
648        let params = strategy::level_params_with_prefix(self.level, prefix_len, input.len())
649            .expect("level validated at construction");
650        self.workspace.prev_huffman = prep.huf_table.clone();
651        self.workspace.prev_ll = prep.ll_table.clone();
652        self.workspace.prev_of = prep.of_table.clone();
653        self.workspace.prev_ml = prep.ml_table.clone();
654        compress_core(
655            input,
656            params,
657            Some(dict_id),
658            prefix,
659            rep_offsets,
660            &mut self.hash_table,
661            &mut self.hash_long,
662            &mut self.dict_hash,
663            &mut self.sequences,
664            output,
665            &mut self.workspace,
666            &mut self.combined,
667            self.content_checksum,
668        )
669    }
670
671    fn take_or_borrow_output(&mut self) -> Cow<'_, [u8]> {
672        if self.output.len() >= zrip_core::LARGE_OUTPUT_THRESHOLD {
673            Cow::Owned(core::mem::take(&mut self.output))
674        } else {
675            Cow::Borrowed(&self.output)
676        }
677    }
678}
679
680#[allow(clippy::too_many_arguments, clippy::unnecessary_wraps)]
681fn compress_core<S: OutputSink>(
682    input: &[u8],
683    params: LevelParams,
684    dict_id: Option<u32>,
685    prefix: &[u8],
686    init_rep_offsets: [u32; 3],
687    hash_table: &mut Vec<u32>,
688    hash_long: &mut Vec<u32>,
689    dict_hash: &mut Vec<u32>,
690    sequences: &mut Vec<Sequence>,
691    output: &mut S,
692    workspace: &mut BlockEncodeWorkspace,
693    combined: &mut Vec<u8>,
694    content_checksum: bool,
695) -> Result<(), CompressError> {
696    let hash_size = match params.strategy {
697        Strategy::Fast => 1usize << params.hash_log,
698        Strategy::DFast => 1usize << params.chain_log,
699    };
700    let long_size = 1usize << params.hash_log;
701
702    if prefix.is_empty() {
703        workspace.prev_huffman = None;
704        workspace.prev_ll = None;
705        workspace.prev_of = None;
706        workspace.prev_ml = None;
707    }
708
709    output.reserve(input.len() + 32);
710    write_frame_header_with_checksum(
711        output,
712        input.len(),
713        dict_id,
714        params.window_log,
715        content_checksum,
716    )?;
717
718    // Paths that walk the input block by block hash each block right after
719    // compressing it, while its bytes are still in cache.
720    let mut checksum = content_checksum.then(|| Xxh64State::new(0));
721    let mut hashed_blocks = false;
722
723    if input.is_empty() {
724        block_encoder::encode_raw_block(&[], true, output)?;
725    } else {
726        let has_prefix = !prefix.is_empty();
727        let mut rep_offsets = init_rep_offsets;
728        let mut offset = 0;
729        let mut positions = PositionBase::new(params.window_log);
730
731        // A table grown from empty is zeroed already.
732        let short_zeroed = hash_table.is_empty();
733        if hash_table.len() != hash_size {
734            hash_table.resize(hash_size, 0);
735        }
736
737        match params.strategy {
738            Strategy::Fast => {
739                if has_prefix && input.len() <= MAX_BLOCK_SIZE {
740                    if dict_hash.len() != hash_size {
741                        dict_hash.resize(hash_size, 0);
742                    }
743                    fast::compress_fast_with_prefix_reuse(
744                        input,
745                        &params,
746                        &rep_offsets,
747                        prefix,
748                        dict_hash,
749                        hash_table,
750                        sequences,
751                        combined,
752                    );
753                    if params.force_raw_literals {
754                        block_encoder::encode_compressed_block_raw(
755                            input,
756                            sequences,
757                            &mut rep_offsets,
758                            true,
759                            output,
760                            workspace,
761                        )?;
762                    } else {
763                        block_encoder::encode_compressed_block(
764                            input,
765                            sequences,
766                            &mut rep_offsets,
767                            true,
768                            output,
769                            workspace,
770                            strategy::block_policy(&params, input.len()),
771                        )?;
772                    }
773                } else if has_prefix {
774                    combined.clear();
775                    combined.reserve(prefix.len() + input.len());
776                    combined.extend_from_slice(prefix);
777                    combined.extend_from_slice(input);
778                    let plen = prefix.len();
779                    fast::prefill_hash_table(
780                        combined,
781                        plen,
782                        params.hash_log,
783                        params.min_match,
784                        hash_table,
785                    );
786
787                    while offset < input.len() {
788                        let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
789                        let is_last = offset + chunk_size >= input.len();
790                        let (start, end) = (plen + offset, plen + offset + chunk_size);
791                        if let Some(shift) = positions.advance(start, end) {
792                            rebase::reduce_hash_table(hash_table, shift);
793                        }
794                        let base = positions.base();
795                        fast::compress_fast_block(
796                            &combined[base..],
797                            start - base,
798                            end - base,
799                            &params,
800                            &rep_offsets,
801                            hash_table,
802                            sequences,
803                        );
804                        if params.force_raw_literals {
805                            block_encoder::encode_compressed_block_raw(
806                                &input[offset..offset + chunk_size],
807                                sequences,
808                                &mut rep_offsets,
809                                is_last,
810                                output,
811                                workspace,
812                            )?;
813                        } else {
814                            block_encoder::encode_compressed_block(
815                                &input[offset..offset + chunk_size],
816                                sequences,
817                                &mut rep_offsets,
818                                is_last,
819                                output,
820                                workspace,
821                                strategy::block_policy(&params, input.len()),
822                            )?;
823                        }
824                        offset += chunk_size;
825                    }
826                } else {
827                    if !short_zeroed {
828                        hash_table.fill(0);
829                    }
830                    while offset < input.len() {
831                        let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
832                        let block_end = offset + chunk_size;
833                        let is_last = block_end >= input.len();
834                        let block = &input[offset..block_end];
835
836                        if let Some(shift) = positions.advance(offset, block_end) {
837                            rebase::reduce_hash_table(hash_table, shift);
838                        }
839                        if skip_match_search(&params, input.len(), block) {
840                            block_encoder::encode_raw_block(block, is_last, output)?;
841                        } else {
842                            let base = positions.base();
843                            fast::compress_fast_block(
844                                &input[base..],
845                                offset - base,
846                                block_end - base,
847                                &params,
848                                &rep_offsets,
849                                hash_table,
850                                sequences,
851                            );
852                            if params.force_raw_literals {
853                                block_encoder::encode_compressed_block_raw(
854                                    block,
855                                    sequences,
856                                    &mut rep_offsets,
857                                    is_last,
858                                    output,
859                                    workspace,
860                                )?;
861                            } else {
862                                block_encoder::encode_compressed_block(
863                                    block,
864                                    sequences,
865                                    &mut rep_offsets,
866                                    is_last,
867                                    output,
868                                    workspace,
869                                    strategy::block_policy(&params, input.len()),
870                                )?;
871                            }
872                        }
873                        if let Some(c) = checksum.as_mut() {
874                            c.update(block);
875                        }
876                        offset = block_end;
877                    }
878                    hashed_blocks = true;
879                }
880            }
881            Strategy::DFast => {
882                let long_zeroed = hash_long.is_empty();
883                if hash_long.len() != long_size {
884                    hash_long.resize(long_size, 0);
885                }
886                if has_prefix && input.len() <= MAX_BLOCK_SIZE {
887                    dfast::compress_dfast_with_prefix_reuse(
888                        input,
889                        &params,
890                        &rep_offsets,
891                        prefix,
892                        hash_table,
893                        hash_long,
894                        sequences,
895                        combined,
896                    );
897                    block_encoder::encode_compressed_block(
898                        input,
899                        sequences,
900                        &mut rep_offsets,
901                        true,
902                        output,
903                        workspace,
904                        strategy::block_policy(&params, input.len()),
905                    )?;
906                } else if has_prefix {
907                    combined.clear();
908                    combined.reserve(prefix.len() + input.len());
909                    combined.extend_from_slice(prefix);
910                    combined.extend_from_slice(input);
911                    let plen = prefix.len();
912                    dfast::prefill_hash_tables(
913                        combined,
914                        plen,
915                        params.hash_log,
916                        params.chain_log,
917                        params.min_match,
918                        hash_table,
919                        hash_long,
920                    );
921
922                    while offset < input.len() {
923                        let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
924                        let is_last = offset + chunk_size >= input.len();
925                        let (start, end) = (plen + offset, plen + offset + chunk_size);
926                        if let Some(shift) = positions.advance(start, end) {
927                            rebase::reduce_hash_table(hash_table, shift);
928                            rebase::reduce_hash_table(hash_long, shift);
929                        }
930                        let base = positions.base();
931                        dfast::compress_dfast_block(
932                            &combined[base..],
933                            start - base,
934                            end - base,
935                            &params,
936                            &rep_offsets,
937                            hash_table,
938                            hash_long,
939                            sequences,
940                        );
941                        block_encoder::encode_compressed_block(
942                            &input[offset..offset + chunk_size],
943                            sequences,
944                            &mut rep_offsets,
945                            is_last,
946                            output,
947                            workspace,
948                            strategy::block_policy(&params, input.len()),
949                        )?;
950                        offset += chunk_size;
951                    }
952                } else {
953                    if !short_zeroed {
954                        hash_table.fill(0);
955                    }
956                    if !long_zeroed {
957                        hash_long.fill(0);
958                    }
959                    while offset < input.len() {
960                        let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
961                        let block_end = offset + chunk_size;
962                        let is_last = block_end >= input.len();
963                        let block = &input[offset..block_end];
964
965                        if let Some(shift) = positions.advance(offset, block_end) {
966                            rebase::reduce_hash_table(hash_table, shift);
967                            rebase::reduce_hash_table(hash_long, shift);
968                        }
969                        if skip_match_search(&params, input.len(), block) {
970                            block_encoder::encode_raw_block(block, is_last, output)?;
971                        } else {
972                            let base = positions.base();
973                            dfast::compress_dfast_block(
974                                &input[base..],
975                                offset - base,
976                                block_end - base,
977                                &params,
978                                &rep_offsets,
979                                hash_table,
980                                hash_long,
981                                sequences,
982                            );
983                            block_encoder::encode_compressed_block(
984                                block,
985                                sequences,
986                                &mut rep_offsets,
987                                is_last,
988                                output,
989                                workspace,
990                                strategy::block_policy(&params, input.len()),
991                            )?;
992                        }
993                        if let Some(c) = checksum.as_mut() {
994                            c.update(block);
995                        }
996                        offset = block_end;
997                    }
998                    hashed_blocks = true;
999                }
1000            }
1001        }
1002    }
1003
1004    if let Some(c) = checksum {
1005        let hash = if hashed_blocks {
1006            c.finish()
1007        } else {
1008            xxh64(input, 0)
1009        };
1010        output.extend_from_slice(&((hash & 0xFFFF_FFFF) as u32).to_le_bytes())?;
1011    }
1012
1013    Ok(())
1014}