mzstd 0.2.1

Zstandard (RFC 8878) compression and decompression in safe Rust; single-threaded encoding is byte-identical to zstd 1.5.7
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
//! Incremental parallel streams: input arrives in chunks, segments become
//! tasks.
//!
//! [`ParallelCompressor::start`] returns a session and a
//! [`ParallelTaskQueue`]. Every full segment the session accepts becomes a
//! task on that queue, and the caller's executors take and run them. The
//! session delivers finished parts in input order, so output can be written
//! while later segments still compress.

use core::marker::PhantomData;
use core::mem::MaybeUninit;

use super::core::{QueueHandle, Stream, StreamTask};
use super::{ParallelCompressor, ParallelEncodeError, TaskCount};
use crate::compressor::session::{InputSize, Operation, StreamConfig};

/// What a parallel session needs next.
///
/// # Examples
///
/// ```
/// use mzstd::compressor::parallel::ParallelEncoderStatus;
///
/// assert_ne!(ParallelEncoderStatus::Pending, ParallelEncoderStatus::NeedsInput);
/// ```
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum ParallelEncoderStatus {
    /// The operation has done all it can; more input or a new operation is
    /// needed.
    NeedsInput,
    /// Compressed bytes are waiting; call again with room for them.
    NeedsOutput,
    /// Queued tasks must complete first: keep executors running, call
    /// [`ParallelEncoderSession::wait`], then call again.
    Pending,
    /// The frame is complete and delivered.
    Finished,
}

/// What one parallel `process` call did.
///
/// # Examples
///
/// ```
/// use mzstd::compressor::parallel::{ParallelEncoderStatus, ParallelProgress};
///
/// let progress = ParallelProgress {
///     consumed: 4,
///     produced: 0,
///     status: ParallelEncoderStatus::NeedsInput,
/// };
/// assert_eq!(progress.consumed, 4);
/// ```
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct ParallelProgress {
    /// Bytes taken from the input.
    pub consumed: usize,
    /// Bytes written to the output.
    pub produced: usize,
    /// What the session needs next.
    pub status: ParallelEncoderStatus,
}

/// A segment compression queued by a streaming session; run it on any
/// thread.
///
/// Dropping it unrun fails the stream with
/// [`ParallelEncodeError::TaskAbandoned`], and a panic while it runs with
/// [`ParallelEncodeError::TaskPanicked`], so the session never waits for it
/// forever.
#[derive(Debug)]
pub struct OwnedParallelTask {
    inner: StreamTask,
}

impl OwnedParallelTask {
    /// Compresses the segment.
    pub fn run(self) {
        self.inner.run();
    }
}

/// Where a streaming session's tasks wait to be run.
///
/// Clone one handle per executor. [`Self::recv`] blocks until a task is
/// queued and returns `None` once the session has finished, failed or been
/// dropped, so `for task in queue { task.run() }` is a complete executor
/// loop.
///
/// Dropping every handle abandons the tasks still queued: the session then
/// fails with [`ParallelEncodeError::TaskAbandoned`] or
/// [`ParallelEncodeError::TaskQueueClosed`] instead of waiting forever. On a
/// single thread, run queued tasks with [`Self::next_task`] before waiting.
///
/// # Examples
///
/// ```
/// use mzstd::compressor::parallel::{ParallelCompressor, ParallelConfig, TaskCount};
/// use mzstd::Operation;
///
/// let mut compressor = ParallelCompressor::new(Default::default(), ParallelConfig::default())?;
/// let (mut session, queue) = compressor.start(Default::default(), TaskCount::ONE);
/// let mut output = [0u8; 64];
/// session.process(b"queued on one thread", &mut output, Operation::End)?;
/// while let Some(task) = queue.next_task() {
///     task.run();
/// }
/// session.wait()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone)]
pub struct ParallelTaskQueue {
    inner: QueueHandle,
}

impl ParallelTaskQueue {
    /// Blocks until a task is queued; returns `None` once the session has
    /// ended.
    ///
    /// # Examples
    ///
    /// ```
    /// use mzstd::compressor::parallel::{ParallelCompressor, ParallelConfig, TaskCount};
    ///
    /// let mut compressor = ParallelCompressor::new(Default::default(), ParallelConfig::default())?;
    /// let (session, queue) = compressor.start(Default::default(), TaskCount::ONE);
    /// drop(session);
    /// assert!(queue.recv().is_none());
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    #[must_use]
    pub fn recv(&self) -> Option<OwnedParallelTask> {
        self.inner.recv().map(|inner| OwnedParallelTask { inner })
    }

    /// Takes the next queued task without blocking, if any.
    #[must_use]
    pub fn next_task(&self) -> Option<OwnedParallelTask> {
        self.inner
            .try_recv()
            .map(|inner| OwnedParallelTask { inner })
    }

    /// Number of tasks queued and not yet taken.
    #[must_use]
    pub fn queued(&self) -> usize {
        self.inner.queued()
    }
}

impl IntoIterator for ParallelTaskQueue {
    type Item = OwnedParallelTask;
    type IntoIter = ParallelTasks;

    /// Yields queued tasks until the session ends, blocking between them.
    fn into_iter(self) -> ParallelTasks {
        ParallelTasks { queue: self }
    }
}

/// Blocking iterator over a [`ParallelTaskQueue`], ending with the session.
#[derive(Debug)]
pub struct ParallelTasks {
    queue: ParallelTaskQueue,
}

impl Iterator for ParallelTasks {
    type Item = OwnedParallelTask;

    fn next(&mut self) -> Option<Self::Item> {
        self.queue.recv()
    }
}

macro_rules! session_methods {
    () => {
        /// Moves the frame forward by one step without blocking.
        ///
        /// Takes what fits the window from `input`, delivers finished parts
        /// into `output` in input order, and reports what it needs next:
        ///
        /// - [`ParallelEncoderStatus::NeedsOutput`]: delivered bytes are
        ///   waiting.
        /// - [`ParallelEncoderStatus::Pending`]: input is left over because
        ///   the window is full, or a `Flush` or `End` waits for queued
        ///   tasks.
        /// - [`ParallelEncoderStatus::NeedsInput`]: everything this
        ///   operation can do is done. A `Continue` call may still have
        ///   tasks running.
        /// - [`ParallelEncoderStatus::Finished`]: the frame was delivered;
        ///   later calls consume and produce nothing.
        ///
        /// A `Flush` reports `NeedsInput` only once everything accepted has
        /// been delivered. Once an `End` has queued the last part, later
        /// calls consume no input whatever their operation, and only
        /// deliver.
        ///
        /// # Errors
        ///
        /// [`ParallelEncodeError::StreamFailed`] with the cause as its source
        /// when the input contradicts a declared size, a task fails, or the
        /// queue is closed, and on every later call.
        ///
        /// # Examples
        ///
        /// ```
        /// use mzstd::compressor::parallel::{
        ///     ParallelCompressor, ParallelConfig, ParallelEncoderStatus, TaskCount,
        /// };
        /// use mzstd::Operation;
        ///
        /// let mut compressor = ParallelCompressor::new(Default::default(), ParallelConfig::default())?;
        /// let (mut session, queue) = compressor.start(Default::default(), TaskCount::ONE);
        /// let mut output = [0u8; 64];
        ///
        /// let progress = session.process(b"accepted", &mut output, Operation::Continue)?;
        /// assert_eq!(progress.consumed, 8);
        /// assert_eq!(progress.status, ParallelEncoderStatus::NeedsInput);
        ///
        /// let progress = session.process(b"", &mut output, Operation::End)?;
        /// assert_eq!(progress.status, ParallelEncoderStatus::Pending);
        /// queue.next_task().unwrap().run();
        /// session.wait()?;
        /// let progress = session.process(b"", &mut output, Operation::End)?;
        /// assert_eq!(progress.status, ParallelEncoderStatus::Finished);
        /// # Ok::<(), Box<dyn std::error::Error>>(())
        /// ```
        pub fn process(
            &mut self,
            input: &[u8],
            output: &mut [u8],
            operation: Operation,
        ) -> Result<ParallelProgress, ParallelEncodeError> {
            self.stream.process(input, output, operation)
        }

        /// [`Self::process`] into uninitialized memory, such as
        /// [`Vec::spare_capacity_mut`], without zeroing it first.
        ///
        /// It produces the same bytes, `consumed` and `produced` counts,
        /// statuses and errors as [`Self::process`], and calls of the two
        /// may be mixed on one frame. On success exactly
        /// `output[..produced]` is initialized and no byte after it is
        /// written; after an error, treat none of `output` as initialized.
        ///
        /// # Errors
        ///
        /// As [`Self::process`].
        ///
        /// # Examples
        ///
        /// ```
        /// use mzstd::compressor::parallel::{
        ///     ParallelCompressor, ParallelConfig, ParallelEncoderStatus, TaskCount,
        /// };
        /// use mzstd::Operation;
        ///
        /// let mut compressor = ParallelCompressor::new(Default::default(), ParallelConfig::default())?;
        /// let (mut session, queue) = compressor.start(Default::default(), TaskCount::ONE);
        /// let mut frame: Vec<u8> = Vec::with_capacity(64);
        /// let mut input = &b"delivered into spare capacity"[..];
        /// loop {
        ///     if frame.len() == frame.capacity() {
        ///         frame.reserve(64);
        ///     }
        ///     let progress =
        ///         session.process_uninit(input, frame.spare_capacity_mut(), Operation::End)?;
        ///     input = &input[progress.consumed..];
        ///     // SAFETY: `process_uninit` initialized the first `produced` spare bytes.
        ///     unsafe { frame.set_len(frame.len() + progress.produced) };
        ///     match progress.status {
        ///         ParallelEncoderStatus::Finished => break,
        ///         ParallelEncoderStatus::Pending => {
        ///             while let Some(task) = queue.next_task() {
        ///                 task.run();
        ///             }
        ///             session.wait()?;
        ///         }
        ///         _ => {}
        ///     }
        /// }
        /// assert_eq!(frame, mzstd::Compressor::default().compress(b"delivered into spare capacity")?);
        /// # Ok::<(), Box<dyn std::error::Error>>(())
        /// ```
        pub fn process_uninit(
            &mut self,
            input: &[u8],
            output: &mut [MaybeUninit<u8>],
            operation: Operation,
        ) -> Result<ParallelProgress, ParallelEncodeError> {
            self.stream.process(input, output, operation)
        }

        /// Makes everything accepted so far decodable, without taking input.
        ///
        /// Shorthand for [`Self::process`] with empty input and
        /// [`Operation::Flush`]. Repeat it, waiting on `Pending`, until it
        /// reports [`ParallelEncoderStatus::NeedsInput`]. A flush ends the
        /// current segment early, which moves every later segment boundary.
        ///
        /// # Errors
        ///
        /// As [`Self::process`].
        ///
        /// # Examples
        ///
        /// ```
        /// use mzstd::compressor::parallel::{
        ///     ParallelCompressor, ParallelConfig, ParallelEncoderStatus, TaskCount,
        /// };
        /// use mzstd::Operation;
        ///
        /// let mut compressor = ParallelCompressor::new(Default::default(), ParallelConfig::default())?;
        /// let (mut session, queue) = compressor.start(Default::default(), TaskCount::ONE);
        /// let mut output = [0u8; 64];
        /// session.process(b"flushed", &mut output, Operation::Continue)?;
        /// // The frame header is ready at once; the segment is queued.
        /// assert_eq!(session.flush(&mut output)?.status, ParallelEncoderStatus::Pending);
        /// queue.next_task().unwrap().run();
        /// session.wait()?;
        /// let progress = session.flush(&mut output)?;
        /// assert_eq!(progress.status, ParallelEncoderStatus::NeedsInput);
        /// assert!(progress.produced > 0);
        /// # Ok::<(), Box<dyn std::error::Error>>(())
        /// ```
        pub fn flush(
            &mut self,
            output: &mut [u8],
        ) -> Result<ParallelProgress, ParallelEncodeError> {
            self.process(&[], output, Operation::Flush)
        }

        /// Ends the frame, without taking input.
        ///
        /// Shorthand for [`Self::process`] with empty input and
        /// [`Operation::End`]. Repeat it, waiting on `Pending`, until it
        /// reports [`ParallelEncoderStatus::Finished`].
        ///
        /// # Errors
        ///
        /// As [`Self::process`].
        ///
        /// # Examples
        ///
        /// ```
        /// use mzstd::compressor::parallel::{
        ///     ParallelCompressor, ParallelConfig, ParallelEncoderStatus, TaskCount,
        /// };
        ///
        /// let mut compressor = ParallelCompressor::new(Default::default(), ParallelConfig::default())?;
        /// let (mut session, _queue) = compressor.start(Default::default(), TaskCount::ONE);
        /// let mut output = [0u8; 64];
        /// // An empty frame is compressed inline; no task is queued.
        /// assert_eq!(session.finish(&mut output)?.status, ParallelEncoderStatus::Finished);
        /// assert!(session.is_finished());
        /// # Ok::<(), Box<dyn std::error::Error>>(())
        /// ```
        pub fn finish(
            &mut self,
            output: &mut [u8],
        ) -> Result<ParallelProgress, ParallelEncodeError> {
            self.process(&[], output, Operation::End)
        }

        /// Blocks until a queued task completes; returns at once when one has
        /// completed since the session last looked, or when none is queued
        /// or running.
        ///
        /// The caller's executors must be able to run tasks meanwhile.
        ///
        /// # Errors
        ///
        /// As [`Self::process`].
        ///
        /// # Examples
        ///
        /// ```
        /// use mzstd::compressor::parallel::{ParallelCompressor, ParallelConfig, TaskCount};
        ///
        /// let mut compressor = ParallelCompressor::new(Default::default(), ParallelConfig::default())?;
        /// let (mut session, _queue) = compressor.start(Default::default(), TaskCount::ONE);
        /// session.wait()?; // Nothing is queued.
        /// # Ok::<(), Box<dyn std::error::Error>>(())
        /// ```
        pub fn wait(&mut self) -> Result<(), ParallelEncodeError> {
            self.stream.wait()
        }

        /// Whether the frame has been completed and delivered.
        #[must_use]
        pub const fn is_finished(&self) -> bool {
            self.stream.is_finished()
        }
    };
}

/// One parallel frame whose input arrives in chunks, borrowing its
/// compressor.
///
/// Created by [`ParallelCompressor::start`], which also returns the
/// [`ParallelTaskQueue`] its tasks go to. The session copies input into
/// fixed segments; each full segment is queued as a task, and at most
/// `window` parts are queued, running or waiting for delivery at once. When
/// the window is full the session takes no more input and reports
/// [`ParallelEncoderStatus::Pending`].
///
/// With a declared input size and without [`Operation::Flush`], the frame is
/// byte-identical to [`ParallelCompressor::prepare_slice`] over the same
/// bytes, however the input is chunked and whatever the window, including
/// the serial fallback for inputs that fit in one segment (which holds
/// without a declared size too). A flush ends the current segment early,
/// which moves every later segment boundary.
///
/// `process` never blocks. [`Self::wait`] blocks until a queued task
/// completes, so executors must be able to run while the caller waits.
/// Finishing, failing or dropping the session closes the queue, which ends
/// every executor's `for task in queue` loop; dropping it unfinished
/// abandons the frame and skips tasks that have not started.
///
/// # Examples
///
/// ```
/// use mzstd::compressor::parallel::{
///     ParallelCompressor, ParallelConfig, ParallelEncoderStatus, SegmentSize, TaskCount,
/// };
/// use mzstd::{InputSize, Operation};
///
/// let config = ParallelConfig::default().with_segment_size(SegmentSize::MIN);
/// let mut compressor = ParallelCompressor::new(Default::default(), config)?;
/// let payload = b"streamed into parallel segments ".repeat(10_000);
///
/// let mut frame = Vec::new();
/// std::thread::scope(|scope| {
///     let stream = InputSize::Exact(payload.len() as u64).into();
///     let (mut session, queue) = compressor.start(stream, TaskCount::try_from(4)?);
///     for _ in 0..4 {
///         let queue = queue.clone();
///         scope.spawn(move || queue.into_iter().for_each(|task| task.run()));
///     }
///     drop(queue);
///
///     let mut buffer = [0u8; 4096];
///     let mut input = payload.as_slice();
///     loop {
///         let progress = session.process(input, &mut buffer, Operation::End)?;
///         input = &input[progress.consumed..];
///         frame.extend_from_slice(&buffer[..progress.produced]);
///         match progress.status {
///             ParallelEncoderStatus::Finished => break,
///             ParallelEncoderStatus::Pending => session.wait()?,
///             _ => {}
///         }
///     }
///     Ok::<(), Box<dyn std::error::Error>>(())
/// })?;
///
/// assert_eq!(frame, compressor.compress(&payload)?);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug)]
pub struct ParallelEncoderSession<'c> {
    stream: Stream,
    compressor: PhantomData<&'c mut ParallelCompressor>,
}

impl ParallelEncoderSession<'_> {
    session_methods!();
}

/// One parallel frame whose input arrives in chunks, owning its compressor.
///
/// Created by [`ParallelCompressor::into_session`]. It runs the same state
/// machine as [`ParallelEncoderSession`], so the same calls produce the same
/// bytes, counts, statuses and errors, but carries no lifetime.
///
/// # Examples
///
/// ```
/// use mzstd::compressor::parallel::{
///     ParallelCompressor, ParallelConfig, ParallelEncoderStatus, TaskCount,
/// };
/// use mzstd::Operation;
///
/// let compressor = ParallelCompressor::new(Default::default(), ParallelConfig::default())?;
/// let (mut session, queue) = compressor.into_session(Default::default(), TaskCount::ONE);
/// let executor = std::thread::spawn(move || queue.into_iter().for_each(|task| task.run()));
///
/// let mut output = [0u8; 64];
/// let mut input = &b"owned parallel frame"[..];
/// loop {
///     let progress = session.process(input, &mut output, Operation::End)?;
///     input = &input[progress.consumed..];
///     match progress.status {
///         ParallelEncoderStatus::Finished => break,
///         ParallelEncoderStatus::Pending => session.wait()?,
///         _ => {}
///     }
/// }
/// let _compressor = session.into_compressor();
/// executor.join().unwrap();
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug)]
pub struct ParallelEncoderSessionOwned {
    stream: Stream,
    compressor: ParallelCompressor,
}

impl ParallelEncoderSessionOwned {
    session_methods!();

    /// Returns the compressor, abandoning an unfinished frame: tasks that
    /// have not started are skipped and the queue is closed.
    #[must_use]
    pub fn into_compressor(self) -> ParallelCompressor {
        drop(self.stream);
        self.compressor
    }
}

/// The declared size of a frame, when it has one.
const fn declared(stream: StreamConfig) -> Option<u64> {
    match stream.input_size() {
        InputSize::Exact(size) => Some(size),
        InputSize::Unknown => None,
    }
}

impl ParallelCompressor {
    fn stream(&self, stream: StreamConfig, window: TaskCount) -> (Stream, ParallelTaskQueue) {
        let (stream, queue) = Stream::new(
            self.requested,
            declared(stream),
            self.parallel.segment_size().get(),
            window,
            self.pool.clone(),
        );
        (stream, ParallelTaskQueue { inner: queue })
    }

    /// Starts a frame whose input arrives in chunks, and returns the queue
    /// its tasks go to.
    ///
    /// `window` caps how many segments may be queued, running or waiting for
    /// delivery at once; with the segment being gathered and its overlap, it
    /// bounds the session's memory. No thread is created: run the queue's
    /// tasks on any executor. See [`ParallelEncoderSession`] for the
    /// protocol.
    ///
    /// # Examples
    ///
    /// ```
    /// use mzstd::compressor::parallel::{ParallelCompressor, ParallelConfig, TaskCount};
    ///
    /// let mut compressor = ParallelCompressor::new(Default::default(), ParallelConfig::default())?;
    /// let (session, queue) = compressor.start(Default::default(), TaskCount::try_from(2)?);
    /// assert!(!session.is_finished());
    /// assert_eq!(queue.queued(), 0);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn start(
        &mut self,
        stream: StreamConfig,
        window: TaskCount,
    ) -> (ParallelEncoderSession<'_>, ParallelTaskQueue) {
        let (stream, queue) = self.stream(stream, window);
        let session = ParallelEncoderSession {
            stream,
            compressor: PhantomData,
        };
        (session, queue)
    }

    /// Starts a frame whose input arrives in chunks, with a session that
    /// owns this compressor.
    ///
    /// As [`Self::start`], but the session carries no lifetime.
    ///
    /// # Examples
    ///
    /// ```
    /// use mzstd::compressor::parallel::{ParallelCompressor, ParallelConfig, TaskCount};
    ///
    /// let compressor = ParallelCompressor::new(Default::default(), ParallelConfig::default())?;
    /// let (session, _queue) = compressor.into_session(Default::default(), TaskCount::ONE);
    /// let _compressor = session.into_compressor();
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    #[must_use]
    pub fn into_session(
        self,
        stream: StreamConfig,
        window: TaskCount,
    ) -> (ParallelEncoderSessionOwned, ParallelTaskQueue) {
        let (stream, queue) = self.stream(stream, window);
        let session = ParallelEncoderSessionOwned {
            stream,
            compressor: self,
        };
        (session, queue)
    }
}