foxstash-core 0.5.0

High-performance local RAG library - SIMD-accelerated vector search, HNSW indexing
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
//! Compression codecs for efficient storage
//!
//! This module provides multiple compression algorithms with automatic fallback
//! and detailed statistics. Compressed data includes a 4-byte header for codec
//! detection and version information.
//!
//! # Supported Codecs
//!
//! - **LZ4**: Fast compression with good ratios (feature: `lz4`)
//! - **Zstd**: Better compression ratios, slightly slower (feature: `zstd`)
//! - **Gzip**: Always available fallback via flate2
//! - **None**: No compression (passthrough)
//!
//! # Examples
//!
//! ```
//! use foxstash_core::storage::compression::{compress, decompress, best_codec};
//!
//! let data = b"Hello, World! This is some test data to compress.".repeat(100);
//!
//! // Compress with best available codec
//! let (compressed, stats) = compress(&data).unwrap();
//! println!("Compressed {} bytes to {} bytes ({:.2}x ratio) using {:?}",
//!          stats.original_size, stats.compressed_size, stats.ratio, stats.codec);
//!
//! // Decompress (codec detected automatically from header)
//! let decompressed = decompress(&compressed).unwrap();
//! assert_eq!(data.as_slice(), decompressed.as_slice());
//! ```

use serde::{Deserialize, Serialize};
use std::io::{self, Write};
use std::time::Instant;

/// Compression codec identifier
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Codec {
    /// No compression (passthrough)
    None,
    /// Gzip compression (always available)
    Gzip,
    /// LZ4 compression (requires `lz4` feature)
    #[cfg(feature = "lz4")]
    Lz4,
    /// Zstd compression (requires `zstd` feature)
    #[cfg(feature = "zstd")]
    Zstd,
}

impl Codec {
    /// Get the codec ID for the header
    fn id(&self) -> u8 {
        match self {
            Codec::None => 0,
            Codec::Gzip => 1,
            #[cfg(feature = "lz4")]
            Codec::Lz4 => 2,
            #[cfg(feature = "zstd")]
            Codec::Zstd => 3,
        }
    }

    /// Parse codec from ID
    fn from_id(id: u8) -> Result<Self, CompressionError> {
        match id {
            0 => Ok(Codec::None),
            1 => Ok(Codec::Gzip),
            #[cfg(feature = "lz4")]
            2 => Ok(Codec::Lz4),
            #[cfg(not(feature = "lz4"))]
            2 => Err(CompressionError::UnsupportedCodec(
                "LZ4 feature not enabled".to_string(),
            )),
            #[cfg(feature = "zstd")]
            3 => Ok(Codec::Zstd),
            #[cfg(not(feature = "zstd"))]
            3 => Err(CompressionError::UnsupportedCodec(
                "Zstd feature not enabled".to_string(),
            )),
            _ => Err(CompressionError::InvalidHeader(format!(
                "Unknown codec ID: {}",
                id
            ))),
        }
    }

    /// Get human-readable name
    pub fn name(&self) -> &'static str {
        match self {
            Codec::None => "None",
            Codec::Gzip => "Gzip",
            #[cfg(feature = "lz4")]
            Codec::Lz4 => "LZ4",
            #[cfg(feature = "zstd")]
            Codec::Zstd => "Zstd",
        }
    }
}

/// Statistics about a compression operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompressionStats {
    /// Original uncompressed size in bytes
    pub original_size: usize,
    /// Compressed size in bytes (including header)
    pub compressed_size: usize,
    /// Compression ratio (original / compressed)
    pub ratio: f64,
    /// Codec used for compression
    pub codec: Codec,
    /// Compression duration in milliseconds
    pub duration_ms: f64,
}

impl CompressionStats {
    /// Calculate space saved in bytes
    pub fn space_saved(&self) -> i64 {
        self.original_size as i64 - self.compressed_size as i64
    }

    /// Calculate space saved as percentage
    pub fn space_saved_percent(&self) -> f64 {
        if self.original_size == 0 {
            0.0
        } else {
            (self.space_saved() as f64 / self.original_size as f64) * 100.0
        }
    }

    /// Compression throughput in MB/s
    pub fn throughput_mbps(&self) -> f64 {
        if self.duration_ms == 0.0 {
            0.0
        } else {
            (self.original_size as f64 / 1_000_000.0) / (self.duration_ms / 1000.0)
        }
    }
}

/// Error types for compression operations
#[derive(Debug, thiserror::Error)]
pub enum CompressionError {
    #[error("IO error: {0}")]
    Io(#[from] io::Error),

    #[error("Invalid header: {0}")]
    InvalidHeader(String),

    #[error("Unsupported codec: {0}")]
    UnsupportedCodec(String),

    #[error("Compression failed: {0}")]
    CompressionFailed(String),

    #[error("Decompression failed: {0}")]
    DecompressionFailed(String),
}

/// Magic header: [codec_id, version, reserved, reserved]
const HEADER_SIZE: usize = 4;
const VERSION: u8 = 1;

/// Create compression header
fn create_header(codec: Codec) -> [u8; HEADER_SIZE] {
    [codec.id(), VERSION, 0, 0]
}

/// Parse compression header
fn parse_header(data: &[u8]) -> Result<(Codec, usize), CompressionError> {
    if data.len() < HEADER_SIZE {
        return Err(CompressionError::InvalidHeader(format!(
            "Data too small: {} bytes",
            data.len()
        )));
    }

    let codec = Codec::from_id(data[0])?;
    let version = data[1];

    if version != VERSION {
        return Err(CompressionError::InvalidHeader(format!(
            "Unsupported version: {}",
            version
        )));
    }

    Ok((codec, HEADER_SIZE))
}

/// Get the best available codec based on enabled features
///
/// Priority order: LZ4 > Zstd > Gzip
///
/// # Examples
///
/// ```
/// use foxstash_core::storage::compression::best_codec;
///
/// let codec = best_codec();
/// println!("Best available codec: {:?}", codec);
/// ```
pub fn best_codec() -> Codec {
    #[cfg(feature = "lz4")]
    {
        Codec::Lz4
    }

    #[cfg(all(feature = "zstd", not(feature = "lz4")))]
    {
        return Codec::Zstd;
    }

    #[cfg(not(any(feature = "lz4", feature = "zstd")))]
    {
        Codec::Gzip
    }
}

/// Compress data using the best available codec
///
/// This function automatically selects the best compression codec based on
/// enabled features and returns both the compressed data and statistics.
///
/// # Examples
///
/// ```
/// use foxstash_core::storage::compression::compress;
///
/// let data = b"Hello, World!".repeat(100);
/// let (compressed, stats) = compress(&data).unwrap();
/// assert!(stats.compressed_size < stats.original_size);
/// ```
pub fn compress(data: &[u8]) -> Result<(Vec<u8>, CompressionStats), CompressionError> {
    compress_with(data, best_codec())
}

/// Compress data using a specific codec
///
/// # Examples
///
/// ```
/// use foxstash_core::storage::compression::{compress_with, Codec};
///
/// let data = b"Hello, World!".repeat(100);
/// let (compressed, stats) = compress_with(&data, Codec::Gzip).unwrap();
/// assert_eq!(stats.codec, Codec::Gzip);
/// ```
pub fn compress_with(
    data: &[u8],
    codec: Codec,
) -> Result<(Vec<u8>, CompressionStats), CompressionError> {
    let start = Instant::now();
    let original_size = data.len();

    let header = create_header(codec);
    let mut compressed = Vec::with_capacity(HEADER_SIZE + data.len());
    compressed.extend_from_slice(&header);

    match codec {
        Codec::None => {
            compressed.extend_from_slice(data);
        }
        Codec::Gzip => {
            compress_gzip(data, &mut compressed)?;
        }
        #[cfg(feature = "lz4")]
        Codec::Lz4 => {
            compress_lz4(data, &mut compressed)?;
        }
        #[cfg(feature = "zstd")]
        Codec::Zstd => {
            compress_zstd(data, &mut compressed)?;
        }
    }

    let duration_ms = start.elapsed().as_secs_f64() * 1000.0;
    let compressed_size = compressed.len();
    let ratio = if compressed_size > 0 {
        original_size as f64 / compressed_size as f64
    } else {
        0.0
    };

    let stats = CompressionStats {
        original_size,
        compressed_size,
        ratio,
        codec,
        duration_ms,
    };

    Ok((compressed, stats))
}

/// Decompress data (codec detected automatically from header)
///
/// # Examples
///
/// ```
/// use foxstash_core::storage::compression::{compress, decompress};
///
/// let original = b"Hello, World!".repeat(100);
/// let (compressed, _) = compress(&original).unwrap();
/// let decompressed = decompress(&compressed).unwrap();
/// assert_eq!(original.as_slice(), decompressed.as_slice());
/// ```
pub fn decompress(data: &[u8]) -> Result<Vec<u8>, CompressionError> {
    let (codec, offset) = parse_header(data)?;
    let compressed_data = &data[offset..];

    match codec {
        Codec::None => Ok(compressed_data.to_vec()),
        Codec::Gzip => decompress_gzip(compressed_data),
        #[cfg(feature = "lz4")]
        Codec::Lz4 => decompress_lz4(compressed_data),
        #[cfg(feature = "zstd")]
        Codec::Zstd => decompress_zstd(compressed_data),
    }
}

// ============================================================================
// Codec Implementations
// ============================================================================

/// Compress using Gzip (always available)
fn compress_gzip(data: &[u8], output: &mut Vec<u8>) -> Result<(), CompressionError> {
    use flate2::write::GzEncoder;
    use flate2::Compression;

    let mut encoder = GzEncoder::new(output, Compression::default());
    encoder.write_all(data)?;
    encoder.finish()?;
    Ok(())
}

/// Decompress using Gzip
fn decompress_gzip(data: &[u8]) -> Result<Vec<u8>, CompressionError> {
    use flate2::read::GzDecoder;
    use std::io::Read;

    let mut decoder = GzDecoder::new(data);
    let mut result = Vec::new();
    decoder
        .read_to_end(&mut result)
        .map_err(|e| CompressionError::DecompressionFailed(e.to_string()))?;
    Ok(result)
}

/// Compress using LZ4
///
/// Stores the original size as a 4-byte little-endian integer before the compressed data
/// to enable proper decompression.
#[cfg(feature = "lz4")]
fn compress_lz4(data: &[u8], output: &mut Vec<u8>) -> Result<(), CompressionError> {
    // Store the original size (4 bytes) so we can decompress correctly
    let original_size = data.len() as u32;
    output.extend_from_slice(&original_size.to_le_bytes());

    let compressed = lz4::block::compress(
        data,
        Some(lz4::block::CompressionMode::HIGHCOMPRESSION(9)),
        false,
    )
    .map_err(|e| CompressionError::CompressionFailed(e.to_string()))?;
    output.extend_from_slice(&compressed);
    Ok(())
}

/// Decompress using LZ4
///
/// Reads the original size from the first 4 bytes, then decompresses the data.
#[cfg(feature = "lz4")]
fn decompress_lz4(data: &[u8]) -> Result<Vec<u8>, CompressionError> {
    if data.len() < 4 {
        return Err(CompressionError::DecompressionFailed(
            "LZ4 data too small: missing size header".to_string(),
        ));
    }

    // Read original size from first 4 bytes
    let size_bytes = [data[0], data[1], data[2], data[3]];
    let original_size = u32::from_le_bytes(size_bytes) as usize;

    // Decompress the remaining data
    let compressed_data = &data[4..];
    lz4::block::decompress(compressed_data, Some(original_size as i32))
        .map_err(|e| CompressionError::DecompressionFailed(e.to_string()))
}

/// Compress using Zstd
#[cfg(feature = "zstd")]
fn compress_zstd(data: &[u8], output: &mut Vec<u8>) -> Result<(), CompressionError> {
    let compressed = zstd::encode_all(data, 3)
        .map_err(|e| CompressionError::CompressionFailed(e.to_string()))?;
    output.extend_from_slice(&compressed);
    Ok(())
}

/// Decompress using Zstd
#[cfg(feature = "zstd")]
fn decompress_zstd(data: &[u8]) -> Result<Vec<u8>, CompressionError> {
    zstd::decode_all(data).map_err(|e| CompressionError::DecompressionFailed(e.to_string()))
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    /// Generate test data with varying characteristics
    fn generate_test_data(size: usize, compressibility: Compressibility) -> Vec<u8> {
        match compressibility {
            Compressibility::Random => {
                // Random data (incompressible)
                (0..size).map(|i| (i * 7 + 13) as u8).collect()
            }
            Compressibility::Repeated => {
                // Highly compressible repeated pattern
                b"Hello, World! ".repeat(size / 14 + 1)[..size].to_vec()
            }
            Compressibility::Structured => {
                // JSON-like structured data
                let json =
                    r#"{"id": "doc-001", "content": "This is a test document", "score": 0.95}"#;
                json.repeat(size / json.len() + 1).as_bytes()[..size].to_vec()
            }
        }
    }

    enum Compressibility {
        Random,
        Repeated,
        Structured,
    }

    #[test]
    fn test_codec_id_roundtrip() {
        // Test all codec IDs can be converted back and forth
        for &id in &[0u8, 1u8] {
            let codec = Codec::from_id(id).unwrap();
            assert_eq!(codec.id(), id);
        }

        #[cfg(feature = "lz4")]
        {
            let codec = Codec::from_id(2).unwrap();
            assert_eq!(codec.id(), 2);
        }

        #[cfg(feature = "zstd")]
        {
            let codec = Codec::from_id(3).unwrap();
            assert_eq!(codec.id(), 3);
        }
    }

    #[test]
    fn test_invalid_codec_id() {
        let result = Codec::from_id(99);
        assert!(result.is_err());
    }

    #[test]
    fn test_best_codec_available() {
        let codec = best_codec();
        // Should always return a valid codec
        assert!(!codec.name().is_empty());

        #[cfg(feature = "lz4")]
        assert_eq!(codec, Codec::Lz4);

        #[cfg(all(feature = "zstd", not(feature = "lz4")))]
        assert_eq!(codec, Codec::Zstd);

        #[cfg(not(any(feature = "lz4", feature = "zstd")))]
        assert_eq!(codec, Codec::Gzip);
    }

    #[test]
    fn test_compression_none() {
        let data = b"Hello, World!";
        let (compressed, stats) = compress_with(data, Codec::None).unwrap();

        assert_eq!(stats.codec, Codec::None);
        assert_eq!(stats.original_size, data.len());
        assert_eq!(stats.compressed_size, data.len() + HEADER_SIZE);

        let decompressed = decompress(&compressed).unwrap();
        assert_eq!(data.as_slice(), decompressed.as_slice());
    }

    #[test]
    fn test_compression_gzip() {
        let data = generate_test_data(1000, Compressibility::Repeated);
        let (compressed, stats) = compress_with(&data, Codec::Gzip).unwrap();

        assert_eq!(stats.codec, Codec::Gzip);
        assert_eq!(stats.original_size, data.len());
        assert!(
            stats.compressed_size < data.len(),
            "Gzip should compress repeated data"
        );
        assert!(stats.ratio > 1.0);
        assert!(stats.duration_ms >= 0.0);

        let decompressed = decompress(&compressed).unwrap();
        assert_eq!(data, decompressed);
    }

    #[cfg(feature = "lz4")]
    #[test]
    fn test_compression_lz4() {
        let data = generate_test_data(1000, Compressibility::Repeated);
        let (compressed, stats) = compress_with(&data, Codec::Lz4).unwrap();

        assert_eq!(stats.codec, Codec::Lz4);
        assert_eq!(stats.original_size, data.len());
        assert!(
            stats.compressed_size < data.len(),
            "LZ4 should compress repeated data"
        );
        assert!(stats.ratio > 1.0);

        let decompressed = decompress(&compressed).unwrap();
        assert_eq!(data, decompressed);
    }

    #[cfg(feature = "zstd")]
    #[test]
    fn test_compression_zstd() {
        let data = generate_test_data(1000, Compressibility::Repeated);
        let (compressed, stats) = compress_with(&data, Codec::Zstd).unwrap();

        assert_eq!(stats.codec, Codec::Zstd);
        assert_eq!(stats.original_size, data.len());
        assert!(
            stats.compressed_size < data.len(),
            "Zstd should compress repeated data"
        );
        assert!(stats.ratio > 1.0);

        let decompressed = decompress(&compressed).unwrap();
        assert_eq!(data, decompressed);
    }

    #[test]
    fn test_roundtrip_best_codec() {
        let data = generate_test_data(5000, Compressibility::Structured);
        let (compressed, stats) = compress(&data).unwrap();

        println!("Best codec: {:?}, ratio: {:.2}x", stats.codec, stats.ratio);

        let decompressed = decompress(&compressed).unwrap();
        assert_eq!(data, decompressed);
    }

    #[test]
    fn test_header_format() {
        let data = b"test data";
        let (compressed, _) = compress_with(data, Codec::Gzip).unwrap();

        // Check header is present and valid
        assert!(compressed.len() >= HEADER_SIZE);
        assert_eq!(compressed[0], Codec::Gzip.id());
        assert_eq!(compressed[1], VERSION);
    }

    #[test]
    fn test_invalid_header_too_small() {
        let data = &[1, 2]; // Too small
        let result = decompress(data);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            CompressionError::InvalidHeader(_)
        ));
    }

    #[test]
    fn test_invalid_header_wrong_version() {
        let mut data = vec![1, 99, 0, 0]; // Wrong version
        data.extend_from_slice(b"some data");
        let result = decompress(&data);
        assert!(result.is_err());
    }

    #[test]
    fn test_compression_stats() {
        let data = generate_test_data(10000, Compressibility::Repeated);
        let (_, stats) = compress(&data).unwrap();

        assert_eq!(stats.original_size, 10000);
        assert!(stats.compressed_size > 0);
        assert!(stats.ratio > 0.0);
        assert!(stats.duration_ms >= 0.0);

        // Test derived stats
        let saved = stats.space_saved();
        let saved_percent = stats.space_saved_percent();
        let throughput = stats.throughput_mbps();

        assert!(saved > 0, "Should save space on repeated data");
        assert!(saved_percent > 0.0 && saved_percent < 100.0);
        assert!(throughput > 0.0);
    }

    #[test]
    fn test_empty_data() {
        let data = b"";
        let (compressed, stats) = compress(data).unwrap();
        assert_eq!(stats.original_size, 0);

        let decompressed = decompress(&compressed).unwrap();
        assert_eq!(data.as_slice(), decompressed.as_slice());
    }

    #[test]
    fn test_large_data() {
        // Test with 1MB of data
        let data = generate_test_data(1_000_000, Compressibility::Structured);
        let (compressed, stats) = compress(&data).unwrap();

        println!(
            "Large data compression: {:.2} MB -> {:.2} MB ({:.2}x, {:.2} MB/s)",
            stats.original_size as f64 / 1_000_000.0,
            stats.compressed_size as f64 / 1_000_000.0,
            stats.ratio,
            stats.throughput_mbps()
        );

        assert!(stats.ratio > 1.0, "Should compress structured data");

        let decompressed = decompress(&compressed).unwrap();
        assert_eq!(data.len(), decompressed.len());
        assert_eq!(data, decompressed);
    }

    #[test]
    fn test_random_data_incompressible() {
        let data = generate_test_data(1000, Compressibility::Random);
        let (compressed, stats) = compress(&data).unwrap();

        // Random data should not compress well
        // Note: Our pseudo-random data is still somewhat compressible
        // True random data would have ratio < 1.0 (expansion)
        println!("Random data ratio: {:.2}x", stats.ratio);
        assert!(
            stats.ratio < 10.0,
            "Random data should not compress as well as structured data"
        );

        // Verify roundtrip works
        let decompressed = decompress(&compressed).unwrap();
        assert_eq!(data, decompressed);
    }

    #[test]
    fn test_embedding_vectors() {
        // Simulate embedding vectors (f32 arrays)
        let embeddings: Vec<f32> = (0..384).map(|i| (i as f32) * 0.001).collect();
        let data: Vec<u8> = embeddings.iter().flat_map(|f| f.to_le_bytes()).collect();

        let (compressed, stats) = compress(&data).unwrap();

        println!(
            "Embedding compression: {} -> {} bytes ({:.2}x)",
            stats.original_size, stats.compressed_size, stats.ratio
        );

        let decompressed = decompress(&compressed).unwrap();
        assert_eq!(data, decompressed);

        // Verify we can reconstruct embeddings
        let reconstructed: Vec<f32> = decompressed
            .chunks_exact(4)
            .map(|chunk| f32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]))
            .collect();
        assert_eq!(embeddings, reconstructed);
    }

    #[test]
    fn test_codec_names() {
        assert_eq!(Codec::None.name(), "None");
        assert_eq!(Codec::Gzip.name(), "Gzip");

        #[cfg(feature = "lz4")]
        assert_eq!(Codec::Lz4.name(), "LZ4");

        #[cfg(feature = "zstd")]
        assert_eq!(Codec::Zstd.name(), "Zstd");
    }

    // ========================================================================
    // Benchmarks (run with --release for meaningful results)
    // ========================================================================

    #[test]
    #[ignore] // Run with: cargo test --release -- --ignored bench_
    fn bench_compression_comparison() {
        let test_cases = vec![
            ("Small Random", 1_000, Compressibility::Random),
            ("Small Repeated", 1_000, Compressibility::Repeated),
            ("Small Structured", 1_000, Compressibility::Structured),
            ("Large Random", 100_000, Compressibility::Random),
            ("Large Repeated", 100_000, Compressibility::Repeated),
            ("Large Structured", 100_000, Compressibility::Structured),
        ];

        println!(
            "\n{:<20} {:<10} {:<12} {:<12} {:<10} {:<12}",
            "Test Case", "Codec", "Original", "Compressed", "Ratio", "Speed (MB/s)"
        );
        println!("{}", "=".repeat(85));

        for (name, size, comp_type) in test_cases {
            let data = generate_test_data(size, comp_type);

            // Test all available codecs
            let codecs = vec![
                Codec::None,
                Codec::Gzip,
                #[cfg(feature = "lz4")]
                Codec::Lz4,
                #[cfg(feature = "zstd")]
                Codec::Zstd,
            ];

            for codec in codecs {
                let (_, stats) = compress_with(&data, codec).unwrap();
                println!(
                    "{:<20} {:<10} {:<12} {:<12} {:<10.2} {:<12.2}",
                    name,
                    codec.name(),
                    stats.original_size,
                    stats.compressed_size,
                    stats.ratio,
                    stats.throughput_mbps()
                );
            }
        }
    }

    #[test]
    #[ignore]
    fn bench_decompression_speed() {
        let data = generate_test_data(1_000_000, Compressibility::Structured);
        let (compressed, comp_stats) = compress(&data).unwrap();

        println!("\nDecompression benchmark:");
        println!(
            "Codec: {:?}, Compressed size: {} bytes",
            comp_stats.codec,
            compressed.len()
        );

        let iterations = 10;
        let start = Instant::now();

        for _ in 0..iterations {
            let _ = decompress(&compressed).unwrap();
        }

        let elapsed = start.elapsed().as_secs_f64();
        let throughput = (data.len() * iterations) as f64 / 1_000_000.0 / elapsed;

        println!("Decompression throughput: {:.2} MB/s", throughput);
    }
}