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 params =
357            strategy::level_params_with_prefix(self.level, dict.content().len(), input.len())
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 params = strategy::level_params_with_prefix(self.level, prep.prefix_len, input.len())
397            .expect("level validated at construction");
398
399        let dict_id = prep.dict_id;
400        let prefix_len = prep.prefix_len;
401        let snapshot_matches = match params.strategy {
402            Strategy::Fast => (1usize << params.hash_log) == prep.hash_snapshot.len(),
403            Strategy::DFast => {
404                (1usize << params.chain_log) == prep.hash_snapshot.len()
405                    && (1usize << params.hash_log) == prep.hash_long_snapshot.len()
406            }
407        };
408        // A one-block input also attaches when the snapshot is sized for
409        // another tier: that beats prefilling a table per call.
410        let attached = (params.strategy == Strategy::Fast
411            && !input.is_empty()
412            && input.len() <= MAX_BLOCK_SIZE
413            && (input.len() <= ATTACH_THRESHOLD || !snapshot_matches))
414            .then(|| prep.attached_table(params.min_match))
415            .flatten();
416        let use_attached = attached.is_some();
417
418        if !use_attached && !snapshot_matches {
419            return self.compress_with_dict_fallback(input, dict_id, prefix_len, output);
420        }
421
422        {
423            let prep = self.prepared.as_mut().unwrap();
424            if !use_attached {
425                // The per-call prefix path resizes these tables for its own
426                // parameter tier, so restore length and contents together.
427                self.hash_table.clear();
428                self.hash_table.extend_from_slice(&prep.hash_snapshot);
429                if !prep.hash_long_snapshot.is_empty() {
430                    self.hash_long.clear();
431                    self.hash_long.extend_from_slice(&prep.hash_long_snapshot);
432                }
433            }
434            prep.combined.truncate(prep.prefix_len);
435            prep.combined.extend_from_slice(input);
436        }
437
438        let prep = self.prepared.as_ref().unwrap();
439
440        if use_attached {
441            self.workspace.prev_huffman = if params.force_raw_literals {
442                None
443            } else {
444                prep.huf_table.clone()
445            };
446        } else if let Some(ref huf) = prep.huf_table {
447            self.workspace.prev_huffman = Some(huf.clone());
448        } else {
449            self.workspace.prev_huffman = None;
450        }
451        self.workspace.prev_ll = prep.ll_table.clone();
452        self.workspace.prev_of = prep.of_table.clone();
453        self.workspace.prev_ml = prep.ml_table.clone();
454
455        output.reserve(input.len() + 32);
456        write_frame_header_with_checksum(
457            output,
458            input.len(),
459            Some(dict_id),
460            params.window_log,
461            self.content_checksum,
462        )?;
463
464        if input.is_empty() {
465            block_encoder::encode_raw_block(&[], true, output)?;
466        } else if use_attached {
467            let input_hash_log = if input.len() >= 2 {
468                let src_log = 32 - ((input.len() as u32) - 1).leading_zeros();
469                params.hash_log.min(src_log).max(strategy::HASH_LOG_MIN)
470            } else {
471                strategy::HASH_LOG_MIN
472            };
473            let input_hash_size = 1usize << input_hash_log;
474            self.small_hash.resize(input_hash_size, 0);
475            self.small_hash.fill(0);
476
477            let mut rep_offsets = prep.rep_offsets;
478            let dict = AttachedDict {
479                table: prep.attached_table(params.min_match).unwrap(),
480                hash_log: prep.attached_hash_log,
481                prefix_len,
482            };
483            let input_params = LevelParams {
484                hash_log: input_hash_log,
485                ..params
486            };
487
488            fast::compress_fast_block_attached(
489                &prep.combined,
490                prefix_len + input.len(),
491                &input_params,
492                &prep.rep_offsets,
493                dict,
494                &mut self.small_hash,
495                &mut self.sequences,
496            );
497
498            if params.force_raw_literals {
499                block_encoder::encode_compressed_block_raw(
500                    input,
501                    &self.sequences,
502                    &mut rep_offsets,
503                    true,
504                    output,
505                    &mut self.workspace,
506                )?;
507            } else {
508                block_encoder::encode_compressed_block(
509                    input,
510                    &self.sequences,
511                    &mut rep_offsets,
512                    true,
513                    output,
514                    &mut self.workspace,
515                    strategy::block_policy(&params, input.len()),
516                )?;
517            }
518        } else {
519            let combined = &prep.combined;
520            let mut rep_offsets = prep.rep_offsets;
521
522            if input.len() <= MAX_BLOCK_SIZE {
523                match params.strategy {
524                    Strategy::Fast => {
525                        fast::compress_fast_block(
526                            combined,
527                            prefix_len,
528                            prefix_len + input.len(),
529                            &params,
530                            &rep_offsets,
531                            &mut self.hash_table,
532                            &mut self.sequences,
533                        );
534                    }
535                    Strategy::DFast => {
536                        dfast::compress_dfast_block(
537                            combined,
538                            prefix_len,
539                            prefix_len + input.len(),
540                            &params,
541                            &rep_offsets,
542                            &mut self.hash_table,
543                            &mut self.hash_long,
544                            &mut self.sequences,
545                        );
546                    }
547                }
548                if params.force_raw_literals {
549                    block_encoder::encode_compressed_block_raw(
550                        input,
551                        &self.sequences,
552                        &mut rep_offsets,
553                        true,
554                        output,
555                        &mut self.workspace,
556                    )?;
557                } else {
558                    block_encoder::encode_compressed_block(
559                        input,
560                        &self.sequences,
561                        &mut rep_offsets,
562                        true,
563                        output,
564                        &mut self.workspace,
565                        strategy::block_policy(&params, input.len()),
566                    )?;
567                }
568            } else {
569                let mut offset = 0;
570                while offset < input.len() {
571                    let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
572                    let is_last = offset + chunk_size >= input.len();
573                    match params.strategy {
574                        Strategy::Fast => {
575                            fast::compress_fast_block(
576                                combined,
577                                prefix_len + offset,
578                                prefix_len + offset + chunk_size,
579                                &params,
580                                &rep_offsets,
581                                &mut self.hash_table,
582                                &mut self.sequences,
583                            );
584                        }
585                        Strategy::DFast => {
586                            dfast::compress_dfast_block(
587                                combined,
588                                prefix_len + offset,
589                                prefix_len + offset + chunk_size,
590                                &params,
591                                &rep_offsets,
592                                &mut self.hash_table,
593                                &mut self.hash_long,
594                                &mut self.sequences,
595                            );
596                        }
597                    }
598                    if params.force_raw_literals {
599                        block_encoder::encode_compressed_block_raw(
600                            &input[offset..offset + chunk_size],
601                            &self.sequences,
602                            &mut rep_offsets,
603                            is_last,
604                            output,
605                            &mut self.workspace,
606                        )?;
607                    } else {
608                        block_encoder::encode_compressed_block(
609                            &input[offset..offset + chunk_size],
610                            &self.sequences,
611                            &mut rep_offsets,
612                            is_last,
613                            output,
614                            &mut self.workspace,
615                            strategy::block_policy(&params, input.len()),
616                        )?;
617                    }
618                    offset += chunk_size;
619                }
620            }
621        }
622
623        if self.content_checksum {
624            let hash = xxh64(input, 0);
625            let checksum = (hash & 0xFFFF_FFFF) as u32;
626            output.extend_from_slice(&checksum.to_le_bytes())?;
627        }
628
629        Ok(())
630    }
631
632    fn compress_with_dict_fallback<S: OutputSink>(
633        &mut self,
634        input: &[u8],
635        dict_id: u32,
636        prefix_len: usize,
637        output: &mut S,
638    ) -> Result<(), CompressError> {
639        let prep = self.prepared.as_ref().unwrap();
640        let rep_offsets = prep.rep_offsets;
641        let prefix = &prep.combined[..prefix_len];
642
643        let params = strategy::level_params_with_prefix(self.level, prefix_len, input.len())
644            .expect("level validated at construction");
645        self.workspace.prev_huffman = prep.huf_table.clone();
646        self.workspace.prev_ll = prep.ll_table.clone();
647        self.workspace.prev_of = prep.of_table.clone();
648        self.workspace.prev_ml = prep.ml_table.clone();
649        compress_core(
650            input,
651            params,
652            Some(dict_id),
653            prefix,
654            rep_offsets,
655            &mut self.hash_table,
656            &mut self.hash_long,
657            &mut self.dict_hash,
658            &mut self.sequences,
659            output,
660            &mut self.workspace,
661            &mut self.combined,
662            self.content_checksum,
663        )
664    }
665
666    fn take_or_borrow_output(&mut self) -> Cow<'_, [u8]> {
667        if self.output.len() >= zrip_core::LARGE_OUTPUT_THRESHOLD {
668            Cow::Owned(core::mem::take(&mut self.output))
669        } else {
670            Cow::Borrowed(&self.output)
671        }
672    }
673}
674
675#[allow(clippy::too_many_arguments, clippy::unnecessary_wraps)]
676fn compress_core<S: OutputSink>(
677    input: &[u8],
678    params: LevelParams,
679    dict_id: Option<u32>,
680    prefix: &[u8],
681    init_rep_offsets: [u32; 3],
682    hash_table: &mut Vec<u32>,
683    hash_long: &mut Vec<u32>,
684    dict_hash: &mut Vec<u32>,
685    sequences: &mut Vec<Sequence>,
686    output: &mut S,
687    workspace: &mut BlockEncodeWorkspace,
688    combined: &mut Vec<u8>,
689    content_checksum: bool,
690) -> Result<(), CompressError> {
691    let hash_size = match params.strategy {
692        Strategy::Fast => 1usize << params.hash_log,
693        Strategy::DFast => 1usize << params.chain_log,
694    };
695    let long_size = 1usize << params.hash_log;
696
697    if prefix.is_empty() {
698        workspace.prev_huffman = None;
699        workspace.prev_ll = None;
700        workspace.prev_of = None;
701        workspace.prev_ml = None;
702    }
703
704    output.reserve(input.len() + 32);
705    write_frame_header_with_checksum(
706        output,
707        input.len(),
708        dict_id,
709        params.window_log,
710        content_checksum,
711    )?;
712
713    // Paths that walk the input block by block hash each block right after
714    // compressing it, while its bytes are still in cache.
715    let mut checksum = content_checksum.then(|| Xxh64State::new(0));
716    let mut hashed_blocks = false;
717
718    if input.is_empty() {
719        block_encoder::encode_raw_block(&[], true, output)?;
720    } else {
721        let has_prefix = !prefix.is_empty();
722        let mut rep_offsets = init_rep_offsets;
723        let mut offset = 0;
724
725        if hash_table.len() != hash_size {
726            hash_table.resize(hash_size, 0);
727        }
728
729        match params.strategy {
730            Strategy::Fast => {
731                if has_prefix && input.len() <= MAX_BLOCK_SIZE {
732                    if dict_hash.len() != hash_size {
733                        dict_hash.resize(hash_size, 0);
734                    }
735                    fast::compress_fast_with_prefix_reuse(
736                        input,
737                        &params,
738                        &rep_offsets,
739                        prefix,
740                        dict_hash,
741                        hash_table,
742                        sequences,
743                        combined,
744                    );
745                    if params.force_raw_literals {
746                        block_encoder::encode_compressed_block_raw(
747                            input,
748                            sequences,
749                            &mut rep_offsets,
750                            true,
751                            output,
752                            workspace,
753                        )?;
754                    } else {
755                        block_encoder::encode_compressed_block(
756                            input,
757                            sequences,
758                            &mut rep_offsets,
759                            true,
760                            output,
761                            workspace,
762                            strategy::block_policy(&params, input.len()),
763                        )?;
764                    }
765                } else if has_prefix {
766                    combined.clear();
767                    combined.reserve(prefix.len() + input.len());
768                    combined.extend_from_slice(prefix);
769                    combined.extend_from_slice(input);
770                    let plen = prefix.len();
771                    fast::prefill_hash_table(
772                        combined,
773                        plen,
774                        params.hash_log,
775                        params.min_match,
776                        hash_table,
777                    );
778
779                    while offset < input.len() {
780                        let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
781                        let is_last = offset + chunk_size >= input.len();
782                        fast::compress_fast_block(
783                            combined,
784                            plen + offset,
785                            plen + offset + chunk_size,
786                            &params,
787                            &rep_offsets,
788                            hash_table,
789                            sequences,
790                        );
791                        if params.force_raw_literals {
792                            block_encoder::encode_compressed_block_raw(
793                                &input[offset..offset + chunk_size],
794                                sequences,
795                                &mut rep_offsets,
796                                is_last,
797                                output,
798                                workspace,
799                            )?;
800                        } else {
801                            block_encoder::encode_compressed_block(
802                                &input[offset..offset + chunk_size],
803                                sequences,
804                                &mut rep_offsets,
805                                is_last,
806                                output,
807                                workspace,
808                                strategy::block_policy(&params, input.len()),
809                            )?;
810                        }
811                        offset += chunk_size;
812                    }
813                } else {
814                    hash_table.fill(0);
815                    while offset < input.len() {
816                        let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
817                        let block_end = offset + chunk_size;
818                        let is_last = block_end >= input.len();
819                        let block = &input[offset..block_end];
820
821                        if skip_match_search(&params, input.len(), block) {
822                            block_encoder::encode_raw_block(block, is_last, output)?;
823                        } else {
824                            fast::compress_fast_block(
825                                input,
826                                offset,
827                                block_end,
828                                &params,
829                                &rep_offsets,
830                                hash_table,
831                                sequences,
832                            );
833                            if params.force_raw_literals {
834                                block_encoder::encode_compressed_block_raw(
835                                    block,
836                                    sequences,
837                                    &mut rep_offsets,
838                                    is_last,
839                                    output,
840                                    workspace,
841                                )?;
842                            } else {
843                                block_encoder::encode_compressed_block(
844                                    block,
845                                    sequences,
846                                    &mut rep_offsets,
847                                    is_last,
848                                    output,
849                                    workspace,
850                                    strategy::block_policy(&params, input.len()),
851                                )?;
852                            }
853                        }
854                        if let Some(c) = checksum.as_mut() {
855                            c.update(block);
856                        }
857                        offset = block_end;
858                    }
859                    hashed_blocks = true;
860                }
861            }
862            Strategy::DFast => {
863                if hash_long.len() != long_size {
864                    hash_long.resize(long_size, 0);
865                }
866                if has_prefix && input.len() <= MAX_BLOCK_SIZE {
867                    dfast::compress_dfast_with_prefix_reuse(
868                        input,
869                        &params,
870                        &rep_offsets,
871                        prefix,
872                        hash_table,
873                        hash_long,
874                        sequences,
875                        combined,
876                    );
877                    block_encoder::encode_compressed_block(
878                        input,
879                        sequences,
880                        &mut rep_offsets,
881                        true,
882                        output,
883                        workspace,
884                        strategy::block_policy(&params, input.len()),
885                    )?;
886                } else if has_prefix {
887                    combined.clear();
888                    combined.reserve(prefix.len() + input.len());
889                    combined.extend_from_slice(prefix);
890                    combined.extend_from_slice(input);
891                    let plen = prefix.len();
892                    dfast::prefill_hash_tables(
893                        combined,
894                        plen,
895                        params.hash_log,
896                        params.chain_log,
897                        params.min_match,
898                        hash_table,
899                        hash_long,
900                    );
901
902                    while offset < input.len() {
903                        let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
904                        let is_last = offset + chunk_size >= input.len();
905                        dfast::compress_dfast_block(
906                            combined,
907                            plen + offset,
908                            plen + offset + chunk_size,
909                            &params,
910                            &rep_offsets,
911                            hash_table,
912                            hash_long,
913                            sequences,
914                        );
915                        block_encoder::encode_compressed_block(
916                            &input[offset..offset + chunk_size],
917                            sequences,
918                            &mut rep_offsets,
919                            is_last,
920                            output,
921                            workspace,
922                            strategy::block_policy(&params, input.len()),
923                        )?;
924                        offset += chunk_size;
925                    }
926                } else {
927                    hash_table.fill(0);
928                    hash_long.fill(0);
929                    while offset < input.len() {
930                        let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
931                        let block_end = offset + chunk_size;
932                        let is_last = block_end >= input.len();
933                        let block = &input[offset..block_end];
934
935                        if skip_match_search(&params, input.len(), block) {
936                            block_encoder::encode_raw_block(block, is_last, output)?;
937                        } else {
938                            dfast::compress_dfast_block(
939                                input,
940                                offset,
941                                block_end,
942                                &params,
943                                &rep_offsets,
944                                hash_table,
945                                hash_long,
946                                sequences,
947                            );
948                            block_encoder::encode_compressed_block(
949                                block,
950                                sequences,
951                                &mut rep_offsets,
952                                is_last,
953                                output,
954                                workspace,
955                                strategy::block_policy(&params, input.len()),
956                            )?;
957                        }
958                        if let Some(c) = checksum.as_mut() {
959                            c.update(block);
960                        }
961                        offset = block_end;
962                    }
963                    hashed_blocks = true;
964                }
965            }
966        }
967    }
968
969    if let Some(c) = checksum {
970        let hash = if hashed_blocks {
971            c.finish()
972        } else {
973            xxh64(input, 0)
974        };
975        output.extend_from_slice(&((hash & 0xFFFF_FFFF) as u32).to_le_bytes())?;
976    }
977
978    Ok(())
979}