gzp 2.0.4

Parallel Compression
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//! Parallel compression.
//!
//! # Examples
//!
//! ```
//! # #[cfg(feature = "deflate")] {
//! use std::{env, fs::File, io::Write};
//!
//! use gzp::{par::compress::{ParCompress, ParCompressBuilder}, deflate::Gzip, ZWriter};
//!
//! let mut writer = vec![];
//! let mut parz: ParCompress<Gzip, _> = ParCompressBuilder::new().from_writer(writer);
//! parz.write_all(b"This is a first test line\n").unwrap();
//! parz.write_all(b"This is a second test line\n").unwrap();
//! parz.finish().unwrap();
//! # }
//! ```
use std::{
    io::{self, Write},
    thread::{JoinHandle, Scope, ScopedJoinHandle},
};

use bytes::{Bytes, BytesMut};
pub use flate2::Compression;
use flume::{bounded, Receiver, Sender};
use log::warn;

use crate::check::Check;
use crate::{CompressResult, FormatSpec, GzpError, Message, ZWriter, DICT_SIZE};

/// The [`ParCompress`] builder.
#[derive(Debug)]
pub struct ParCompressBuilder<F>
where
    F: FormatSpec,
{
    /// The buffersize accumulate before trying to compress it. Defaults to `F::DEFAULT_BUFSIZE`.
    buffer_size: usize,
    /// The number of threads to use for compression. Defaults to all available threads.
    num_threads: usize,
    /// The compression level of the output, see [`Compression`].
    compression_level: Compression,
    /// The out file format to use.
    format: F,
    /// Whether or not to pin threads to specific cpus and what core to start pins at
    pin_threads: Option<usize>,
}

impl<F> ParCompressBuilder<F>
where
    F: FormatSpec,
{
    /// Create a new [`ParCompressBuilder`] object.
    pub fn new() -> Self {
        Self {
            buffer_size: F::DEFAULT_BUFSIZE,
            num_threads: num_cpus::get(),
            compression_level: Compression::new(3),
            format: F::new(),
            pin_threads: None,
        }
    }

    /// Set the [`buffer_size`](ParCompressBuilder.buffer_size). Must be >= [`DICT_SIZE`].
    ///
    /// # Errors
    /// - [`GzpError::BufferSize`] error if selected buffer size is less than [`DICT_SIZE`].
    pub fn buffer_size(mut self, buffer_size: usize) -> Result<Self, GzpError> {
        if buffer_size < DICT_SIZE {
            return Err(GzpError::BufferSize(buffer_size, DICT_SIZE));
        }
        self.buffer_size = buffer_size;
        Ok(self)
    }

    /// Set the [`num_threads`](ParCompressBuilder.num_threads) that will be used for compression.
    ///
    /// Note that one additional thread will be used for writing. Threads equal to `num_threads`
    /// will be spun up in the background and will remain blocking and waiting for blocks to compress
    /// until ['finish`](ParCompress.finish) is called.
    ///
    /// # Errors
    /// - [`GzpError::NumThreads`] error if 0 threads selected.
    pub fn num_threads(mut self, num_threads: usize) -> Result<Self, GzpError> {
        if num_threads == 0 {
            return Err(GzpError::NumThreads(num_threads));
        }
        self.num_threads = num_threads;
        Ok(self)
    }

    /// Set the [`compression_level`](ParCompressBuilder.compression_level).
    pub fn compression_level(mut self, compression_level: Compression) -> Self {
        self.compression_level = compression_level;
        self
    }

    /// Set the [`pin_threads`](ParCompressBuilder.pin_threads).
    pub fn pin_threads(mut self, pin_threads: Option<usize>) -> Self {
        if core_affinity::get_core_ids().is_none() {
            warn!("Pinning threads is not supported on your platform. Please see core_affinity_rs. No threads will be pinned, but everything will work.");
            self.pin_threads = None;
        } else {
            self.pin_threads = pin_threads;
        }
        self
    }

    /// Create a configured [`ParCompress`] object.
    pub fn from_writer<W: Write + Send + 'static>(self, writer: W) -> ParCompress<'static, F, W> {
        let (tx_compressor, rx_compressor) = bounded(self.num_threads * 2);
        let (tx_writer, rx_writer) = bounded(self.num_threads * 2);
        let buffer_size = self.buffer_size;
        let comp_level = self.compression_level;
        let pin_threads = self.pin_threads;
        let format = self.format;
        let num_threads = self.num_threads;
        let handle = std::thread::spawn(move || {
            ParCompress::run(
                &rx_compressor,
                &rx_writer,
                writer,
                num_threads,
                comp_level,
                format,
                pin_threads,
            )
        });
        ParCompress {
            handle: Some(MaybeScopedJoinHandle::Static(handle)),
            tx_compressor: Some(tx_compressor),
            tx_writer: Some(tx_writer),
            dictionary: None,
            buffer: BytesMut::with_capacity(buffer_size),
            buffer_size,
            format,
        }
    }

    /// Create a configured [`ParCompress`] object.
    ///
    /// This is similar to [`from_writer`](ParCompressBuilder::from_writer) but allows
    /// the writer to be borrowed for the lifetime of the specified scope, rather than
    /// requiring it to be `'static`.
    ///
    /// ```rust
    /// use gzp::par::compress::ParCompressBuilder;
    /// use gzp::deflate::Gzip;
    /// use gzp::ZWriter;
    /// use std::io::Write;
    ///
    /// let mut output = Vec::new();
    ///
    /// std::thread::scope(|scope| {
    ///     let mut compressor = ParCompressBuilder::<Gzip>::new()
    ///         .from_borrowed_writer(&mut output, scope);
    ///     
    ///     compressor.write_all(b"Data to compress").unwrap();
    ///     compressor.finish().unwrap()
    /// });
    /// ````
    pub fn from_borrowed_writer<'scope, 'env, W: Write + Send + 'scope>(
        self,
        writer: W,
        scope: &'scope Scope<'scope, 'env>,
    ) -> ParCompress<'scope, F, W> {
        let (tx_compressor, rx_compressor) = bounded(self.num_threads * 2);
        let (tx_writer, rx_writer) = bounded(self.num_threads * 2);
        let buffer_size = self.buffer_size;
        let comp_level = self.compression_level;
        let pin_threads = self.pin_threads;
        let format = self.format;
        let num_threads = self.num_threads;
        let handle = scope.spawn(move || {
            ParCompress::run(
                &rx_compressor,
                &rx_writer,
                writer,
                num_threads,
                comp_level,
                format,
                pin_threads,
            )
        });
        ParCompress {
            handle: Some(MaybeScopedJoinHandle::Scoped(handle)),
            tx_compressor: Some(tx_compressor),
            tx_writer: Some(tx_writer),
            dictionary: None,
            buffer: BytesMut::with_capacity(buffer_size),
            buffer_size,
            format,
        }
    }
}

impl<F> Default for ParCompressBuilder<F>
where
    F: FormatSpec,
{
    fn default() -> Self {
        Self::new()
    }
}

enum MaybeScopedJoinHandle<'scope, T> {
    Static(JoinHandle<T>),
    Scoped(ScopedJoinHandle<'scope, T>),
}

impl<'scope, T> MaybeScopedJoinHandle<'scope, T> {
    fn join(self) -> Result<T, Box<dyn std::any::Any + Send>> {
        match self {
            MaybeScopedJoinHandle::Static(handle) => handle.join(),
            MaybeScopedJoinHandle::Scoped(handle) => handle.join(),
        }
    }
}

#[allow(unused)]
pub struct ParCompress<'scope, F, W>
where
    F: FormatSpec,
    W: Write,
{
    handle: Option<MaybeScopedJoinHandle<'scope, Result<W, GzpError>>>,
    tx_compressor: Option<Sender<Message<F::C>>>,
    tx_writer: Option<Sender<Receiver<CompressResult<F::C>>>>,
    buffer: BytesMut,
    dictionary: Option<Bytes>,
    buffer_size: usize,
    format: F,
}

impl<'scope, F, W> ParCompress<'scope, F, W>
where
    F: FormatSpec,
    W: Write,
{
    /// Create a builder to configure the [`ParCompress`] runtime.
    pub fn builder() -> ParCompressBuilder<F> {
        ParCompressBuilder::new()
    }

    /// Launch threads to compress chunks and coordinate sending compressed results
    /// to the writer.
    #[allow(clippy::needless_collect)]
    fn run(
        rx: &Receiver<Message<F::C>>,
        rx_writer: &Receiver<Receiver<CompressResult<F::C>>>,
        mut writer: W,
        num_threads: usize,
        compression_level: Compression,
        format: F,
        pin_threads: Option<usize>,
    ) -> Result<W, GzpError>
    where
        W: Write + Send,
    {
        let (core_ids, pin_threads) = if let Some(core_ids) = core_affinity::get_core_ids() {
            (core_ids, pin_threads)
        } else {
            // Handle the case where core affinity doesn't work for a platform.
            // We test and warn in the constructors for this case, so no warning should be needed here.
            (vec![], None)
        };
        let handles: Vec<JoinHandle<Result<(), GzpError>>> = (0..num_threads)
            .map(|i| {
                let rx = rx.clone();
                let core_ids = core_ids.clone();
                std::thread::spawn(move || -> Result<(), GzpError> {
                    if let Some(pin_at) = pin_threads {
                        if let Some(id) = core_ids.get(pin_at + i) {
                            core_affinity::set_for_current(*id);
                        }
                    }

                    let mut compressor = format.create_compressor(compression_level)?;
                    while let Ok(m) = rx.recv() {
                        let chunk = &m.buffer;
                        let buffer = format.encode(
                            chunk,
                            &mut compressor,
                            compression_level,
                            m.dictionary.as_ref(),
                            m.is_last,
                        )?;
                        let mut check = F::create_check();
                        check.update(chunk);

                        m.oneshot
                            .send(Ok::<(F::C, Vec<u8>), GzpError>((check, buffer)))
                            .map_err(|_e| GzpError::ChannelSend)?;
                    }
                    Ok(())
                })
            })
            // This collect is needed to force the evaluation, otherwise this thread will block on writes waiting
            // for data to show up that will never come since the iterator is lazy.
            .collect();

        // Writer
        writer.write_all(&format.header(compression_level))?;
        let mut running_check = F::create_check();
        while let Ok(chunk_chan) = rx_writer.recv() {
            let chunk_chan: Receiver<CompressResult<F::C>> = chunk_chan;
            let (check, chunk) = chunk_chan.recv()??;
            running_check.combine(&check);
            writer.write_all(&chunk)?;
        }
        let footer = format.footer(&running_check);
        writer.write_all(&footer)?;
        writer.flush()?;

        // Gracefully shutdown the compression threads
        handles
            .into_iter()
            .try_for_each(|handle| match handle.join() {
                Ok(result) => result,
                Err(e) => std::panic::resume_unwind(e),
            })?;
        Ok(writer)
    }

    /// Flush this output stream, ensuring all intermediately buffered contents are sent.
    ///
    /// If this is the last buffer to be sent, set `is_last` to false to trigger compression
    /// stream completion.
    ///
    /// # Panics
    /// - If called after `finish`
    fn flush_last(&mut self, is_last: bool) -> std::io::Result<()> {
        loop {
            let b = self
                .buffer
                .split_to(std::cmp::min(self.buffer.len(), self.buffer_size))
                .freeze();
            let (mut m, r) = Message::new_parts(b, self.dictionary.take());
            if is_last && self.buffer.is_empty() {
                m.is_last = true;
            }

            if m.buffer.len() >= DICT_SIZE && !m.is_last && self.format.needs_dict() {
                self.dictionary = Some(m.buffer.slice(m.buffer.len() - DICT_SIZE..));
            }

            let send_result = self.tx_writer.as_ref().unwrap().send(r);
            if let Err(error) = send_result {
                return Err(self.recover_send_error(error));
            }

            let send_result = self.tx_compressor.as_ref().unwrap().send(m);
            if let Err(error) = send_result {
                return Err(self.recover_send_error(error));
            }
            if self.buffer.is_empty() {
                break;
            }
        }
        Ok(())
    }

    /// Shut down the pipeline and recover its error after a channel send fails.
    ///
    /// Taking all three resources before joining also disarms [`Drop`], so an error
    /// returned by `write`, `flush`, or `finish` cannot trigger a second teardown.
    #[cold]
    fn recover_send_error<T>(&mut self, send_error: flume::SendError<T>) -> io::Error {
        let handle = self.handle.take().unwrap();
        drop(send_error);
        drop(self.tx_compressor.take());
        drop(self.tx_writer.take());

        let error = match handle.join() {
            Ok(result) => result.map(|_| ()),
            Err(error) => std::panic::resume_unwind(error),
        };
        match error {
            Ok(()) => std::panic::resume_unwind(Box::new(error)),
            Err(GzpError::Io(error)) => error,
            Err(error) => io::Error::other(error),
        }
    }
}

impl<'scope, F, W> ZWriter<W> for ParCompress<'scope, F, W>
where
    F: FormatSpec,
    W: Write,
{
    /// Flush the buffers and wait on all threads to finish working.
    ///
    /// This *MUST* be called before the [`ParCompress`] object goes out of scope.
    ///
    /// # Errors
    /// - [`GzpError`] if there is an issue flushing the last blocks or an issue joining on the writer thread
    ///
    fn finish(&mut self) -> Result<W, GzpError> {
        self.flush_last(true)?;

        // while !self.tx_compressor.as_ref().unwrap().is_empty() {}
        // while !self.tx_writer.as_ref().unwrap().is_empty() {}
        drop(self.tx_compressor.take());
        drop(self.tx_writer.take());
        match self.handle.take().unwrap().join() {
            Ok(result) => result,
            Err(e) => std::panic::resume_unwind(e),
        }
    }
}

impl<'scope, F, W> Drop for ParCompress<'scope, F, W>
where
    F: FormatSpec,
    W: Write,
{
    fn drop(&mut self) {
        if self.tx_compressor.is_some() && self.tx_writer.is_some() && self.handle.is_some() {
            self.finish().unwrap();
        }
        // Resources already cleaned up if channels and handle are None
    }
}

impl<'scope, F, W> Write for ParCompress<'scope, F, W>
where
    F: FormatSpec,
    W: Write,
{
    /// Write a buffer into this writer, returning how many bytes were written.
    ///
    /// # Panics
    /// - If called after calling `finish`
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.buffer.extend_from_slice(buf);
        while self.buffer.len() > self.buffer_size {
            let b = self.buffer.split_to(self.buffer_size).freeze();
            let (m, r) = Message::new_parts(b, self.dictionary.take());
            // Bytes uses and ARC, this is O(1) to get the last 32k bytes from teh previous chunk
            self.dictionary = if self.format.needs_dict() {
                Some(m.buffer.slice(m.buffer.len() - DICT_SIZE..))
            } else {
                None
            };
            let send_result = self.tx_writer.as_ref().unwrap().send(r);
            if let Err(error) = send_result {
                return Err(self.recover_send_error(error));
            }

            let send_result = self.tx_compressor.as_ref().unwrap().send(m);
            if let Err(error) = send_result {
                return Err(self.recover_send_error(error));
            }
            self.buffer
                .reserve(self.buffer_size.saturating_sub(self.buffer.len()));
        }

        Ok(buf.len())
    }

    /// Flush this output stream, ensuring all intermediately buffered contents are sent.
    fn flush(&mut self) -> std::io::Result<()> {
        self.flush_last(false)
    }
}

#[cfg(all(test, feature = "deflate"))]
mod tests {
    use std::io::{self, Write};
    use std::panic::{catch_unwind, AssertUnwindSafe};
    use std::sync::mpsc;
    use std::time::{Duration, Instant};

    use crate::deflate::Gzip;
    use crate::{GzpError, ZWriter};

    use super::{MaybeScopedJoinHandle, ParCompress, ParCompressBuilder};

    #[derive(Debug)]
    struct FailingWriter {
        write_attempted: mpsc::Sender<()>,
    }

    impl Write for FailingWriter {
        fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
            let _ = self.write_attempted.send(());
            Err(io::Error::new(io::ErrorKind::BrokenPipe, "sink is gone"))
        }

        fn flush(&mut self) -> io::Result<()> {
            Ok(())
        }
    }

    #[derive(Debug)]
    struct PanickingWriter {
        write_attempted: mpsc::Sender<()>,
    }

    impl Write for PanickingWriter {
        fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
            let _ = self.write_attempted.send(());
            panic!("sink panicked");
        }

        fn flush(&mut self) -> io::Result<()> {
            Ok(())
        }
    }

    fn wait_for_writer_thread<W: Write>(compressor: &ParCompress<'_, Gzip, W>) {
        let deadline = Instant::now() + Duration::from_secs(5);
        loop {
            let is_finished = match compressor.handle.as_ref().unwrap() {
                MaybeScopedJoinHandle::Static(handle) => handle.is_finished(),
                MaybeScopedJoinHandle::Scoped(handle) => handle.is_finished(),
            };
            if is_finished {
                return;
            }
            assert!(Instant::now() < deadline, "writer thread did not finish");
            std::thread::yield_now();
        }
    }

    fn compressor_with_failed_writer() -> ParCompress<'static, Gzip, FailingWriter> {
        let (write_attempted, write_attempted_rx) = mpsc::channel();
        let compressor = ParCompressBuilder::new()
            .num_threads(1)
            .unwrap()
            .from_writer(FailingWriter { write_attempted });

        write_attempted_rx
            .recv_timeout(Duration::from_secs(5))
            .expect("writer did not attempt the gzip header");
        wait_for_writer_thread(&compressor);
        compressor
    }

    fn assert_pipeline_closed<W: Write>(compressor: &ParCompress<'_, Gzip, W>) {
        assert!(compressor.handle.is_none());
        assert!(compressor.tx_compressor.is_none());
        assert!(compressor.tx_writer.is_none());
    }

    fn assert_sink_error(error: GzpError) {
        match error {
            GzpError::Io(error) => {
                assert_eq!(error.kind(), io::ErrorKind::BrokenPipe);
                assert_eq!(error.to_string(), "sink is gone");
            }
            error => panic!("expected the sink's I/O error, got {:?}", error),
        }
    }

    #[test]
    fn finish_error_is_not_retried_by_drop() {
        let mut compressor = compressor_with_failed_writer();
        let result = catch_unwind(AssertUnwindSafe(move || {
            let result = compressor.finish();
            assert_pipeline_closed(&compressor);
            drop(compressor);
            result
        }));

        let error = result
            .expect("dropping after a failed finish must not panic")
            .expect_err("finish should report the sink error");
        assert_sink_error(error);
    }

    #[test]
    fn flush_error_is_not_retried_by_drop() {
        let mut compressor = compressor_with_failed_writer();
        let result = catch_unwind(AssertUnwindSafe(move || {
            let result = compressor.flush();
            assert_pipeline_closed(&compressor);
            drop(compressor);
            result
        }));

        let error = result
            .expect("dropping after a failed flush must not panic")
            .expect_err("flush should report the sink error");
        assert_eq!(error.kind(), io::ErrorKind::BrokenPipe);
        assert_eq!(error.to_string(), "sink is gone");
    }

    #[test]
    fn write_error_still_recovers_the_sink_error() {
        let mut compressor = compressor_with_failed_writer();
        let input = vec![0; compressor.buffer_size + 1];
        let result = catch_unwind(AssertUnwindSafe(move || {
            let result = compressor.write(&input);
            assert_pipeline_closed(&compressor);
            drop(compressor);
            result
        }));

        let error = result
            .expect("dropping after a failed write must not panic")
            .expect_err("write should report the sink error");
        assert_eq!(error.kind(), io::ErrorKind::BrokenPipe);
        assert_eq!(error.to_string(), "sink is gone");
    }

    #[test]
    fn scoped_finish_error_is_not_retried_by_drop() {
        let result = catch_unwind(AssertUnwindSafe(|| {
            std::thread::scope(|scope| {
                let (write_attempted, write_attempted_rx) = mpsc::channel();
                let mut compressor = ParCompressBuilder::new()
                    .num_threads(1)
                    .unwrap()
                    .from_borrowed_writer(FailingWriter { write_attempted }, scope);

                write_attempted_rx
                    .recv_timeout(Duration::from_secs(5))
                    .expect("writer did not attempt the gzip header");
                wait_for_writer_thread(&compressor);

                let error = compressor
                    .finish()
                    .expect_err("finish should report the sink error");
                assert_pipeline_closed(&compressor);
                assert_sink_error(error);
                drop(compressor);
            });
        }));

        result.expect("dropping a failed scoped compressor must not panic");
    }

    #[test]
    fn writer_panic_is_resumed_only_once() {
        let (write_attempted, write_attempted_rx) = mpsc::channel();
        let mut compressor = ParCompressBuilder::new()
            .num_threads(1)
            .unwrap()
            .from_writer(PanickingWriter { write_attempted });
        write_attempted_rx
            .recv_timeout(Duration::from_secs(5))
            .expect("writer did not attempt the gzip header");
        wait_for_writer_thread(&compressor);

        let result = catch_unwind(AssertUnwindSafe(move || {
            let panic = catch_unwind(AssertUnwindSafe(|| compressor.finish()));
            assert_pipeline_closed(&compressor);
            drop(compressor);
            panic
        }))
        .expect("dropping after resuming the writer panic must not panic again");

        let panic = result.expect_err("the writer panic should be resumed");
        let message = panic
            .downcast_ref::<&str>()
            .copied()
            .or_else(|| panic.downcast_ref::<String>().map(String::as_str));
        assert_eq!(message, Some("sink panicked"));
    }
}