Skip to main content

structured_zstd/encoding/
mod.rs

1//! Zstandard encoder — frame compression, streaming, dictionary support.
2//!
3//! Four entry points cover the common use cases:
4//!
5//! * [`compress`] — one-shot helper that builds a self-contained
6//!   Zstandard frame from a `Read` source to a `Write` sink. The
7//!   input is consumed incrementally from `Read`, so input buffering
8//!   stays bounded; however, the compressed output is buffered in
9//!   memory until the frame is complete so the Frame Content Size
10//!   field can be filled in the header — peak memory is
11//!   `O(compressed_size)` (worst-case `O(input_size)` for
12//!   incompressible payloads, plus a small frame overhead). The
13//!   savings vs [`compress_to_vec`] come from not materialising the
14//!   input alongside the output.
15//! * [`compress_to_vec`] — same one-shot path as [`compress`] but
16//!   the input is eagerly drained into an internal `Vec` first
17//!   (`read_to_end`) so the encoder can be handed a `&[u8]` and a
18//!   precise source-size hint. Peak memory is therefore ≈
19//!   `input_size + output_size`; prefer [`compress`] or
20//!   [`StreamingEncoder`] when the input is large or unbounded.
21//! * [`StreamingEncoder`] — implements [`crate::io::Write`], which
22//!   re-exports [`std::io::Write`] under the `std` feature and falls
23//!   back to a `no_std`-friendly trait otherwise. Accepts bytes
24//!   incrementally and flushes compressed output as blocks fill.
25//!   Requires `set_pledged_content_size` before the first write if
26//!   the Frame Content Size field is to be populated.
27//! * [`FrameCompressor`] — lower-level builder that owns the matcher and
28//!   the per-frame configuration; the streaming and one-shot helpers are
29//!   thin wrappers over it. Reach for it when you need to swap in a custom
30//!   [`Matcher`] implementation or share the matcher across frames.
31//!
32//! Compression intensity is selected via [`CompressionLevel`], which
33//! provides both named presets (`Fastest`, `Default`, `Better`, `Best`) and
34//! numeric levels (`from_level(n)`) that mirror C zstd's level numbering
35//! (negative for ultra-fast, `0` = default, `1..=22` for the standard
36//! range).
37//!
38//! All produced frames are valid RFC 8878 Zstandard streams and decode
39//! through both this crate's [`crate::decoding`] module and upstream C zstd.
40//!
41//! For memory budgeting, [`estimated_compression_workspace_bytes`] reports
42//! the approximate steady-state heap footprint of a one-shot compression at
43//! a given level (window + match-finder tables + block staging).
44
45pub(crate) mod block_header;
46pub(crate) mod blocks;
47pub(crate) mod dict_attach;
48pub(crate) mod fastpath;
49pub(crate) mod frame_header;
50pub(crate) mod incompressible;
51pub(crate) mod match_generator;
52pub(crate) mod util;
53
54// `#111` encoder architecture rewrite. `cost_model`, `opt`,
55// `strategy`, `dfast`, `row`, and `simple` host the relocated
56// cost-model types, the optimal-parser plain-data types, the
57// const-generic [`strategy::Strategy`] trait + per-level [`strategy::
58// StrategyTag`] dispatcher, and the Dfast / Row / Simple matchers
59// respectively. `match_table::helpers` hosts the shared match-finder
60// primitives. The rewrite plan is tracked in
61// <https://github.com/structured-world/structured-zstd/issues/111>;
62// per-phase boundaries are `perf/post-pr-110-baseline` (start),
63// `perf/post-pr-121-baseline` (post-Phase-2).
64pub(crate) mod bt;
65pub(crate) mod cost_model;
66pub(crate) mod dfast;
67pub(crate) mod hc;
68// LDM uses `twox_hash::XxHash64` (per-window XXH64 over the
69// `min_match_length` byte slice, donor `zstd_ldm.c:315`). The
70// `twox-hash` dependency is gated behind the `hash` feature so
71// `default-features = false` builds (no_std, embedded) don't pull
72// it in. `BtMatcher::ldm_producer` and the `cfg(feature = "hash")`
73// blocks inside `BtMatcher::prepare_ldm_candidates` /
74// `BtMatcher::reset` carry the same gate; the call site in
75// `match_generator.rs::start_matching_optimal` invokes
76// `prepare_ldm_candidates` unconditionally because the
77// gating is internal to the method body (under
78// `not(feature = "hash")` the method shrinks to the legacy
79// `ldm_sequences.clear()` stub).
80#[cfg(feature = "hash")]
81pub(crate) mod ldm;
82pub(crate) mod match_table;
83pub(crate) mod opt;
84pub(crate) mod row;
85pub(crate) mod simple;
86pub(crate) mod strategy;
87
88pub(crate) mod frame_compressor;
89#[cfg(feature = "lsm")]
90pub mod frame_emit_info;
91mod levels;
92pub(crate) mod parameters;
93#[cfg(feature = "bench_internals")]
94pub mod sequence_capture;
95mod streaming_encoder;
96pub use frame_compressor::{EncoderDictionary, FrameCompressor};
97#[cfg(feature = "lsm")]
98pub use frame_emit_info::{BlockType, FrameBlock, FrameEmitInfo};
99pub use match_generator::{
100    MatchGeneratorDriver, estimated_bt_strategy_extra_bytes, estimated_compression_workspace_bytes,
101};
102pub use parameters::{
103    Bounds, CParameter, CompressionParameters, CompressionParametersBuilder, ParameterError,
104    Strategy,
105};
106pub use streaming_encoder::StreamingEncoder;
107
108use crate::io::{Read, Write};
109use alloc::vec::Vec;
110
111/// Convenience function to compress some source into a target without reusing any resources of the compressor
112/// ```rust
113/// use structured_zstd::encoding::{compress, CompressionLevel};
114/// let data: &[u8] = &[0,0,0,0,0,0,0,0,0,0,0,0];
115/// let mut target = Vec::new();
116/// compress(data, &mut target, CompressionLevel::Fastest);
117/// ```
118pub fn compress<R: Read, W: Write>(source: R, target: W, level: CompressionLevel) {
119    let mut frame_enc = FrameCompressor::new(level);
120    frame_enc.set_source(source);
121    frame_enc.set_drain(target);
122    frame_enc.compress();
123}
124
125/// Convenience function to compress some source into a Vec without reusing any resources of the compressor.
126///
127/// This helper eagerly buffers the full input (`Read`) before compression so it
128/// can provide a source-size hint to the one-shot encoder path. Peak memory can
129/// therefore be roughly `input_size + output_size`. For very large payloads or
130/// tighter memory budgets, prefer streaming APIs such as [`StreamingEncoder`].
131///
132/// **This is NOT a streaming API.** The source is fully buffered
133/// into a `Vec<u8>` before any compression work begins, so peak input
134/// memory is bounded by `source.len()` (not "constant regardless of
135/// payload size" as a stream-shaped encoder would offer). If the
136/// source is large enough that holding it in memory is not acceptable,
137/// use [`StreamingEncoder`] which consumes chunks incrementally
138/// without the up-front Vec build.
139///
140/// This helper drives `read_to_end` to materialize the full source
141/// into a `Vec<u8>` before forwarding the slice to
142/// [`compress_slice_to_vec`]. For a `Read` whose size is unknown ahead
143/// of time, `read_to_end` grows that input `Vec` via power-of-two
144/// doubling: peak input allocation can be up to 2× the final source
145/// length transiently. The live working set on this entry point is
146/// roughly `input.capacity()` plus the block-accumulation buffer and
147/// per-block scratch carried by [`compress_slice_to_vec`], plus the
148/// exactly-sized output `Vec`. [`StreamingEncoder`] avoids the input
149/// materialization step entirely and is the right entry point when
150/// the source is large or unbounded.
151///
152/// ```rust
153/// use structured_zstd::encoding::{compress_to_vec, CompressionLevel};
154/// let data: &[u8] = &[0,0,0,0,0,0,0,0,0,0,0,0];
155/// let compressed = compress_to_vec(data, CompressionLevel::Fastest);
156/// ```
157pub fn compress_to_vec<R: Read>(source: R, level: CompressionLevel) -> Vec<u8> {
158    let mut source = source;
159    let mut input = Vec::new();
160    source.read_to_end(&mut input).unwrap();
161    compress_slice_to_vec(input.as_slice(), level)
162}
163
164/// Compress a contiguous byte slice into a fresh `Vec<u8>` without the
165/// input-buffering step that [`compress_to_vec`] performs to adapt a
166/// `Read` source.
167///
168/// One-shot wrapper over
169/// [`FrameCompressor::compress_independent_frame`]: the input is read by
170/// reference (the eligible Fast path scans it in place, no per-block
171/// history copy), and the returned `Vec` is allocated exactly once at the
172/// final frame size after compression. Peak transient memory is the
173/// block-accumulation buffer (grown via amortized doubling, ≈ 2× current
174/// compressed size at the last realloc) plus the exactly-sized output. The
175/// worst-case compressed-size bound is never pinned upfront, so a highly
176/// compressible 100 MiB input does not charge ~100 MiB of worst-case
177/// expansion against peak.
178///
179/// To compress many slices, construct one [`FrameCompressor`] and call
180/// [`compress_independent_frame_into`](FrameCompressor::compress_independent_frame_into)
181/// in a loop instead, which reuses the matcher tables, scratch, and output
182/// buffer across frames (this function allocates and primes from scratch
183/// each call).
184///
185/// # Panics
186///
187/// Panics on encoder error (matches the failure surface of
188/// [`compress_to_vec`], which this function backs). Out-of-memory during
189/// the output / per-block scratch allocations is handled by the global
190/// allocator's abort policy. The slice/Vec entry points mirror the donor
191/// `ZSTD_compress` shape (no error return on the bulk path).
192///
193/// ```rust
194/// use structured_zstd::encoding::{compress_slice_to_vec, CompressionLevel};
195/// let data: &[u8] = &[0,0,0,0,0,0,0,0,0,0,0,0];
196/// let compressed = compress_slice_to_vec(data, CompressionLevel::Fastest);
197/// ```
198pub fn compress_slice_to_vec(source: &[u8], level: CompressionLevel) -> Vec<u8> {
199    // Bare `FrameCompressor` resolves all three type params to their
200    // defaults (`&'static [u8]` reader, `Vec<u8>` drain, MatchGeneratorDriver);
201    // neither the reader nor the drain is used by the in-place
202    // `compress_independent_frame` path.
203    let mut enc: FrameCompressor = FrameCompressor::new(level);
204    enc.compress_independent_frame(source)
205}
206
207/// Worst-case compressed-frame size for an input of `src_size` bytes.
208///
209/// A destination buffer of this size is always large enough to hold the
210/// output of [`compress_slice_to_vec`] (or any single-frame compression) for
211/// an input of `src_size` bytes, so a caller sizing a fixed buffer once (the
212/// shape the C `ZSTD_compress` entry point needs) never has to grow it.
213///
214/// Mirrors the upstream `ZSTD_COMPRESSBOUND` formula exactly:
215/// `src_size + (src_size >> 8) + margin`, where `margin` is
216/// `(128 KiB - src_size) >> 11` for inputs below 128 KiB and `0` otherwise.
217/// The margin guarantees `bound(a) + bound(b) <= bound(a + b)` for blocks of
218/// at least 128 KiB, which keeps multi-frame concatenation sizing sound.
219///
220/// Saturates at [`usize::MAX`] if the formula would overflow on a
221/// pathologically large `src_size` — no allocation that large can exist, so
222/// the saturated value is the correct "cannot fit" sentinel rather than a
223/// masked wrap.
224///
225/// ```rust
226/// use structured_zstd::encoding::{compress_bound, compress_slice_to_vec, CompressionLevel};
227/// let data = [7u8; 4096];
228/// assert!(compress_slice_to_vec(&data, CompressionLevel::Default).len() <= compress_bound(data.len()));
229/// ```
230pub const fn compress_bound(src_size: usize) -> usize {
231    const LOWER: usize = 128 * 1024;
232    let margin = if src_size < LOWER {
233        (LOWER - src_size) >> 11
234    } else {
235        0
236    };
237    // Saturating is the correct UPPER-BOUND semantic here, not a masked bug:
238    // this is a public API over an arbitrary `usize`, and the largest meaningful
239    // bound is `usize::MAX`. A real slice is at most `isize::MAX` bytes, so the
240    // `* 1.004 + margin` cannot overflow for genuine inputs; the saturation only
241    // caps a pathological caller-supplied size at the representable ceiling.
242    src_size
243        .saturating_add(src_size >> 8)
244        .saturating_add(margin)
245}
246
247/// Compress a byte slice into a fresh `Vec<u8>` using fine-grained
248/// [`CompressionParameters`] (#27) instead of a bare
249/// [`CompressionLevel`].
250///
251/// One-shot wrapper over [`FrameCompressor::set_parameters`] +
252/// [`FrameCompressor::compress_independent_frame`]. The produced frame is
253/// a valid RFC 8878 stream regardless of the knobs chosen.
254///
255/// ```rust
256/// use structured_zstd::encoding::{
257///     compress_with_parameters, CompressionLevel, CompressionParameters, Strategy,
258/// };
259/// let data: &[u8] = b"the quick brown fox jumps over the lazy dog";
260/// let params = CompressionParameters::builder(CompressionLevel::Level(5))
261///     .strategy(Strategy::Greedy)
262///     .build()
263///     .unwrap();
264/// let compressed = compress_with_parameters(data, &params);
265/// assert!(!compressed.is_empty());
266/// ```
267pub fn compress_with_parameters(source: &[u8], params: &CompressionParameters) -> Vec<u8> {
268    let mut enc: FrameCompressor = FrameCompressor::new(params.level());
269    enc.set_parameters(params);
270    enc.compress_independent_frame(source)
271}
272
273/// The compression mode used impacts the speed of compression,
274/// and resulting compression ratios. Faster compression will result
275/// in worse compression ratios, and vice versa.
276#[derive(Copy, Clone, Debug, PartialEq, Eq)]
277pub enum CompressionLevel {
278    /// This level does not compress the data at all, and simply wraps
279    /// it in a Zstandard frame.
280    Uncompressed,
281    /// This level is roughly equivalent to Zstd compression level 1
282    Fastest,
283    /// This level uses the crate's dedicated `dfast`-style matcher to
284    /// target a better speed/ratio tradeoff than [`CompressionLevel::Fastest`].
285    ///
286    /// It represents this crate's "default" compression setting and may
287    /// evolve in future versions as the implementation moves closer to
288    /// reference zstd level 3 behavior.
289    Default,
290    /// This level is roughly equivalent to Zstd level 7.
291    ///
292    /// Uses the hash-chain matcher with a lazy2 matching strategy: the encoder
293    /// evaluates up to two positions ahead before committing to a match,
294    /// trading speed for a better compression ratio than [`CompressionLevel::Default`].
295    Better,
296    /// This level is roughly equivalent to Zstd level 11.
297    ///
298    /// Uses the hash-chain matcher with a deep lazy2 matching strategy and
299    /// a 16 MiB window. Compared to [`CompressionLevel::Better`], this level
300    /// uses larger hash and chain tables (2 M / 1 M entries vs 1 M / 512 K),
301    /// a deeper search (32 candidates vs 16), and a higher target match
302    /// length (128 vs 48), trading speed for the best compression ratio
303    /// available in this crate.
304    Best,
305    /// Numeric compression level.
306    ///
307    /// Levels 1–22 correspond to the C zstd level numbering.  Higher values
308    /// produce smaller output at the cost of more CPU time.  Negative values
309    /// select ultra-fast modes that trade ratio for speed.  Level 0 is
310    /// treated as [`DEFAULT_LEVEL`](Self::DEFAULT_LEVEL), matching C zstd
311    /// semantics.
312    ///
313    /// Named variants map to specific numeric levels:
314    /// [`Fastest`](Self::Fastest) = 1, [`Default`](Self::Default) = 3,
315    /// [`Better`](Self::Better) = 7, [`Best`](Self::Best) = 11.
316    /// [`Best`](Self::Best) remains the highest-ratio named preset, but
317    /// [`Level`](Self::Level) values above 11 can target stronger (slower)
318    /// tuning than the named hierarchy.
319    ///
320    /// Levels above 11 use progressively larger windows and deeper search.
321    /// Levels 16–17 use a `btopt`-style price parser, 18–19 use `btultra`,
322    /// and 20–22 use a `btultra2`-style two-pass selection profile.
323    ///
324    /// Semver note: this variant was added after the initial enum shape and
325    /// is a breaking API change for downstream crates that exhaustively
326    /// `match` on [`CompressionLevel`] without a wildcard arm.
327    Level(i32),
328}
329
330impl CompressionLevel {
331    /// The minimum supported numeric compression level (ultra-fast mode).
332    pub const MIN_LEVEL: i32 = -131072;
333    /// The maximum supported numeric compression level.
334    pub const MAX_LEVEL: i32 = 22;
335    /// The default numeric compression level (equivalent to [`Default`](Self::Default)).
336    pub const DEFAULT_LEVEL: i32 = 3;
337
338    /// Create a compression level from a numeric value.
339    ///
340    /// Returns named variants for canonical levels (`0`/`3`, `1`, `7`, `11`)
341    /// and [`Level`](Self::Level) for all other values.
342    ///
343    /// With the default matcher backend (`MatchGeneratorDriver`), values
344    /// outside [`MIN_LEVEL`](Self::MIN_LEVEL)..=[`MAX_LEVEL`](Self::MAX_LEVEL)
345    /// are silently clamped during built-in level parameter resolution.
346    pub const fn from_level(level: i32) -> Self {
347        match level {
348            0 | Self::DEFAULT_LEVEL => Self::Default,
349            1 => Self::Fastest,
350            7 => Self::Better,
351            11 => Self::Best,
352            _ => Self::Level(level),
353        }
354    }
355}
356
357/// Trait used by the encoder that users can use to extend the matching facilities with their own algorithm
358/// making their own tradeoffs between runtime, memory usage and compression ratio
359///
360/// This trait operates on buffers that represent the chunks of data the matching algorithm wants to work on.
361/// Each one of these buffers is referred to as a *space*. One or more of these buffers represent the window
362/// the decoder will need to decode the data again.
363///
364/// This library asks the Matcher for a new buffer using `get_next_space` to allow reusing of allocated buffers when they are no longer part of the
365/// window of data that is being used for matching.
366///
367/// The library fills the buffer with data that is to be compressed and commits them back to the matcher using `commit_space`.
368///
369/// Then it will either call `start_matching` or, if the space is deemed not worth compressing, `skip_matching` is called.
370///
371/// This is repeated until no more data is left to be compressed.
372pub trait Matcher {
373    /// Get a space where we can put data to be matched on. Will be encoded as one block. The maximum allowed size is 128 kB.
374    fn get_next_space(&mut self) -> alloc::vec::Vec<u8>;
375    /// Get a reference to the last committed space
376    fn get_last_space(&mut self) -> &[u8];
377    /// Commit a space to the matcher so it can be matched against
378    fn commit_space(&mut self, space: alloc::vec::Vec<u8>);
379    /// Just process the data in the last committed space for future matching.
380    fn skip_matching(&mut self);
381    /// Hint-aware skip path used internally to thread a precomputed block
382    /// incompressibility verdict to matcher backends.
383    ///
384    /// Default implementation preserves backwards compatibility for external
385    /// custom matchers by delegating to [`skip_matching`](Self::skip_matching).
386    fn skip_matching_with_hint(&mut self, _incompressible_hint: Option<bool>) {
387        self.skip_matching();
388    }
389    /// Process the data in the last committed space for future matching AND generate matches for the data
390    fn start_matching(&mut self, handle_sequence: impl for<'a> FnMut(Sequence<'a>));
391    /// Reset this matcher so it can be used for the next new frame
392    fn reset(&mut self, level: CompressionLevel);
393    /// Provide a hint about the total uncompressed size for the next frame.
394    ///
395    /// Implementations may use this to select smaller hash tables and windows
396    /// for small inputs, matching the C zstd source-size-class behavior.
397    /// Called before [`reset`](Self::reset) when the caller knows the input
398    /// size (e.g. from pledged content size or file metadata).
399    ///
400    /// The default implementation is a no-op for custom matchers and
401    /// test stubs. The built-in runtime matcher (`MatchGeneratorDriver`)
402    /// overrides this hook and applies the hint during level resolution.
403    fn set_source_size_hint(&mut self, _size: u64) {}
404    /// Hint the byte size of the dictionary that will be primed into the next
405    /// frame. The built-in runtime matcher uses it to size the binary-tree /
406    /// hash-chain match-finder tables from the dictionary's cParams tier rather
407    /// than the source window (donor CDict economics), while keeping the
408    /// eviction window source-sized. Default no-op for custom matchers and test
409    /// stubs; consumed at the next [`reset`](Self::reset).
410    fn set_dictionary_size_hint(&mut self, _size: usize) {}
411    /// Drop any per-frame fine-grained parameter overrides installed via
412    /// the public parameter API, reverting to plain level-based geometry
413    /// at the next [`reset`](Self::reset). Called by
414    /// [`FrameCompressor::set_compression_level`](crate::encoding::FrameCompressor::set_compression_level)
415    /// so switching back to a bare level after a customized frame does not
416    /// keep the old overrides sticky. Default no-op for custom matchers.
417    fn clear_param_overrides(&mut self) {}
418    /// Prime matcher state with dictionary history before compressing the next frame.
419    /// Default implementation is a no-op for custom matchers that do not support this.
420    fn prime_with_dictionary(&mut self, _dict_content: &[u8], _offset_hist: [u32; 3]) {}
421    /// CDict-equivalent fast path for repeated frames sharing one dictionary.
422    /// Restore the matcher state captured by [`Self::capture_primed_dictionary`]
423    /// at the SAME level (a table copy) instead of re-running
424    /// [`Self::prime_with_dictionary`] (which re-hashes every dictionary
425    /// position). Returns `true` when a matching snapshot was restored;
426    /// `false` (the default) means the caller must prime then capture.
427    fn restore_primed_dictionary(&mut self, _level: CompressionLevel) -> bool {
428        false
429    }
430    /// Snapshot the post-prime matcher state for the given level so later
431    /// frames can [`Self::restore_primed_dictionary`] it. Default no-op.
432    fn capture_primed_dictionary(&mut self, _level: CompressionLevel) {}
433    /// Drop any captured prime snapshot (dictionary or level changed).
434    /// Default no-op.
435    fn invalidate_primed_dictionary(&mut self) {}
436    /// Seed matcher cost model with dictionary entropy tables before the next frame.
437    /// Default implementation is a no-op for custom matchers.
438    fn seed_dictionary_entropy(
439        &mut self,
440        _huff: Option<&crate::huff0::huff0_encoder::HuffmanTable>,
441        _ll: Option<&crate::fse::fse_encoder::FSETable>,
442        _ml: Option<&crate::fse::fse_encoder::FSETable>,
443        _of: Option<&crate::fse::fse_encoder::FSETable>,
444    ) {
445    }
446    /// Returns whether this matcher can consume dictionary priming state and produce
447    /// dictionary-dependent sequences. Defaults to `false` for custom matchers.
448    fn supports_dictionary_priming(&self) -> bool {
449        false
450    }
451    /// Heap bytes this matcher's allocations hold (tables, history, scratch),
452    /// excluding the inline struct itself. Lets a context report its true
453    /// footprint via `ZSTD_sizeof_CCtx`. Defaults to `0` for custom matchers.
454    fn heap_size(&self) -> usize {
455        0
456    }
457    /// The size of the window the decoder will need to execute all sequences produced by this matcher.
458    ///
459    /// Must return a positive (non-zero) value; returning 0 causes
460    /// [`StreamingEncoder`] to reject the first write with an invalid-input error
461    /// (`InvalidInput` with `std`, `Other` with `no_std`).
462    ///
463    /// Must remain stable for the lifetime of a frame.
464    /// It may change only after `reset()` is called for the next frame
465    /// (for example because the compression level changed).
466    fn window_size(&self) -> u64;
467}
468
469#[derive(PartialEq, Eq, Debug)]
470/// Sequences that a [`Matcher`] can produce
471pub enum Sequence<'data> {
472    /// Is encoded as a sequence for the decoder sequence execution.
473    ///
474    /// First the literals will be copied to the decoded data,
475    /// then `match_len` bytes are copied from `offset` bytes back in the decoded data
476    Triple {
477        literals: &'data [u8],
478        offset: usize,
479        match_len: usize,
480    },
481    /// This is returned as the last sequence in a block
482    ///
483    /// These literals will just be copied at the end of the sequence execution by the decoder
484    Literals { literals: &'data [u8] },
485}
486
487#[cfg(test)]
488mod compress_bound_tests {
489    use super::{CompressionLevel, compress_bound, compress_slice_to_vec};
490
491    #[test]
492    fn matches_upstream_formula_below_threshold() {
493        // src_size + (src_size >> 8) + ((128 KiB - src_size) >> 11).
494        assert_eq!(compress_bound(0), 64);
495        assert_eq!(compress_bound(4096), 4096 + 16 + 62);
496    }
497
498    #[test]
499    fn drops_margin_at_and_above_threshold() {
500        let lower = 128 * 1024;
501        assert_eq!(compress_bound(lower), lower + (lower >> 8));
502        assert_eq!(compress_bound(lower + 1), (lower + 1) + ((lower + 1) >> 8));
503    }
504
505    #[test]
506    fn saturates_instead_of_wrapping() {
507        // No allocation this large can exist; the ceiling is the right sentinel.
508        assert_eq!(compress_bound(usize::MAX), usize::MAX);
509    }
510
511    #[test]
512    fn always_fits_real_compressed_output() {
513        for len in [0usize, 1, 100, 4096, 200_000] {
514            let data = alloc::vec![7u8; len];
515            let out = compress_slice_to_vec(&data, CompressionLevel::Default);
516            assert!(out.len() <= compress_bound(len), "len={len}");
517        }
518    }
519}