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    pub fn new(level: i32) -> Result<Self, CompressError> {
189        let params = strategy::level_params(level).ok_or(CompressError::InvalidLevel(level))?;
190        let max_log = strategy::max_hash_log(level).expect("level validated above");
191        let alloc_size = 1usize << max_log;
192        let (hash_table, hash_long) = match params.strategy {
193            Strategy::Fast => (vec![0u32; alloc_size], Vec::new()),
194            Strategy::DFast => (vec![0u32; alloc_size], vec![0u32; alloc_size]),
195        };
196        Ok(Self {
197            level,
198            content_checksum: true,
199            dict: None,
200            prepared: None,
201            hash_table,
202            hash_long,
203            dict_hash: Vec::new(),
204            small_hash: Vec::new(),
205            sequences: Vec::new(),
206            output: Vec::new(),
207            workspace: BlockEncodeWorkspace::new(),
208            combined: Vec::new(),
209        })
210    }
211
212    /// Creates a new context with a pre-loaded dictionary.
213    ///
214    /// The prepared hash table snapshot is built for the T0 (>256 KB)
215    /// parameter tier. Inputs whose tiered params match T0's hash sizes
216    /// use the fast snapshot-restore path; others fall back to per-call
217    /// prefix hashing.
218    ///
219    /// Use [`with_dict_for_size`] to build the snapshot for a specific
220    /// input size tier.
221    pub fn with_dict(level: i32, dict: Dictionary) -> Result<Self, CompressError> {
222        Self::with_dict_for_size(level, dict, usize::MAX)
223    }
224
225    /// Creates a new context with a pre-loaded dictionary, optimized for
226    /// inputs of approximately `expected_size` bytes.
227    ///
228    /// The prepared hash table snapshot is built for the parameter tier
229    /// matching `expected_size`. Inputs in the same tier use the fast
230    /// snapshot-restore path.
231    pub fn with_dict_for_size(
232        level: i32,
233        dict: Dictionary,
234        expected_size: usize,
235    ) -> Result<Self, CompressError> {
236        let total_window = dict.content().len().saturating_add(expected_size);
237        let params = strategy::level_params_for_size(level, total_window)
238            .ok_or(CompressError::InvalidLevel(level))?;
239        let prepared = PreparedDict::new(&dict, level, &params);
240        let hash_table = vec![0u32; prepared.hash_snapshot.len()];
241        let hash_long = vec![0u32; prepared.hash_long_snapshot.len()];
242        Ok(Self {
243            level,
244            content_checksum: true,
245            dict: Some((dict, expected_size)),
246            prepared: Some(prepared),
247            hash_table,
248            hash_long,
249            dict_hash: Vec::new(),
250            small_hash: Vec::new(),
251            sequences: Vec::new(),
252            output: Vec::new(),
253            workspace: BlockEncodeWorkspace::new(),
254            combined: Vec::new(),
255        })
256    }
257
258    /// The level of the next frame.
259    pub const fn level(&self) -> i32 {
260        self.level
261    }
262
263    /// Switches later frames to `level` (-7..=4; -8 runs as -7).
264    ///
265    /// Hash tables and buffers stay allocated and resize on use. A context
266    /// with a dictionary prepares the dictionary again for the new level.
267    pub fn set_level(&mut self, level: i32) -> Result<(), CompressError> {
268        if level == self.level {
269            return Ok(());
270        }
271        strategy::level_params(level).ok_or(CompressError::InvalidLevel(level))?;
272        if let Some((dict, expected_size)) = &self.dict {
273            let total_window = dict.content().len().saturating_add(*expected_size);
274            let params = strategy::level_params_for_size(level, total_window)
275                .expect("level validated above");
276            self.prepared = Some(PreparedDict::new(dict, level, &params));
277        }
278        self.level = level;
279        Ok(())
280    }
281
282    /// Enables or disables the standard Zstandard frame content checksum.
283    ///
284    /// Checksums are enabled by default. Disabling them is useful when an
285    /// enclosing format already authenticates the decoded content.
286    pub const fn set_content_checksum(&mut self, enabled: bool) {
287        self.content_checksum = enabled;
288    }
289
290    /// Whether newly compressed frames include a content checksum.
291    pub const fn content_checksum(&self) -> bool {
292        self.content_checksum
293    }
294
295    /// Compresses `input` using the context's level and optional dictionary.
296    pub fn compress(&mut self, input: &[u8]) -> Result<Cow<'_, [u8]>, CompressError> {
297        let mut output = core::mem::take(&mut self.output);
298        output.clear();
299        let result = self.compress_to(input, &mut output);
300        self.output = output;
301        result?;
302        Ok(self.take_or_borrow_output())
303    }
304
305    /// Compresses `input` into a caller-owned buffer while reusing this
306    /// context's hash tables, workspace, and dictionary.
307    ///
308    /// Writes one frame to the start of `output` and returns its length.
309    /// Unlike [`Self::compress`], the result can outlive the next use of this
310    /// context without copying. `zrip::compress_bound(input.len())` bytes are
311    /// always enough. Returns [`CompressError::OutputTooSmall`] if the frame
312    /// does not fit; `output` then holds a partial frame.
313    pub fn compress_into(
314        &mut self,
315        input: &[u8],
316        output: &mut [u8],
317    ) -> Result<usize, CompressError> {
318        let mut sink = SliceSink::new(output);
319        self.compress_to(input, &mut sink)?;
320        Ok(sink.pos())
321    }
322
323    /// Writes one frame for `input` to `output`.
324    fn compress_to<S: OutputSink>(
325        &mut self,
326        input: &[u8],
327        output: &mut S,
328    ) -> Result<(), CompressError> {
329        if self.prepared.is_some() {
330            return self.compress_with_prepared(input, output);
331        }
332        let params = strategy::level_params_for_size(self.level, input.len())
333            .expect("level validated at construction");
334        compress_core(
335            input,
336            params,
337            None,
338            &[],
339            [1u32, 4, 8],
340            &mut self.hash_table,
341            &mut self.hash_long,
342            &mut self.dict_hash,
343            &mut self.sequences,
344            output,
345            &mut self.workspace,
346            &mut self.combined,
347            self.content_checksum,
348        )
349    }
350
351    /// Compresses `input` using an ad-hoc dictionary (overrides the stored one).
352    pub fn compress_with_dict(
353        &mut self,
354        input: &[u8],
355        dict: &Dictionary,
356    ) -> Result<Cow<'_, [u8]>, CompressError> {
357        let params =
358            strategy::level_params_with_prefix(self.level, dict.content().len(), input.len())
359                .expect("level validated at construction");
360        self.workspace.prev_ll = dict
361            .ll_table()
362            .map(|(dt, al)| block_encoder::FseEncodeTable::from_decode_table(dt, al, 35));
363        self.workspace.prev_of = dict
364            .of_table()
365            .map(|(dt, al)| block_encoder::FseEncodeTable::from_decode_table(dt, al, 31));
366        self.workspace.prev_ml = dict
367            .ml_table()
368            .map(|(dt, al)| block_encoder::FseEncodeTable::from_decode_table(dt, al, 52));
369        self.workspace.prev_huffman = dict
370            .huf_table()
371            .and_then(|(dt, tl)| HuffmanEncodeTable::from_decode_table(dt, tl));
372        self.output.clear();
373        compress_core(
374            input,
375            params,
376            Some(dict.id()),
377            dict.content(),
378            *dict.rep_offsets(),
379            &mut self.hash_table,
380            &mut self.hash_long,
381            &mut self.dict_hash,
382            &mut self.sequences,
383            &mut self.output,
384            &mut self.workspace,
385            &mut self.combined,
386            self.content_checksum,
387        )?;
388        Ok(self.take_or_borrow_output())
389    }
390
391    fn compress_with_prepared<S: OutputSink>(
392        &mut self,
393        input: &[u8],
394        output: &mut S,
395    ) -> Result<(), CompressError> {
396        let prep = self.prepared.as_ref().unwrap();
397        let params = strategy::level_params_with_prefix(self.level, prep.prefix_len, input.len())
398            .expect("level validated at construction");
399
400        let dict_id = prep.dict_id;
401        let prefix_len = prep.prefix_len;
402        let snapshot_matches = match params.strategy {
403            Strategy::Fast => (1usize << params.hash_log) == prep.hash_snapshot.len(),
404            Strategy::DFast => {
405                (1usize << params.chain_log) == prep.hash_snapshot.len()
406                    && (1usize << params.hash_log) == prep.hash_long_snapshot.len()
407            }
408        };
409        // A one-block input also attaches when the snapshot is sized for
410        // another tier: that beats prefilling a table per call.
411        let attached = (params.strategy == Strategy::Fast
412            && !input.is_empty()
413            && input.len() <= MAX_BLOCK_SIZE
414            && (input.len() <= ATTACH_THRESHOLD || !snapshot_matches))
415            .then(|| prep.attached_table(params.min_match))
416            .flatten();
417        let use_attached = attached.is_some();
418
419        if !use_attached && !snapshot_matches {
420            return self.compress_with_dict_fallback(input, dict_id, prefix_len, output);
421        }
422
423        {
424            let prep = self.prepared.as_mut().unwrap();
425            if !use_attached {
426                // The per-call prefix path resizes these tables for its own
427                // parameter tier, so restore length and contents together.
428                self.hash_table.clear();
429                self.hash_table.extend_from_slice(&prep.hash_snapshot);
430                if !prep.hash_long_snapshot.is_empty() {
431                    self.hash_long.clear();
432                    self.hash_long.extend_from_slice(&prep.hash_long_snapshot);
433                }
434            }
435            prep.combined.truncate(prep.prefix_len);
436            prep.combined.extend_from_slice(input);
437        }
438
439        let prep = self.prepared.as_ref().unwrap();
440
441        if use_attached {
442            self.workspace.prev_huffman = if params.force_raw_literals {
443                None
444            } else {
445                prep.huf_table.clone()
446            };
447        } else if let Some(ref huf) = prep.huf_table {
448            self.workspace.prev_huffman = Some(huf.clone());
449        } else {
450            self.workspace.prev_huffman = None;
451        }
452        self.workspace.prev_ll = prep.ll_table.clone();
453        self.workspace.prev_of = prep.of_table.clone();
454        self.workspace.prev_ml = prep.ml_table.clone();
455
456        output.reserve(input.len() + 32);
457        write_frame_header_with_checksum(
458            output,
459            input.len(),
460            Some(dict_id),
461            params.window_log,
462            self.content_checksum,
463        )?;
464
465        if input.is_empty() {
466            block_encoder::encode_raw_block(&[], true, output)?;
467        } else if use_attached {
468            let input_hash_log = if input.len() >= 2 {
469                let src_log = strategy::src_log(input.len());
470                params.hash_log.min(src_log).max(strategy::HASH_LOG_MIN)
471            } else {
472                strategy::HASH_LOG_MIN
473            };
474            let input_hash_size = 1usize << input_hash_log;
475            self.small_hash.resize(input_hash_size, 0);
476            self.small_hash.fill(0);
477
478            let mut rep_offsets = prep.rep_offsets;
479            let dict = AttachedDict {
480                table: prep.attached_table(params.min_match).unwrap(),
481                hash_log: prep.attached_hash_log,
482                prefix_len,
483            };
484            let input_params = LevelParams {
485                hash_log: input_hash_log,
486                ..params
487            };
488
489            fast::compress_fast_block_attached(
490                &prep.combined,
491                prefix_len + input.len(),
492                &input_params,
493                &prep.rep_offsets,
494                dict,
495                &mut self.small_hash,
496                &mut self.sequences,
497            );
498
499            if params.force_raw_literals {
500                block_encoder::encode_compressed_block_raw(
501                    input,
502                    &self.sequences,
503                    &mut rep_offsets,
504                    true,
505                    output,
506                    &mut self.workspace,
507                )?;
508            } else {
509                block_encoder::encode_compressed_block(
510                    input,
511                    &self.sequences,
512                    &mut rep_offsets,
513                    true,
514                    output,
515                    &mut self.workspace,
516                    strategy::block_policy(&params, input.len()),
517                )?;
518            }
519        } else {
520            let combined = &prep.combined;
521            let mut rep_offsets = prep.rep_offsets;
522
523            if input.len() <= MAX_BLOCK_SIZE {
524                match params.strategy {
525                    Strategy::Fast => {
526                        fast::compress_fast_block(
527                            combined,
528                            prefix_len,
529                            prefix_len + input.len(),
530                            &params,
531                            &rep_offsets,
532                            &mut self.hash_table,
533                            &mut self.sequences,
534                        );
535                    }
536                    Strategy::DFast => {
537                        dfast::compress_dfast_block(
538                            combined,
539                            prefix_len,
540                            prefix_len + input.len(),
541                            &params,
542                            &rep_offsets,
543                            &mut self.hash_table,
544                            &mut self.hash_long,
545                            &mut self.sequences,
546                        );
547                    }
548                }
549                if params.force_raw_literals {
550                    block_encoder::encode_compressed_block_raw(
551                        input,
552                        &self.sequences,
553                        &mut rep_offsets,
554                        true,
555                        output,
556                        &mut self.workspace,
557                    )?;
558                } else {
559                    block_encoder::encode_compressed_block(
560                        input,
561                        &self.sequences,
562                        &mut rep_offsets,
563                        true,
564                        output,
565                        &mut self.workspace,
566                        strategy::block_policy(&params, input.len()),
567                    )?;
568                }
569            } else {
570                let mut positions = PositionBase::new(params.window_log);
571                let mut offset = 0;
572                while offset < input.len() {
573                    let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
574                    let is_last = offset + chunk_size >= input.len();
575                    let (start, end) = (prefix_len + offset, prefix_len + offset + chunk_size);
576                    if let Some(shift) = positions.advance(start, end) {
577                        rebase::reduce_hash_table(&mut self.hash_table, shift);
578                        rebase::reduce_hash_table(&mut self.hash_long, shift);
579                    }
580                    let base = positions.base();
581                    let src = &combined[base..];
582                    match params.strategy {
583                        Strategy::Fast => {
584                            fast::compress_fast_block(
585                                src,
586                                start - base,
587                                end - base,
588                                &params,
589                                &rep_offsets,
590                                &mut self.hash_table,
591                                &mut self.sequences,
592                            );
593                        }
594                        Strategy::DFast => {
595                            dfast::compress_dfast_block(
596                                src,
597                                start - base,
598                                end - base,
599                                &params,
600                                &rep_offsets,
601                                &mut self.hash_table,
602                                &mut self.hash_long,
603                                &mut self.sequences,
604                            );
605                        }
606                    }
607                    if params.force_raw_literals {
608                        block_encoder::encode_compressed_block_raw(
609                            &input[offset..offset + chunk_size],
610                            &self.sequences,
611                            &mut rep_offsets,
612                            is_last,
613                            output,
614                            &mut self.workspace,
615                        )?;
616                    } else {
617                        block_encoder::encode_compressed_block(
618                            &input[offset..offset + chunk_size],
619                            &self.sequences,
620                            &mut rep_offsets,
621                            is_last,
622                            output,
623                            &mut self.workspace,
624                            strategy::block_policy(&params, input.len()),
625                        )?;
626                    }
627                    offset += chunk_size;
628                }
629            }
630        }
631
632        if self.content_checksum {
633            let hash = xxh64(input, 0);
634            let checksum = (hash & 0xFFFF_FFFF) as u32;
635            output.extend_from_slice(&checksum.to_le_bytes())?;
636        }
637
638        Ok(())
639    }
640
641    fn compress_with_dict_fallback<S: OutputSink>(
642        &mut self,
643        input: &[u8],
644        dict_id: u32,
645        prefix_len: usize,
646        output: &mut S,
647    ) -> Result<(), CompressError> {
648        let prep = self.prepared.as_ref().unwrap();
649        let rep_offsets = prep.rep_offsets;
650        let prefix = &prep.combined[..prefix_len];
651
652        let params = strategy::level_params_with_prefix(self.level, prefix_len, input.len())
653            .expect("level validated at construction");
654        self.workspace.prev_huffman = prep.huf_table.clone();
655        self.workspace.prev_ll = prep.ll_table.clone();
656        self.workspace.prev_of = prep.of_table.clone();
657        self.workspace.prev_ml = prep.ml_table.clone();
658        compress_core(
659            input,
660            params,
661            Some(dict_id),
662            prefix,
663            rep_offsets,
664            &mut self.hash_table,
665            &mut self.hash_long,
666            &mut self.dict_hash,
667            &mut self.sequences,
668            output,
669            &mut self.workspace,
670            &mut self.combined,
671            self.content_checksum,
672        )
673    }
674
675    fn take_or_borrow_output(&mut self) -> Cow<'_, [u8]> {
676        if self.output.len() >= zrip_core::LARGE_OUTPUT_THRESHOLD {
677            Cow::Owned(core::mem::take(&mut self.output))
678        } else {
679            Cow::Borrowed(&self.output)
680        }
681    }
682}
683
684#[allow(clippy::too_many_arguments, clippy::unnecessary_wraps)]
685fn compress_core<S: OutputSink>(
686    input: &[u8],
687    params: LevelParams,
688    dict_id: Option<u32>,
689    prefix: &[u8],
690    init_rep_offsets: [u32; 3],
691    hash_table: &mut Vec<u32>,
692    hash_long: &mut Vec<u32>,
693    dict_hash: &mut Vec<u32>,
694    sequences: &mut Vec<Sequence>,
695    output: &mut S,
696    workspace: &mut BlockEncodeWorkspace,
697    combined: &mut Vec<u8>,
698    content_checksum: bool,
699) -> Result<(), CompressError> {
700    let hash_size = match params.strategy {
701        Strategy::Fast => 1usize << params.hash_log,
702        Strategy::DFast => 1usize << params.chain_log,
703    };
704    let long_size = 1usize << params.hash_log;
705
706    if prefix.is_empty() {
707        workspace.prev_huffman = None;
708        workspace.prev_ll = None;
709        workspace.prev_of = None;
710        workspace.prev_ml = None;
711    }
712
713    output.reserve(input.len() + 32);
714    write_frame_header_with_checksum(
715        output,
716        input.len(),
717        dict_id,
718        params.window_log,
719        content_checksum,
720    )?;
721
722    // Paths that walk the input block by block hash each block right after
723    // compressing it, while its bytes are still in cache.
724    let mut checksum = content_checksum.then(|| Xxh64State::new(0));
725    let mut hashed_blocks = false;
726
727    if input.is_empty() {
728        block_encoder::encode_raw_block(&[], true, output)?;
729    } else {
730        let has_prefix = !prefix.is_empty();
731        let mut rep_offsets = init_rep_offsets;
732        let mut offset = 0;
733        let mut positions = PositionBase::new(params.window_log);
734
735        if hash_table.len() != hash_size {
736            hash_table.resize(hash_size, 0);
737        }
738
739        match params.strategy {
740            Strategy::Fast => {
741                if has_prefix && input.len() <= MAX_BLOCK_SIZE {
742                    if dict_hash.len() != hash_size {
743                        dict_hash.resize(hash_size, 0);
744                    }
745                    fast::compress_fast_with_prefix_reuse(
746                        input,
747                        &params,
748                        &rep_offsets,
749                        prefix,
750                        dict_hash,
751                        hash_table,
752                        sequences,
753                        combined,
754                    );
755                    if params.force_raw_literals {
756                        block_encoder::encode_compressed_block_raw(
757                            input,
758                            sequences,
759                            &mut rep_offsets,
760                            true,
761                            output,
762                            workspace,
763                        )?;
764                    } else {
765                        block_encoder::encode_compressed_block(
766                            input,
767                            sequences,
768                            &mut rep_offsets,
769                            true,
770                            output,
771                            workspace,
772                            strategy::block_policy(&params, input.len()),
773                        )?;
774                    }
775                } else if has_prefix {
776                    combined.clear();
777                    combined.reserve(prefix.len() + input.len());
778                    combined.extend_from_slice(prefix);
779                    combined.extend_from_slice(input);
780                    let plen = prefix.len();
781                    fast::prefill_hash_table(
782                        combined,
783                        plen,
784                        params.hash_log,
785                        params.min_match,
786                        hash_table,
787                    );
788
789                    while offset < input.len() {
790                        let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
791                        let is_last = offset + chunk_size >= input.len();
792                        let (start, end) = (plen + offset, plen + offset + chunk_size);
793                        if let Some(shift) = positions.advance(start, end) {
794                            rebase::reduce_hash_table(hash_table, shift);
795                        }
796                        let base = positions.base();
797                        fast::compress_fast_block(
798                            &combined[base..],
799                            start - base,
800                            end - base,
801                            &params,
802                            &rep_offsets,
803                            hash_table,
804                            sequences,
805                        );
806                        if params.force_raw_literals {
807                            block_encoder::encode_compressed_block_raw(
808                                &input[offset..offset + chunk_size],
809                                sequences,
810                                &mut rep_offsets,
811                                is_last,
812                                output,
813                                workspace,
814                            )?;
815                        } else {
816                            block_encoder::encode_compressed_block(
817                                &input[offset..offset + chunk_size],
818                                sequences,
819                                &mut rep_offsets,
820                                is_last,
821                                output,
822                                workspace,
823                                strategy::block_policy(&params, input.len()),
824                            )?;
825                        }
826                        offset += chunk_size;
827                    }
828                } else {
829                    hash_table.fill(0);
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                if hash_long.len() != long_size {
883                    hash_long.resize(long_size, 0);
884                }
885                if has_prefix && input.len() <= MAX_BLOCK_SIZE {
886                    dfast::compress_dfast_with_prefix_reuse(
887                        input,
888                        &params,
889                        &rep_offsets,
890                        prefix,
891                        hash_table,
892                        hash_long,
893                        sequences,
894                        combined,
895                    );
896                    block_encoder::encode_compressed_block(
897                        input,
898                        sequences,
899                        &mut rep_offsets,
900                        true,
901                        output,
902                        workspace,
903                        strategy::block_policy(&params, input.len()),
904                    )?;
905                } else if has_prefix {
906                    combined.clear();
907                    combined.reserve(prefix.len() + input.len());
908                    combined.extend_from_slice(prefix);
909                    combined.extend_from_slice(input);
910                    let plen = prefix.len();
911                    dfast::prefill_hash_tables(
912                        combined,
913                        plen,
914                        params.hash_log,
915                        params.chain_log,
916                        params.min_match,
917                        hash_table,
918                        hash_long,
919                    );
920
921                    while offset < input.len() {
922                        let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
923                        let is_last = offset + chunk_size >= input.len();
924                        let (start, end) = (plen + offset, plen + offset + chunk_size);
925                        if let Some(shift) = positions.advance(start, end) {
926                            rebase::reduce_hash_table(hash_table, shift);
927                            rebase::reduce_hash_table(hash_long, shift);
928                        }
929                        let base = positions.base();
930                        dfast::compress_dfast_block(
931                            &combined[base..],
932                            start - base,
933                            end - base,
934                            &params,
935                            &rep_offsets,
936                            hash_table,
937                            hash_long,
938                            sequences,
939                        );
940                        block_encoder::encode_compressed_block(
941                            &input[offset..offset + chunk_size],
942                            sequences,
943                            &mut rep_offsets,
944                            is_last,
945                            output,
946                            workspace,
947                            strategy::block_policy(&params, input.len()),
948                        )?;
949                        offset += chunk_size;
950                    }
951                } else {
952                    hash_table.fill(0);
953                    hash_long.fill(0);
954                    while offset < input.len() {
955                        let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
956                        let block_end = offset + chunk_size;
957                        let is_last = block_end >= input.len();
958                        let block = &input[offset..block_end];
959
960                        if let Some(shift) = positions.advance(offset, block_end) {
961                            rebase::reduce_hash_table(hash_table, shift);
962                            rebase::reduce_hash_table(hash_long, shift);
963                        }
964                        if skip_match_search(&params, input.len(), block) {
965                            block_encoder::encode_raw_block(block, is_last, output)?;
966                        } else {
967                            let base = positions.base();
968                            dfast::compress_dfast_block(
969                                &input[base..],
970                                offset - base,
971                                block_end - base,
972                                &params,
973                                &rep_offsets,
974                                hash_table,
975                                hash_long,
976                                sequences,
977                            );
978                            block_encoder::encode_compressed_block(
979                                block,
980                                sequences,
981                                &mut rep_offsets,
982                                is_last,
983                                output,
984                                workspace,
985                                strategy::block_policy(&params, input.len()),
986                            )?;
987                        }
988                        if let Some(c) = checksum.as_mut() {
989                            c.update(block);
990                        }
991                        offset = block_end;
992                    }
993                    hashed_blocks = true;
994                }
995            }
996        }
997    }
998
999    if let Some(c) = checksum {
1000        let hash = if hashed_blocks {
1001            c.finish()
1002        } else {
1003            xxh64(input, 0)
1004        };
1005        output.extend_from_slice(&((hash & 0xFFFF_FFFF) as u32).to_le_bytes())?;
1006    }
1007
1008    Ok(())
1009}