Skip to main content

compress_slice_to_vec

Function compress_slice_to_vec 

Source
pub fn compress_slice_to_vec(source: &[u8], level: CompressionLevel) -> Vec<u8> 
Expand description

Compress a contiguous byte slice into a fresh Vec<u8> without the input-buffering step that compress_to_vec performs to adapt a Read source. Donor-parity peak-memory shape: the input is read by reference, and the output Vec is seeded with min(compress_bound(src), OUTPUT_BLOCK_CAP = 128 KiB).

The seed is intentionally capped at one donor block (OUTPUT_BLOCK_CAP = 128 KiB). Three regimes:

  • Inputs up to ~127 KiB where compress_bound(src) < OUTPUT_BLOCK_CAP — the min picks the tighter bound and the Vec never reallocates inside the measured window (cheap, no doubling spikes, no over-allocation).
  • Inputs around 128 KiB where compress_bound(src) >= OUTPUT_BLOCK_CAP — the min clamps to the cap, so the “no reallocation” property holds only as long as the actual compressed output also fits within OUTPUT_BLOCK_CAP. For high-entropy inputs near the cap boundary the Vec may grow by one doubling step before the next block lands.
  • Larger inputs — the Vec grows via amortized doubling, with peak transient memory ≈ 2× current compressed size at the last realloc. Deliberate trade-off against pinning compress_bound(src) upfront, which on the 100 MiB large-log-stream scenario pinned 100.4 MiB even though the actual compressed output was ≪ 1 MiB.

§Panics

Panics on encoder error (matches the failure surface of compress_to_vec, which this function backs). The internal FrameCompressor::compress call propagates io::Error from the output Vec writer; under the in-tree default writer that channel is infallible and any error becomes a panic. Out-of- memory during Vec::with_capacity or the encoder’s per-block scratch allocations is handled by the global allocator’s abort policy. Migrating to a fallible variant requires plumbing Result through FrameCompressor::compress — out of scope for the slice/Vec entry points which mirror the donor ZSTD_compress shape (no error return on the bulk path).

use structured_zstd::encoding::{compress_slice_to_vec, CompressionLevel};
let data: &[u8] = &[0,0,0,0,0,0,0,0,0,0,0,0];
let compressed = compress_slice_to_vec(data, CompressionLevel::Fastest);