zstd-native 0.1.2+zstd.1.5.7

Fastest pure Rust Zstandard (zstd) compression and decompression: byte-identical with libzstd 1.5.7 and faster than C. No C code, no_std.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! Compression: a port of libzstd 1.5.7's compressor whose output is
//! byte-identical to libzstd's for the same input and parameters (issue #6).
//!
//! Layout (each module ports the named libzstd file; function names in
//! comments refer to libzstd):
//! - `bitwriter`: `BIT_CStream_t` (common/bitstream.h)
//! - `hist`: compress/hist.c
//! - `fse_enc`: compress/fse_compress.c (+ FSE encoder parts of common/fse.h)
//! - `huf_enc`: compress/huf_compress.c
//! - `literals`: compress/zstd_compress_literals.c
//! - `sequences`: compress/zstd_compress_sequences.c
//! - `compress`: blocks and frames from compress/zstd_compress.c
//! - `seq_store`: `SeqStore_t`, `ZSTD_storeSeq`, `ZSTD_count`, hashing
//!   (compress/zstd_compress_internal.h)
//! - `params`: compression levels and parameter adjustment (clevels.h,
//!   `ZSTD_getCParams_internal`, `ZSTD_adjustCParams_internal`)
//! - `fast`: compress/zstd_fast.c (noDict, extDict and dictMatchState
//!   variants; the window)
//! - `dfast`: compress/zstd_double_fast.c (noDict, extDict, dictMatchState)
//! - `lazy`: compress/zstd_lazy.c (greedy, lazy, lazy2, btlazy2: parser,
//!   hash chain and binary tree; noDict, extDict and dictMatchState)
//! - `row`: compress/zstd_lazy.c (the row-based match finder)
//! - `opt`: compress/zstd_opt.c (btopt, btultra, btultra2: price model,
//!   binary tree match finder, optimal parser, dictionary statistics;
//!   noDict, extDict and dictMatchState)
//! - `presplit`: compress/zstd_preSplit.c
//! - `split`: the post-block splitter from compress/zstd_compress.c
//!   (`ZSTD_compressBlock_splitBlock`)
//! - `stream`: streaming compression from compress/zstd_compress.c
//!   (`ZSTD_compressStream2`, `ZSTD_compressStream_generic` in buffered
//!   mode, `ZSTD_CCtx_init_compressStream2`); the input ring buffer makes
//!   the window non-contiguous, which the match finders' extDict variants
//!   handle
//! - `cdict`: dictionaries (`ZSTD_CDict`, `ZSTD_createCDict`,
//!   `ZSTD_compress_insertDictionary`, `ZSTD_loadCEntropy`,
//!   `ZSTD_loadDictionaryContent`, `ZSTD_resetCCtx_usingCDict`): loading,
//!   CDicts with tagged tables, attaching or copying them, the parameters
//!   with a dictionary
//! - `ldm`: long distance matching, compress/zstd_ldm.c (gear rolling
//!   hash, long match generation, `ZSTD_ldm_blockCompress`), which libzstd
//!   enables by itself for `btopt` and above with a 128 MiB window; the
//!   optimal parser takes its matches as candidates (`ZSTD_optLdm_*` in
//!   `opt`)
//! - `mt` (with `std`): multithreaded compression,
//!   compress/zstdmt_compress.c (jobs compressed by worker threads, the
//!   overlap between jobs, the serial LDM step) and how
//!   `ZSTD_compressStream2` drives it with `nbWorkers >= 1`
//! - `table`: the match finder tables, 64-byte aligned like libzstd's
//!   workspace tables (`ZSTD_cwksp`)
#![allow(dead_code)] // filled in step by step (issue #6)

pub(crate) mod bitwriter;
#[cfg(all(target_arch = "x86_64", not(feature = "portable")))]
pub(crate) mod block_asm;
pub(crate) mod cdict;
pub(crate) mod compress;
pub(crate) mod dfast;
pub(crate) mod fast;
pub(crate) mod fse_enc;
pub(crate) mod hist;
pub(crate) mod huf_enc;
pub(crate) mod lazy;
pub(crate) mod ldm;
pub(crate) mod literals;
#[cfg(feature = "std")]
pub(crate) mod mt;
pub(crate) mod opt;
pub(crate) mod params;
pub(crate) mod presplit;
pub(crate) mod row;
#[cfg(all(target_arch = "x86_64", not(feature = "portable")))]
pub(crate) mod seq_asm;
pub(crate) mod seq_store;
pub(crate) mod sequences;
pub(crate) mod split;
pub(crate) mod stream;
pub(crate) mod table;

pub use cdict::EncoderDictionary;
use compress::FrameParams;
pub use compress::compress_bound;
pub use stream::{CompressProgress, EndOp, StreamEncoder};

/// Compresses `src` into one zstd frame at `level` (1 to 22, negative for
/// faster; 0 means the default, 3), like libzstd's `ZSTD_compress`: the
/// output is byte-identical to libzstd 1.5.7's.
///
/// Every level and strategy is implemented, `fast` to `btultra2`, with
/// libzstd's block splitters and its long distance matcher, which it
/// enables by itself at level 22 for inputs over 64 MiB (a 128 MiB window;
/// memory use is then libzstd's too, about 700 MiB of tables).
///
/// ```
/// let data = b"hello hello hello hello hello hello";
/// let compressed = zstd_native::compress(data, 1)?;
/// assert_eq!(zstd_native::decompress(&compressed)?, data);
/// # Ok::<(), zstd_native::Error>(())
/// ```
///
/// # Errors
///
/// None in practice: the output buffer is [`compress_bound`] bytes, always
/// enough.
pub fn compress(src: &[u8], level: i32) -> crate::error::Result<alloc::vec::Vec<u8>> {
    with_cached(|fc| compress::compress_with(fc, src, level, true, FrameParams::default()))
}

/// Compresses into `dst`, returning the compressed size; `dst` of
/// [`compress_bound`]`(src.len())` bytes is always enough.
///
/// # Errors
///
/// [`Error::OutputTooSmall`](crate::Error::OutputTooSmall) if `dst` is too
/// small, or as [`compress`].
pub fn compress_to_slice(src: &[u8], dst: &mut [u8], level: i32) -> crate::error::Result<usize> {
    with_cached(|fc| {
        compress::compress_to_slice_with(fc, src, dst, level, true, FrameParams::default())
    })
}

/// Compresses `src` into one zstd frame at `level` with the dictionary
/// `dict` (zstd-format or raw content, see [`EncoderDictionary`]; an empty
/// one is no dictionary). The output is byte-identical to libzstd 1.5.7's
/// `ZSTD_CCtx_loadDictionary` then `ZSTD_compress2` on a new context (the
/// `zstd` crate's `bulk::Compressor::with_dictionary(level, dict)`). It
/// indexes the dictionary on every call: for many inputs, use a
/// [`Compressor`] (which indexes it once) or an [`EncoderDictionary`].
///
/// ```
/// let dict = b"the quick brown fox jumps over the lazy dog";
/// let compressed = zstd_native::compress_with_dictionary(b"the lazy dog jumps", dict, 3)?;
/// let mut d = zstd_native::Decompressor::with_dictionary(zstd_native::Dictionary::new(dict)?);
/// assert_eq!(d.decompress(&compressed)?, b"the lazy dog jumps");
/// # Ok::<(), zstd_native::Error>(())
/// ```
///
/// # Errors
///
/// [`Error::InvalidDictionary`](crate::Error::InvalidDictionary) if a
/// zstd-format dictionary is corrupt, or as [`compress`].
pub fn compress_with_dictionary(
    src: &[u8],
    dict: &[u8],
    level: i32,
) -> crate::error::Result<alloc::vec::Vec<u8>> {
    let mut slot = cdict::DictSlot::local(dict)?;
    with_cached(|fc| {
        compress::compress_dict_with(fc, &mut slot, src, level, true, FrameParams::default())
    })
}

type Cached = Option<alloc::boxed::Box<compress::FrameCompressor>>;

/// Runs `f` with a per-thread cached compressor (with `std`): reusing its
/// tables and buffers matters for small inputs, as in libzstd's CCtx.
#[cfg(feature = "std")]
fn with_cached<T>(f: impl FnOnce(&mut Cached) -> T) -> T {
    use core::cell::Cell;
    std::thread_local! {
        static CACHED: Cell<Cached> = const { Cell::new(None) };
    }
    let mut c = CACHED.with(Cell::take);
    let r = f(&mut c);
    CACHED.with(|cell| cell.set(c));
    r
}

#[cfg(not(feature = "std"))]
fn with_cached<T>(f: impl FnOnce(&mut Cached) -> T) -> T {
    f(&mut None)
}

/// A reusable compressor (libzstd's `ZSTD_CCtx`): keeps its tables and
/// buffers between frames, which matters for many small inputs, and
/// optionally a dictionary.
///
/// ```
/// let mut c = zstd_native::Compressor::new(1);
/// let a = c.compress(b"first message, first message")?;
/// let b = c.compress(b"second message")?;
/// assert_eq!(zstd_native::decompress(&a)?, b"first message, first message");
/// assert_eq!(zstd_native::decompress(&b)?, b"second message");
/// # Ok::<(), zstd_native::Error>(())
/// ```
///
/// With a dictionary, its frames are byte-identical to those of libzstd's
/// context with the dictionary loaded (`ZSTD_CCtx_loadDictionary`, the
/// `zstd` crate's `bulk::Compressor::with_dictionary`) or referenced
/// (`ZSTD_CCtx_refCDict`, `with_prepared_dictionary`):
///
/// ```
/// let dict = b"{\"id\": 0, \"name\": \"\", \"tags\": []}";
/// let mut c = zstd_native::Compressor::with_dictionary(3, dict)?;
/// let a = c.compress(b"{\"id\": 1, \"name\": \"one\", \"tags\": [\"a\"]}")?;
/// let mut d = zstd_native::Decompressor::with_dictionary(zstd_native::Dictionary::new(dict)?);
/// assert_eq!(d.decompress(&a)?, b"{\"id\": 1, \"name\": \"one\", \"tags\": [\"a\"]}");
/// # Ok::<(), zstd_native::Error>(())
/// ```
///
/// With `std`, it can compress with worker threads
/// ([`set_workers`](Self::set_workers)), as libzstd's `ZSTD_compress2` with
/// `ZSTD_c_nbWorkers`.
pub struct Compressor {
    level: i32,
    inner: Cached,
    dict: cdict::DictSlot,
    fparams: FrameParams,
    #[cfg(feature = "std")]
    mt_params: mt::MtParams,
    #[cfg(feature = "std")]
    mt: Option<alloc::boxed::Box<mt::MtCtx>>,
}

impl core::fmt::Debug for Compressor {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let mut d = f.debug_struct("Compressor");
        d.field("level", &self.level)
            .field("dict", &self.dict)
            .field("checksum", &self.fparams.checksum);
        #[cfg(feature = "std")]
        d.field("workers", &self.mt_params.workers);
        d.finish_non_exhaustive()
    }
}

impl Compressor {
    /// A compressor for `level` (see [`compress`]).
    pub fn new(level: i32) -> Self {
        Compressor {
            level,
            inner: None,
            dict: cdict::DictSlot::None,
            fparams: FrameParams::default(),
            #[cfg(feature = "std")]
            mt_params: mt::MtParams::default(),
            #[cfg(feature = "std")]
            mt: None,
        }
    }

    /// Ends each frame with a content checksum (libzstd's
    /// `ZSTD_c_checksumFlag`; off by default): the low 32 bits of the
    /// XXH64 of the frame's content, which decoders verify. It costs 4
    /// bytes per frame and the time to hash the input.
    ///
    /// ```
    /// let mut c = zstd_native::Compressor::new(3);
    /// c.set_checksum(true);
    /// let a = c.compress(b"checked, checked, checked")?;
    /// assert_eq!(zstd_native::decompress(&a)?, b"checked, checked, checked");
    /// # Ok::<(), zstd_native::Error>(())
    /// ```
    pub fn set_checksum(&mut self, on: bool) {
        self.fparams.checksum = on;
    }

    /// Writes the content size into the frame header (libzstd's
    /// `ZSTD_c_contentSizeFlag`; on by default). Off, the header leaves it
    /// out (decoders then cannot tell the size before decoding); the
    /// compressed blocks are the same.
    pub fn set_content_size_flag(&mut self, on: bool) {
        self.fparams.content_size = on;
    }

    /// Writes the dictionary's ID into the frame header, when a
    /// dictionary with an ID is used (libzstd's `ZSTD_c_dictIDFlag`; on by
    /// default). Off, the header leaves it out (decoders then cannot check
    /// that they have the right dictionary); the compressed blocks are the
    /// same.
    pub fn set_dict_id_flag(&mut self, on: bool) {
        self.fparams.dict_id = on;
    }

    /// Compresses with `n` worker threads (libzstd's `ZSTD_c_nbWorkers`; at
    /// most 256, 64 on 32-bit targets); 0, the default, compresses in the
    /// calling thread. The output is byte-identical to libzstd's
    /// `ZSTD_compress2` with the same worker count (a different count
    /// changes it, see below).
    ///
    /// As in libzstd, inputs of at most 512 KiB are still compressed in the
    /// calling thread; larger ones are cut into jobs (by default `4 <<
    /// windowLog` bytes, at least 1 MiB: 8 MiB at level 3, 32 MiB at level
    /// 19), each compressed by a worker starting from the end of the
    /// previous one, so a frame needs several jobs to use several workers.
    /// Multithreaded output differs from single-threaded output, and
    /// compresses slightly less well.
    ///
    /// The threads are started at the first frame that uses them and kept
    /// until the compressor is dropped (or the count changes). Memory: per
    /// worker, a compressor's tables for the level; per job in flight (at
    /// most the smallest power of 2 above the worker count plus 2), its
    /// input and output.
    ///
    /// On 32-bit targets, large jobs and many workers can need more memory
    /// than the address space has: libzstd's round buffer of the input is
    /// `workers + 3` jobs (`+ 2` without overlap; at least the window plus
    /// the slack jobs with long distance matching), so level 22's 512 MiB jobs, or a 512 MiB job size, need
    /// 2 GiB with one worker. Compressing a frame then fails with
    /// [`Error::MemoryAllocation`](crate::Error::MemoryAllocation), as
    /// libzstd fails to allocate it (`memory_allocation`).
    #[cfg(feature = "std")]
    pub fn set_workers(&mut self, n: u32) {
        self.mt_params.set_workers(n);
    }

    /// The worker thread count (see [`set_workers`](Self::set_workers)).
    #[cfg(feature = "std")]
    pub fn workers(&self) -> u32 {
        self.mt_params.workers
    }

    /// Sets the job size of multithreaded compression (libzstd's
    /// `ZSTD_c_jobSize`: 0 is automatic, other sizes are raised to 512 KiB
    /// and lowered to 1 GiB, 512 MiB on 32-bit targets). For tests.
    #[cfg(feature = "std")]
    #[doc(hidden)]
    pub fn set_job_size(&mut self, size: usize) {
        self.mt_params.set_job_size(size);
    }

    /// Sets the overlap between jobs of multithreaded compression
    /// (libzstd's `ZSTD_c_overlapLog`: 0 is automatic, else 1 (none) to 9
    /// (the whole window)). For tests.
    ///
    /// # Panics
    ///
    /// If `log` is over 9.
    #[cfg(feature = "std")]
    #[doc(hidden)]
    pub fn set_overlap_log(&mut self, log: u32) {
        self.mt_params.set_overlap_log(log);
    }

    /// `ZSTD_compress2` with workers: the multithreaded path for `src`, if
    /// it is taken; the output written into `dst` (or a new vector if
    /// `None`).
    #[cfg(feature = "std")]
    fn compress_mt(
        &mut self,
        src: &[u8],
        dst: Option<&mut [u8]>,
    ) -> Option<crate::error::Result<(alloc::vec::Vec<u8>, usize)>> {
        let pledged = Some(src.len() as u64);
        if !self.mt_params.applies(pledged) {
            return None;
        }
        Some((|| {
            let (cd, level) = self.dict.for_frame(self.level)?;
            let cp = cdict::frame_cparams(level, cd.as_deref(), pledged, 0, false);
            let ldm = params::ldm_enabled(&cp);
            let m = self
                .mt
                .get_or_insert_with(|| alloc::boxed::Box::new(mt::MtCtx::new()));
            m.init_frame(&self.mt_params, cp, ldm, cd, pledged, self.fparams)?;
            match dst {
                Some(d) => {
                    let p = m.compress(src, d, EndOp::End)?;
                    if p.remaining != 0 {
                        m.abandon();
                        return Err(crate::Error::OutputTooSmall);
                    }
                    Ok((alloc::vec::Vec::new(), p.written))
                }
                None => {
                    let mut v = alloc::vec::Vec::with_capacity(compress_bound(src.len()));
                    m.compress_end_to_vec(src, &mut v)?;
                    let n = v.len();
                    Ok((v, n))
                }
            }
        })())
    }

    /// A compressor for `level` with the dictionary `dict` (see
    /// [`set_dictionary`](Self::set_dictionary)).
    ///
    /// # Errors
    ///
    /// [`Error::InvalidDictionary`](crate::Error::InvalidDictionary) if a
    /// zstd-format dictionary is corrupt.
    pub fn with_dictionary(level: i32, dict: &[u8]) -> crate::error::Result<Self> {
        let mut c = Self::new(level);
        c.set_dictionary(dict)?;
        Ok(c)
    }

    /// A compressor with a prepared dictionary, at its level (see
    /// [`set_prepared_dictionary`](Self::set_prepared_dictionary)).
    pub fn with_prepared_dictionary(dict: &EncoderDictionary) -> Self {
        let mut c = Self::new(dict.level());
        c.set_prepared_dictionary(dict);
        c
    }

    /// Changes the level for the next frames. With a prepared dictionary,
    /// frames take the dictionary's level instead; a dictionary given to
    /// [`set_dictionary`](Self::set_dictionary) is indexed at the level of
    /// the first frame that uses it (as in libzstd).
    pub fn set_level(&mut self, level: i32) {
        self.level = level;
    }

    /// Uses `dict` for the next frames (libzstd's
    /// `ZSTD_CCtx_loadDictionary`), replacing any dictionary; an empty
    /// `dict` removes it. Zstd-format dictionaries (starting with the
    /// magic number `0xEC30A437`) and raw content are both accepted, see
    /// [`EncoderDictionary`]. The dictionary is indexed for the level of the
    /// next frame, then kept.
    ///
    /// # Errors
    ///
    /// [`Error::InvalidDictionary`](crate::Error::InvalidDictionary) if a
    /// zstd-format dictionary is corrupt; the compressor then has no
    /// dictionary.
    pub fn set_dictionary(&mut self, dict: &[u8]) -> crate::error::Result<()> {
        self.dict = cdict::DictSlot::None;
        self.dict = cdict::DictSlot::local(dict)?;
        Ok(())
    }

    /// Uses the prepared `dict` for the next frames, at its level (libzstd's
    /// `ZSTD_CCtx_refCDict`), replacing any dictionary.
    pub fn set_prepared_dictionary(&mut self, dict: &EncoderDictionary) {
        self.dict = cdict::DictSlot::Prepared(dict.inner.clone());
    }

    /// Removes the dictionary.
    pub fn clear_dictionary(&mut self) {
        self.dict = cdict::DictSlot::None;
    }

    /// Compresses `src` as one frame (see [`compress`]).
    ///
    /// # Errors
    ///
    /// As [`compress`].
    pub fn compress(&mut self, src: &[u8]) -> crate::error::Result<alloc::vec::Vec<u8>> {
        #[cfg(feature = "std")]
        if let Some(r) = self.compress_mt(src, None) {
            return r.map(|(v, _)| v);
        }
        if !matches!(self.dict, cdict::DictSlot::None) {
            return compress::compress_dict_with(
                &mut self.inner,
                &mut self.dict,
                src,
                self.level,
                false,
                self.fparams,
            );
        }
        compress::compress_with(&mut self.inner, src, self.level, false, self.fparams)
    }

    /// Compresses `src` as one frame into `dst` (see [`compress_to_slice`]).
    ///
    /// # Errors
    ///
    /// As [`compress_to_slice`].
    pub fn compress_to_slice(&mut self, src: &[u8], dst: &mut [u8]) -> crate::error::Result<usize> {
        #[cfg(feature = "std")]
        if let Some(r) = self.compress_mt(src, Some(&mut *dst)) {
            return r.map(|(_, n)| n);
        }
        if !matches!(self.dict, cdict::DictSlot::None) {
            return compress::compress_dict_to_slice_with(
                &mut self.inner,
                &mut self.dict,
                src,
                dst,
                self.level,
                false,
                self.fparams,
            );
        }
        compress::compress_to_slice_with(&mut self.inner, src, dst, self.level, false, self.fparams)
    }
}