gzippy 0.8.0

The fastest parallel gzip. Drop-in replacement for gzip and pigz, and a Rust library.
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
//! Compression Roundtrip Oracle
//!
//! Verifies all compression paths produce valid gzip output that decompresses
//! to the original data. Compares compression ratios and tests thread independence.
//!
//! Layer 0: CompressOracle — known test data with expected properties
//! Layer 1: Roundtrip correctness — every path produces decompressible output
//! Layer 2: Ratio comparison — no path produces unnecessarily large output
//! Layer 3: Thread independence — same ratio regardless of thread count

#[cfg(test)]
mod tests {
    #![allow(unused_variables)]

    use crate::compress::parallel::{compress_single_member, GzipHeaderInfo, ParallelGzEncoder};
    use crate::compress::pipelined::PipelinedGzEncoder;
    use std::io::{Cursor, Read, Write};

    // =========================================================================
    // Layer 0: CompressOracle — test data with known properties
    // =========================================================================

    struct CompressOracle {
        /// Original uncompressed data.
        original: Vec<u8>,
        /// What type of data this is (for diagnostics).
        name: &'static str,
    }

    impl CompressOracle {
        fn new(name: &'static str, data: Vec<u8>) -> Self {
            Self {
                original: data,
                name,
            }
        }

        /// Verify that compressed data decompresses to the original.
        fn verify_roundtrip(&self, compressed: &[u8], path_name: &str) {
            let decompressed = decompress_reference(compressed);
            assert_eq!(
                decompressed.len(),
                self.original.len(),
                "{} [{}]: decompressed size {} != original {}",
                self.name,
                path_name,
                decompressed.len(),
                self.original.len()
            );
            assert_eq!(
                decompressed, self.original,
                "{} [{}]: decompressed content differs from original",
                self.name, path_name
            );
        }

        fn ratio(&self, compressed: &[u8]) -> f64 {
            compressed.len() as f64 / self.original.len() as f64
        }
    }

    fn decompress_reference(gz_data: &[u8]) -> Vec<u8> {
        use flate2::read::MultiGzDecoder;
        let mut decoder = MultiGzDecoder::new(gz_data);
        let mut output = Vec::new();
        decoder.read_to_end(&mut output).unwrap();
        output
    }

    fn make_literal_data(size: usize) -> Vec<u8> {
        (0..size)
            .map(|i| (i.wrapping_mul(2654435761) >> 24) as u8)
            .collect()
    }

    fn make_rle_data(size: usize) -> Vec<u8> {
        let mut data = Vec::with_capacity(size);
        for i in 0..(size / 1000).max(1) {
            let byte = (i % 256) as u8;
            data.extend(std::iter::repeat_n(byte, 1000.min(size - data.len())));
        }
        data.truncate(size);
        data
    }

    fn make_mixed_data(size: usize) -> Vec<u8> {
        let mut data = Vec::with_capacity(size);
        let mut rng: u64 = 0xdeadbeef;
        let phrases: &[&[u8]] = &[
            b"the quick brown fox jumps over the lazy dog. ",
            b"pack my box with five dozen liquor jugs! ",
            b"0123456789 abcdefghijklmnopqrstuvwxyz\n",
        ];
        while data.len() < size {
            rng = rng.wrapping_mul(6364136223846793005).wrapping_add(1);
            if (rng >> 32) % 5 < 2 {
                data.push((rng >> 16) as u8);
            } else {
                let phrase = phrases[((rng >> 24) as usize) % phrases.len()];
                let remaining = size - data.len();
                data.extend_from_slice(&phrase[..remaining.min(phrase.len())]);
            }
        }
        data.truncate(size);
        data
    }

    // =========================================================================
    // Compression path helpers
    // =========================================================================

    /// Compress with libdeflate via compress_single_member (L1-L5 parallel path).
    fn compress_libdeflate(data: &[u8], level: u32) -> Vec<u8> {
        let mut output = Vec::new();
        let header = GzipHeaderInfo::default();
        compress_single_member(&mut output, data, level, &header).unwrap();
        output
    }

    /// Compress with ParallelGzEncoder (multi-threaded libdeflate blocks).
    fn compress_parallel(data: &[u8], level: u32, threads: usize) -> Vec<u8> {
        let encoder = ParallelGzEncoder::new(level, threads);
        let mut output = Vec::new();
        encoder.compress(Cursor::new(data), &mut output).unwrap();
        output
    }

    /// Compress with PipelinedGzEncoder (L6-L9 zlib-ng streaming).
    fn compress_pipelined(data: &[u8], level: u32, threads: usize) -> Vec<u8> {
        let encoder = PipelinedGzEncoder::new(level, threads);
        let mut output = Vec::new();
        encoder.compress(Cursor::new(data), &mut output).unwrap();
        output
    }

    /// Compress with flate2 (reference implementation).
    fn compress_flate2(data: &[u8], level: u32) -> Vec<u8> {
        let mut encoder =
            flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::new(level));
        encoder.write_all(data).unwrap();
        encoder.finish().unwrap()
    }

    // =========================================================================
    // Layer 0 tests: Oracle self-consistency
    // =========================================================================

    #[test]
    fn test_compress_oracle_setup() {
        let oracles = [
            CompressOracle::new("literals", make_literal_data(2_000_000)),
            CompressOracle::new("rle", make_rle_data(2_000_000)),
            CompressOracle::new("mixed", make_mixed_data(2_000_000)),
        ];

        for oracle in &oracles {
            // Reference roundtrip
            let compressed = compress_flate2(&oracle.original, 6);
            oracle.verify_roundtrip(&compressed, "flate2-L6");
            eprintln!(
                "{}: {} bytes, flate2 L6 ratio {:.1}%",
                oracle.name,
                oracle.original.len(),
                oracle.ratio(&compressed) * 100.0
            );
        }
    }

    // =========================================================================
    // Layer 1: Roundtrip correctness — every path produces valid output
    // =========================================================================

    #[test]
    fn test_roundtrip_libdeflate_levels() {
        let oracle = CompressOracle::new("mixed", make_mixed_data(2_000_000));

        for level in [1, 3, 5] {
            let compressed = compress_libdeflate(&oracle.original, level);
            oracle.verify_roundtrip(&compressed, &format!("libdeflate-L{}", level));
            eprintln!(
                "  libdeflate L{}: ratio {:.1}%",
                level,
                oracle.ratio(&compressed) * 100.0
            );
        }
    }

    #[test]
    fn test_roundtrip_parallel_levels() {
        let oracle = CompressOracle::new("mixed", make_mixed_data(2_000_000));

        for level in [1, 3, 5] {
            let compressed = compress_parallel(&oracle.original, level, 4);
            oracle.verify_roundtrip(&compressed, &format!("parallel-L{}-T4", level));
            eprintln!(
                "  parallel L{} T4: ratio {:.1}%",
                level,
                oracle.ratio(&compressed) * 100.0
            );
        }
    }

    #[test]
    fn test_roundtrip_pipelined_levels() {
        let oracle = CompressOracle::new("mixed", make_mixed_data(2_000_000));

        for level in [6, 7, 9] {
            let compressed = compress_pipelined(&oracle.original, level, 4);
            oracle.verify_roundtrip(&compressed, &format!("pipelined-L{}-T4", level));
            eprintln!(
                "  pipelined L{} T4: ratio {:.1}%",
                level,
                oracle.ratio(&compressed) * 100.0
            );
        }
    }

    // =========================================================================
    // Layer 2: Ratio comparison across paths
    // =========================================================================

    #[test]
    fn test_ratio_comparison() {
        let oracle = CompressOracle::new("mixed", make_mixed_data(4_000_000));

        eprintln!(
            "=== Compression Ratio Comparison ({} bytes) ===",
            oracle.original.len()
        );
        eprintln!("{:<25} {:>10} {:>10}", "Path", "Size", "Ratio");
        eprintln!("{}", "-".repeat(48));

        let paths: Vec<(&str, Vec<u8>)> = vec![
            ("flate2-L1", compress_flate2(&oracle.original, 1)),
            ("flate2-L6", compress_flate2(&oracle.original, 6)),
            ("flate2-L9", compress_flate2(&oracle.original, 9)),
            ("libdeflate-L1", compress_libdeflate(&oracle.original, 1)),
            ("libdeflate-L5", compress_libdeflate(&oracle.original, 5)),
            ("parallel-L1-T4", compress_parallel(&oracle.original, 1, 4)),
            ("parallel-L5-T4", compress_parallel(&oracle.original, 5, 4)),
            (
                "pipelined-L6-T4",
                compress_pipelined(&oracle.original, 6, 4),
            ),
            (
                "pipelined-L9-T4",
                compress_pipelined(&oracle.original, 9, 4),
            ),
        ];

        for (name, compressed) in &paths {
            oracle.verify_roundtrip(compressed, name);
            eprintln!(
                "{:<25} {:>10} {:>9.1}%",
                name,
                compressed.len(),
                oracle.ratio(compressed) * 100.0
            );
        }
    }

    // =========================================================================
    // Layer 3: Thread independence — same output regardless of thread count
    // =========================================================================

    #[test]
    fn test_parallel_thread_independence() {
        let oracle = CompressOracle::new("mixed", make_mixed_data(2_000_000));

        // All thread counts should produce decompressible output
        let thread_counts = [1, 2, 4, 8];
        let mut ratios = Vec::new();

        for &threads in &thread_counts {
            let compressed = compress_parallel(&oracle.original, 1, threads);
            oracle.verify_roundtrip(&compressed, &format!("parallel-L1-T{}", threads));
            let ratio = oracle.ratio(&compressed);
            ratios.push((threads, ratio));
        }

        eprintln!("parallel L1 thread scaling:");
        for (threads, ratio) in &ratios {
            eprintln!("  T{}: ratio {:.1}%", threads, ratio * 100.0);
        }

        // Ratios should be within 5% of each other (different block boundaries)
        let min_ratio = ratios.iter().map(|(_, r)| *r).fold(f64::MAX, f64::min);
        let max_ratio = ratios.iter().map(|(_, r)| *r).fold(f64::MIN, f64::max);
        let spread = (max_ratio - min_ratio) / min_ratio;

        assert!(
            spread < 0.15,
            "ratio spread {:.1}% is too large across thread counts (min {:.1}%, max {:.1}%)",
            spread * 100.0,
            min_ratio * 100.0,
            max_ratio * 100.0
        );
    }

    #[test]
    fn test_pipelined_thread_independence() {
        let oracle = CompressOracle::new("mixed", make_mixed_data(2_000_000));

        let thread_counts = [1, 2, 4];
        let mut ratios = Vec::new();

        for &threads in &thread_counts {
            let compressed = compress_pipelined(&oracle.original, 6, threads);
            oracle.verify_roundtrip(&compressed, &format!("pipelined-L6-T{}", threads));
            let ratio = oracle.ratio(&compressed);
            ratios.push((threads, ratio));
        }

        eprintln!("pipelined L6 thread scaling:");
        for (threads, ratio) in &ratios {
            eprintln!("  T{}: ratio {:.1}%", threads, ratio * 100.0);
        }

        let min_ratio = ratios.iter().map(|(_, r)| *r).fold(f64::MAX, f64::min);
        let max_ratio = ratios.iter().map(|(_, r)| *r).fold(f64::MIN, f64::max);
        let spread = (max_ratio - min_ratio) / min_ratio;

        assert!(
            spread < 0.05,
            "pipelined ratio spread {:.1}% is too large across thread counts",
            spread * 100.0
        );
    }

    // =========================================================================
    // Pipelined block size boundary tests
    // =========================================================================

    #[test]
    fn test_pipelined_block_size_below_50mb() {
        let data = make_mixed_data(49 * 1024 * 1024);
        let compressed = compress_pipelined(&data, 6, 2);
        let decompressed = decompress_reference(&compressed);
        assert_eq!(decompressed.len(), data.len());
        assert_eq!(decompressed, data);
    }

    #[test]
    fn test_pipelined_block_size_above_50mb() {
        let data = make_mixed_data(51 * 1024 * 1024);
        let compressed = compress_pipelined(&data, 6, 2);
        let decompressed = decompress_reference(&compressed);
        assert_eq!(decompressed.len(), data.len());
        assert_eq!(decompressed, data);
    }

    // =========================================================================
    // T1 pipelined deadlock regression test
    // =========================================================================

    #[test]
    fn test_t1_pipelined_completes_without_deadlock() {
        use std::time::Duration;
        let data = make_mixed_data(500_000);
        let (tx, rx) = std::sync::mpsc::channel();
        let data_clone = data.clone();
        let handle = std::thread::spawn(move || {
            let compressed = compress_pipelined(&data_clone, 6, 1);
            tx.send(compressed).unwrap();
        });
        match rx.recv_timeout(Duration::from_secs(30)) {
            Ok(compressed) => {
                let decompressed = decompress_reference(&compressed);
                assert_eq!(decompressed, data, "T1 pipelined output must match");
            }
            Err(_) => panic!("T1 pipelined compression deadlocked (>30s)"),
        }
        handle.join().unwrap();
    }

    // =========================================================================
    // Ratio probe routing tests
    // =========================================================================

    #[test]
    fn test_ratio_probe_libdeflate_for_incompressible_data() {
        // Random data is incompressible (ratio >= 10%) → libdeflate path
        let data = make_literal_data(200_000);
        let level = 3u32;
        let lvl = libdeflater::CompressionLvl::new(level as i32).unwrap();
        let mut comp = libdeflater::Compressor::new(lvl);
        let bound = comp.deflate_compress_bound(65536);
        let mut out = vec![0u8; bound];
        let actual = comp
            .deflate_compress(&data[..65536], &mut out)
            .unwrap_or(65536);
        let ratio = actual as f64 / 65536.0;
        assert!(
            ratio >= 0.10,
            "random data ratio {:.3} should be >= 0.10 (libdeflate path)",
            ratio
        );
    }

    #[test]
    fn test_ratio_probe_flate2_for_highly_compressible() {
        // All-zeros data should route to flate2 (ratio < 10%)
        let data = vec![0u8; 200_000];
        let level = 3u32;
        let lvl = libdeflater::CompressionLvl::new(level as i32).unwrap();
        let mut comp = libdeflater::Compressor::new(lvl);
        let bound = comp.deflate_compress_bound(65536);
        let mut out = vec![0u8; bound];
        let actual = comp
            .deflate_compress(&data[..65536], &mut out)
            .unwrap_or(65536);
        let ratio = actual as f64 / 65536.0;
        assert!(
            ratio < 0.10,
            "all-zeros ratio {:.3} should be < 0.10 (flate2 path)",
            ratio
        );
    }

    #[test]
    fn test_both_ratio_paths_produce_valid_gzip() {
        // Regardless of which path the ratio probe chooses,
        // the output must be valid gzip that decompresses correctly.
        let normal_data = make_mixed_data(200_000);
        let compressed_normal = compress_libdeflate(&normal_data, 3);
        let dec_normal = decompress_reference(&compressed_normal);
        assert_eq!(dec_normal, normal_data, "libdeflate path output must match");

        let zeros = vec![0u8; 200_000];
        let compressed_zeros = compress_flate2(&zeros, 3);
        let dec_zeros = decompress_reference(&compressed_zeros);
        assert_eq!(dec_zeros, zeros, "flate2 path output must match");
    }
}