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