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
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
//! Inflate Performance Oracle
//!
//! Block-by-block comparison of consume_first_decode (pure Rust) vs libdeflate
//! (C FFI) to identify exactly which block types and data patterns cause the
//! performance gap.
//!
//! Layer 0: InflateOracle — extract individual deflate blocks with metadata
//! Layer 1: Correctness — verify both implementations produce identical output
//! Layer 2: Per-block timing — time each block with both implementations
//! Layer 3: Pattern analysis — categorize blocks and correlate with speed gaps

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

    use crate::decompress::inflate::consume_first_decode::{inflate_consume_first, Bits};
    use crate::decompress::scan_inflate::scan_deflate_fast;
    use std::io::Write;
    use std::time::Instant;

    // =========================================================================
    // Layer 0: InflateOracle — block-level ground truth
    // =========================================================================

    #[derive(Clone)]
    #[allow(dead_code)]
    struct BlockInfo {
        index: usize,
        btype: u8,
        bfinal: bool,
        start_byte: usize,
        start_bit_offset: u8,
        output_size: usize,
        output_offset: usize,
    }

    struct InflateOracle {
        deflate_data: Vec<u8>,
        expected_output: Vec<u8>,
        blocks: Vec<BlockInfo>,
    }

    impl InflateOracle {
        fn from_gzip(gzip_data: &[u8]) -> Self {
            let header_size =
                crate::decompress::parallel::marker_decode::skip_gzip_header(gzip_data)
                    .expect("valid gzip header");
            let deflate_data = &gzip_data[header_size..gzip_data.len() - 8];

            // Scan to get block boundaries
            let scan = scan_deflate_fast(deflate_data, 1, 0).expect("scan should succeed");

            // Parse blocks to get per-block metadata
            let blocks = parse_blocks(deflate_data, scan.total_output_size);

            // Full decode for expected output
            let mut output = vec![0u8; scan.total_output_size + 65536];
            let size = inflate_consume_first(deflate_data, &mut output).expect("inflate failed");
            output.truncate(size);

            Self {
                deflate_data: deflate_data.to_vec(),
                expected_output: output,
                blocks,
            }
        }

        fn from_raw(data: &[u8]) -> Self {
            let gz = compress_gzip(data);
            Self::from_gzip(&gz)
        }

        fn block_type_name(btype: u8) -> &'static str {
            match btype {
                0 => "stored",
                1 => "fixed",
                2 => "dynamic",
                _ => "unknown",
            }
        }

        fn summary(&self) {
            let mut type_counts = [0usize; 3];
            let mut type_bytes = [0usize; 3];
            for b in &self.blocks {
                if (b.btype as usize) < 3 {
                    type_counts[b.btype as usize] += 1;
                    type_bytes[b.btype as usize] += b.output_size;
                }
            }
            eprintln!(
                "inflate oracle: {} blocks, {} bytes output",
                self.blocks.len(),
                self.expected_output.len()
            );
            for t in 0..3 {
                if type_counts[t] > 0 {
                    eprintln!(
                        "  {}: {} blocks, {} bytes ({:.1}%)",
                        Self::block_type_name(t as u8),
                        type_counts[t],
                        type_bytes[t],
                        type_bytes[t] as f64 / self.expected_output.len() as f64 * 100.0
                    );
                }
            }
        }
    }

    fn compress_gzip(data: &[u8]) -> Vec<u8> {
        let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
        encoder.write_all(data).unwrap();
        encoder.finish().unwrap()
    }

    /// Parse a deflate stream to extract per-block metadata.
    fn parse_blocks(deflate_data: &[u8], total_output: usize) -> Vec<BlockInfo> {
        let mut blocks = Vec::new();
        let mut bits = Bits::new(deflate_data);
        let mut output = vec![0u8; total_output + 65536];
        let mut out_pos = 0usize;
        let mut index = 0;

        loop {
            let block_start_byte = bits.pos;
            let block_start_bits = bits.bitsleft as u8;

            if bits.available() < 3 {
                bits.refill();
            }

            let header = bits.peek();
            let bfinal = (header & 1) != 0;
            let btype = ((header >> 1) & 3) as u8;
            bits.consume(3);

            let prev_pos = out_pos;
            match btype {
                0 => {
                    out_pos = crate::decompress::inflate::consume_first_decode::decode_stored_pub(
                        &mut bits,
                        &mut output,
                        out_pos,
                    )
                    .unwrap()
                }
                1 => {
                    out_pos = crate::decompress::inflate::consume_first_decode::decode_fixed_pub(
                        &mut bits,
                        &mut output,
                        out_pos,
                    )
                    .unwrap()
                }
                2 => {
                    out_pos = crate::decompress::inflate::consume_first_decode::decode_dynamic_pub(
                        &mut bits,
                        &mut output,
                        out_pos,
                    )
                    .unwrap()
                }
                _ => break,
            }

            // Compute start bit position from saved state
            let real_bitsleft = (block_start_bits as usize) & 0x3F;
            let start_bit_pos = block_start_byte * 8 - real_bitsleft;

            blocks.push(BlockInfo {
                index,
                btype,
                bfinal,
                start_byte: start_bit_pos / 8,
                start_bit_offset: (start_bit_pos % 8) as u8,
                output_size: out_pos - prev_pos,
                output_offset: prev_pos,
            });

            index += 1;

            if bfinal {
                break;
            }
        }

        blocks
    }

    fn make_test_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 ABCDEFGHIJKLMNOP\n",
            b"how vexingly quick daft zebras jump. ",
        ];
        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
    }

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

    #[test]
    fn test_inflate_oracle_synthetic() {
        let data = make_test_data(4 * 1024 * 1024);
        let oracle = InflateOracle::from_raw(&data);
        oracle.summary();
        assert_eq!(oracle.expected_output, data);
        assert!(!oracle.blocks.is_empty());
    }

    #[test]
    fn test_inflate_oracle_silesia() {
        let gz = match std::fs::read("benchmark_data/silesia-gzip.tar.gz") {
            Ok(d) => d,
            Err(_) => {
                eprintln!("skipping (silesia not found)");
                return;
            }
        };
        let oracle = InflateOracle::from_gzip(&gz);
        oracle.summary();
        assert!(!oracle.blocks.is_empty());
    }

    // =========================================================================
    // Layer 1: Correctness — both paths produce identical output
    // =========================================================================

    #[test]
    fn test_consume_first_matches_libdeflate() {
        let data = make_test_data(4 * 1024 * 1024);
        let oracle = InflateOracle::from_raw(&data);

        // consume_first
        let mut cf_output = vec![0u8; oracle.expected_output.len() + 65536];
        let cf_size =
            inflate_consume_first(&oracle.deflate_data, &mut cf_output).expect("cf inflate");
        cf_output.truncate(cf_size);

        // libdeflate
        let mut ld_output = vec![0u8; oracle.expected_output.len() + 65536];
        let ld_size =
            crate::decompress::bgzf::inflate_into_pub(&oracle.deflate_data, &mut ld_output)
                .expect("ld inflate");
        ld_output.truncate(ld_size);

        assert_eq!(
            cf_size, ld_size,
            "output size mismatch: cf={} ld={}",
            cf_size, ld_size
        );
        assert_eq!(cf_output, ld_output, "output content mismatch");
        assert_eq!(cf_output, oracle.expected_output, "doesn't match expected");

        eprintln!(
            "correctness: consume_first and libdeflate produce identical {} byte output",
            cf_size
        );
    }

    #[test]
    fn test_consume_first_matches_libdeflate_silesia() {
        let gz = match std::fs::read("benchmark_data/silesia-gzip.tar.gz") {
            Ok(d) => d,
            Err(_) => {
                eprintln!("skipping (silesia not found)");
                return;
            }
        };
        let oracle = InflateOracle::from_gzip(&gz);

        let mut cf_output = vec![0u8; oracle.expected_output.len() + 65536];
        let cf_size =
            inflate_consume_first(&oracle.deflate_data, &mut cf_output).expect("cf inflate");

        let mut ld_output = vec![0u8; oracle.expected_output.len() + 65536];
        let ld_size =
            crate::decompress::bgzf::inflate_into_pub(&oracle.deflate_data, &mut ld_output)
                .expect("ld inflate");

        assert_eq!(cf_size, ld_size, "silesia output size mismatch");
        assert_eq!(
            &cf_output[..cf_size],
            &ld_output[..ld_size],
            "silesia output content mismatch"
        );
        eprintln!(
            "silesia correctness: both produce identical {} byte output",
            cf_size
        );
    }

    // =========================================================================
    // Layer 2: Performance comparison — full stream timing
    // =========================================================================

    #[test]
    fn test_inflate_perf_comparison() {
        let data = make_test_data(8 * 1024 * 1024);
        let oracle = InflateOracle::from_raw(&data);

        let trials = 10;

        // Warm up
        let mut output = vec![0u8; oracle.expected_output.len() + 65536];
        let _ = inflate_consume_first(&oracle.deflate_data, &mut output);
        let _ = crate::decompress::bgzf::inflate_into_pub(&oracle.deflate_data, &mut output);

        // Time consume_first
        let start = Instant::now();
        for _ in 0..trials {
            let _ = inflate_consume_first(&oracle.deflate_data, &mut output);
        }
        let cf_elapsed = start.elapsed();

        // Time libdeflate
        let start = Instant::now();
        for _ in 0..trials {
            let _ = crate::decompress::bgzf::inflate_into_pub(&oracle.deflate_data, &mut output);
        }
        let ld_elapsed = start.elapsed();

        let cf_mbps =
            (oracle.expected_output.len() as f64 * trials as f64) / cf_elapsed.as_secs_f64() / 1e6;
        let ld_mbps =
            (oracle.expected_output.len() as f64 * trials as f64) / ld_elapsed.as_secs_f64() / 1e6;
        let ratio = cf_mbps / ld_mbps * 100.0;

        eprintln!(
            "=== Inflate Performance (synthetic {} bytes) ===",
            oracle.expected_output.len()
        );
        eprintln!("  consume_first: {:.0} MB/s", cf_mbps);
        eprintln!("  libdeflate:    {:.0} MB/s", ld_mbps);
        eprintln!("  ratio:         {:.1}% of libdeflate", ratio);
    }

    #[test]
    fn test_inflate_perf_comparison_silesia() {
        let gz = match std::fs::read("benchmark_data/silesia-gzip.tar.gz") {
            Ok(d) => d,
            Err(_) => {
                eprintln!("skipping (silesia not found)");
                return;
            }
        };
        let oracle = InflateOracle::from_gzip(&gz);

        let trials = 5;
        let mut output = vec![0u8; oracle.expected_output.len() + 65536];

        // Warm up
        let _ = inflate_consume_first(&oracle.deflate_data, &mut output);
        let _ = crate::decompress::bgzf::inflate_into_pub(&oracle.deflate_data, &mut output);

        let start = Instant::now();
        for _ in 0..trials {
            let _ = inflate_consume_first(&oracle.deflate_data, &mut output);
        }
        let cf_elapsed = start.elapsed();

        let start = Instant::now();
        for _ in 0..trials {
            let _ = crate::decompress::bgzf::inflate_into_pub(&oracle.deflate_data, &mut output);
        }
        let ld_elapsed = start.elapsed();

        let cf_mbps =
            (oracle.expected_output.len() as f64 * trials as f64) / cf_elapsed.as_secs_f64() / 1e6;
        let ld_mbps =
            (oracle.expected_output.len() as f64 * trials as f64) / ld_elapsed.as_secs_f64() / 1e6;
        let ratio = cf_mbps / ld_mbps * 100.0;

        eprintln!(
            "=== Inflate Performance (silesia {} bytes) ===",
            oracle.expected_output.len()
        );
        eprintln!("  consume_first: {:.0} MB/s", cf_mbps);
        eprintln!("  libdeflate:    {:.0} MB/s", ld_mbps);
        eprintln!("  ratio:         {:.1}% of libdeflate", ratio);
        oracle.summary();
    }

    // =========================================================================
    // Layer 2.5: Per-block timing — isolate which blocks cause the gap
    // =========================================================================

    #[test]
    fn test_per_block_timing_silesia() {
        let gz = match std::fs::read("benchmark_data/silesia-gzip.tar.gz") {
            Ok(d) => d,
            Err(_) => {
                eprintln!("skipping (silesia not found)");
                return;
            }
        };
        let oracle = InflateOracle::from_gzip(&gz);

        // Group blocks into ~1MB regions and time each region with both decoders.
        // We can't time individual deflate blocks independently (no reset between blocks),
        // but we CAN time the full stream and correlate with block metadata.

        // Approach: decode the full stream with consume_first in per-block mode,
        // timing each block separately.
        struct BlockTiming {
            index: usize,
            btype: u8,
            output_size: usize,
            cf_ns: u64,
        }

        let mut timings = Vec::with_capacity(oracle.blocks.len());
        let mut output = vec![0u8; oracle.expected_output.len() + 65536];
        let mut bits = Bits::new(&oracle.deflate_data);
        let mut out_pos = 0usize;

        for block in &oracle.blocks {
            if bits.available() < 3 {
                bits.refill();
            }
            let header = bits.peek();
            let _bfinal = (header & 1) != 0;
            let btype = ((header >> 1) & 3) as u8;
            bits.consume(3);

            let start = Instant::now();

            match btype {
                0 => {
                    out_pos = crate::decompress::inflate::consume_first_decode::decode_stored_pub(
                        &mut bits,
                        &mut output,
                        out_pos,
                    )
                    .unwrap();
                }
                1 => {
                    out_pos = crate::decompress::inflate::consume_first_decode::decode_fixed_pub(
                        &mut bits,
                        &mut output,
                        out_pos,
                    )
                    .unwrap();
                }
                2 => {
                    out_pos = crate::decompress::inflate::consume_first_decode::decode_dynamic_pub(
                        &mut bits,
                        &mut output,
                        out_pos,
                    )
                    .unwrap();
                }
                _ => break,
            }

            let elapsed = start.elapsed();
            timings.push(BlockTiming {
                index: block.index,
                btype,
                output_size: out_pos - block.output_offset,
                cf_ns: elapsed.as_nanos() as u64,
            });
        }

        // Aggregate by block type
        let mut type_total_ns = [0u64; 3];
        let mut type_total_bytes = [0u64; 3];
        let mut type_count = [0u32; 3];
        let mut slowest_blocks: Vec<(usize, u8, f64, usize)> = Vec::new();

        for t in &timings {
            if (t.btype as usize) < 3 {
                type_total_ns[t.btype as usize] += t.cf_ns;
                type_total_bytes[t.btype as usize] += t.output_size as u64;
                type_count[t.btype as usize] += 1;
            }
            if t.output_size > 0 {
                let mbps = t.output_size as f64 / (t.cf_ns as f64 / 1e9) / 1e6;
                slowest_blocks.push((t.index, t.btype, mbps, t.output_size));
            }
        }

        eprintln!("=== Per-Block Timing (silesia, consume_first) ===");
        eprintln!(
            "{:<10} {:>6} {:>12} {:>10} {:>10}",
            "Type", "Count", "Output MB", "Time ms", "MB/s"
        );
        eprintln!("{}", "-".repeat(55));
        for t in 0..3 {
            if type_count[t] > 0 {
                let mb = type_total_bytes[t] as f64 / 1e6;
                let ms = type_total_ns[t] as f64 / 1e6;
                let mbps = type_total_bytes[t] as f64 / (type_total_ns[t] as f64 / 1e9) / 1e6;
                eprintln!(
                    "{:<10} {:>6} {:>12.2} {:>10.2} {:>10.0}",
                    InflateOracle::block_type_name(t as u8),
                    type_count[t],
                    mb,
                    ms,
                    mbps
                );
            }
        }

        // Show slowest 10 blocks
        slowest_blocks.sort_by(|a, b| a.2.partial_cmp(&b.2).unwrap());
        eprintln!("\n--- 10 Slowest Blocks ---");
        eprintln!(
            "{:<8} {:>8} {:>10} {:>10}",
            "Block#", "Type", "Size KB", "MB/s"
        );
        for (idx, btype, mbps, size) in slowest_blocks.iter().take(10) {
            eprintln!(
                "{:<8} {:>8} {:>10.1} {:>10.0}",
                idx,
                InflateOracle::block_type_name(*btype),
                *size as f64 / 1024.0,
                mbps
            );
        }

        // Show fastest 5 blocks for comparison
        eprintln!("\n--- 5 Fastest Blocks ---");
        for (idx, btype, mbps, size) in slowest_blocks.iter().rev().take(5) {
            eprintln!(
                "{:<8} {:>8} {:>10.1} {:>10.0}",
                idx,
                InflateOracle::block_type_name(*btype),
                *size as f64 / 1024.0,
                mbps
            );
        }
    }

    // =========================================================================
    // Layer 3: Pattern analysis — which block types cause the gap
    // =========================================================================

    #[test]
    fn test_inflate_pattern_analysis() {
        type PatternFn = fn() -> Vec<u8>;
        let patterns: &[(&str, PatternFn)] = &[
            ("literals", || {
                // Mostly unique bytes — tests literal decoding
                (0..4_000_000u32)
                    .map(|i| (i.wrapping_mul(2654435761) >> 24) as u8)
                    .collect()
            }),
            ("rle", || {
                // Long runs of the same byte — tests distance=1 copy
                let mut data = Vec::with_capacity(4_000_000);
                for _ in 0..100 {
                    let byte = (data.len() % 256) as u8;
                    data.extend(std::iter::repeat_n(byte, 40_000));
                }
                data
            }),
            ("short_matches", || {
                // Repeating short patterns — tests small distance copies
                let pattern = b"abcdefgh12345678";
                let mut data = Vec::with_capacity(4_000_000);
                while data.len() < 4_000_000 {
                    data.extend_from_slice(pattern);
                }
                data.truncate(4_000_000);
                data
            }),
            ("mixed", || {
                // Mix of literals and matches (realistic data)
                make_test_data(4_000_000)
            }),
        ];

        eprintln!("=== Inflate Pattern Analysis ===");
        eprintln!(
            "{:<15} {:>10} {:>10} {:>8}",
            "Pattern", "CF MB/s", "LD MB/s", "Ratio"
        );
        eprintln!("{}", "-".repeat(48));

        for (name, gen) in patterns {
            let data = gen();
            let gz = compress_gzip(&data);
            let header_size = crate::decompress::parallel::marker_decode::skip_gzip_header(&gz)
                .expect("valid header");
            let deflate = &gz[header_size..gz.len() - 8];

            let mut output = vec![0u8; data.len() + 65536];
            let trials = 10;

            // Warm up
            let _ = inflate_consume_first(deflate, &mut output);
            let _ = crate::decompress::bgzf::inflate_into_pub(deflate, &mut output);

            let start = Instant::now();
            for _ in 0..trials {
                let _ = inflate_consume_first(deflate, &mut output);
            }
            let cf_elapsed = start.elapsed();

            let start = Instant::now();
            for _ in 0..trials {
                let _ = crate::decompress::bgzf::inflate_into_pub(deflate, &mut output);
            }
            let ld_elapsed = start.elapsed();

            let cf_mbps = (data.len() as f64 * trials as f64) / cf_elapsed.as_secs_f64() / 1e6;
            let ld_mbps = (data.len() as f64 * trials as f64) / ld_elapsed.as_secs_f64() / 1e6;
            let ratio = cf_mbps / ld_mbps * 100.0;

            eprintln!(
                "{:<15} {:>10.0} {:>10.0} {:>7.1}%",
                name, cf_mbps, ld_mbps, ratio
            );
        }
    }
}