pub struct Compressor { /* private fields */ }Expand description
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");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\"]}");With std, it can compress with worker threads
(set_workers), as libzstd’s ZSTD_compress2 with
ZSTD_c_nbWorkers.
Implementations§
Source§impl Compressor
impl Compressor
Sourcepub fn set_checksum(&mut self, on: bool)
pub fn set_checksum(&mut self, on: bool)
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");Sourcepub fn set_content_size_flag(&mut self, on: bool)
pub fn set_content_size_flag(&mut self, on: bool)
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.
Sourcepub fn set_dict_id_flag(&mut self, on: bool)
pub fn set_dict_id_flag(&mut self, on: bool)
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.
Sourcepub fn set_workers(&mut self, n: u32)
Available on crate feature std only.
pub fn set_workers(&mut self, n: u32)
std only.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 with level 22’s
256 MiB jobs (the largest there) it reaches 2 GiB from 5 workers on.
Compressing a frame then fails with
Error::MemoryAllocation, as
libzstd fails to allocate it (memory_allocation).
Sourcepub fn workers(&self) -> u32
Available on crate feature std only.
pub fn workers(&self) -> u32
std only.The worker thread count (see set_workers).
Sourcepub fn with_dictionary(level: i32, dict: &[u8]) -> Result<Self, Error>
pub fn with_dictionary(level: i32, dict: &[u8]) -> Result<Self, Error>
A compressor for level with the dictionary dict (see
set_dictionary).
§Errors
Error::InvalidDictionary if a
zstd-format dictionary is corrupt.
Sourcepub fn with_prepared_dictionary(dict: &EncoderDictionary) -> Self
pub fn with_prepared_dictionary(dict: &EncoderDictionary) -> Self
A compressor with a prepared dictionary, at its level (see
set_prepared_dictionary).
Sourcepub fn set_level(&mut self, level: i32)
pub fn set_level(&mut self, level: i32)
Changes the level for the next frames. With a prepared dictionary,
frames take the dictionary’s level instead; a dictionary given to
set_dictionary is indexed at the level of
the first frame that uses it (as in libzstd).
Sourcepub fn set_dictionary(&mut self, dict: &[u8]) -> Result<(), Error>
pub fn set_dictionary(&mut self, dict: &[u8]) -> Result<(), Error>
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 if a
zstd-format dictionary is corrupt; the compressor then has no
dictionary.
Sourcepub fn set_prepared_dictionary(&mut self, dict: &EncoderDictionary)
pub fn set_prepared_dictionary(&mut self, dict: &EncoderDictionary)
Uses the prepared dict for the next frames, at its level (libzstd’s
ZSTD_CCtx_refCDict), replacing any dictionary.
Sourcepub fn clear_dictionary(&mut self)
pub fn clear_dictionary(&mut self)
Removes the dictionary.