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