| Crates.io | minlz |
| lib.rs | minlz |
| version | 0.1.3 |
| created_at | 2025-11-13 11:13:11.371202+00 |
| updated_at | 2025-11-14 12:19:02.925644+00 |
| description | S2 compression format - compatible with klauspost/compress/s2 |
| homepage | |
| repository | https://github.com/KarpelesLab/minlz-rs |
| max_upload_size | |
| id | 1930880 |
| size | 1,387,091 |
A high-performance Rust implementation of the S2 compression format, providing binary compatibility with the Go implementation at github.com/klauspost/compress/s2.
s2c and s2d tools compatible with Go implementationS2 is an extension of the Snappy compression format that provides:
Note: S2-compressed data cannot be decompressed by Snappy decoders.
More Information: S2 Design & Improvements - Overview of S2's design and improvements
Add this to your Cargo.toml:
[dependencies]
minlz = "0.1"
Enable concurrent compression for improved performance on multi-core systems:
[dependencies]
minlz = { version = "0.1", features = ["concurrent"] }
use minlz::{encode, decode};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let data = b"Hello, World! This is a test.";
// Compress
let compressed = encode(data);
println!("Compressed {} bytes to {} bytes", data.len(), compressed.len());
// Decompress
let decompressed = decode(&compressed)?;
assert_eq!(data, &decompressed[..]);
Ok(())
}
use minlz::{Writer, Reader};
use std::io::{Write, Read};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let data = b"Streaming compression with CRC validation!";
// Compress using stream format
let mut compressed = Vec::new();
{
let mut writer = Writer::new(&mut compressed);
writer.write_all(data)?;
writer.flush()?;
}
// Decompress using stream format
let mut reader = Reader::new(&compressed[..]);
let mut decompressed = Vec::new();
reader.read_to_end(&mut decompressed)?;
assert_eq!(data, &decompressed[..]);
Ok(())
}
use minlz::{encode, encode_better, encode_best};
let data = b"Some data to compress...";
// Fast compression (default)
let compressed = encode(data);
// Better compression (slower)
let compressed_better = encode_better(data);
// Best compression (slowest)
let compressed_best = encode_best(data);
Enable the concurrent feature for parallel compression on multi-core systems:
use minlz::ConcurrentWriter;
use std::io::Write;
let mut compressed = Vec::new();
{
// Compress with 4 concurrent workers
let mut writer = ConcurrentWriter::new(&mut compressed, 4);
writer.write_all(&large_data)?;
writer.flush()?;
}
Dictionaries can improve compression of similar data by pre-seeding the compressor with common patterns:
use minlz::{make_dict, encode_with_dict, decode_with_dict};
// Create a dictionary from sample data
let samples = b"Common patterns that appear frequently in your data...";
let dict = make_dict(samples, Some(b"Common")).unwrap();
// Encode with dictionary
let data = b"Data to compress...";
let compressed = encode_with_dict(data, &dict);
// Decode with dictionary
let decompressed = decode_with_dict(&compressed, &dict)?;
assert_eq!(data, &decompressed[..]);
// Serialize dictionary for storage/transmission
let dict_bytes = dict.to_bytes();
The minlz-tools package provides s2c (compression) and s2d (decompression) command-line tools that are fully compatible with the Go s2 tools.
# Install from source
cargo install --path minlz-tools
# Compress a file
s2c input.txt # Creates input.txt.s2
s2c --slower input.txt # Best compression
s2c --faster input.txt # Fast compression
# Decompress a file
s2d input.txt.s2 # Creates input.txt
s2d --verify input.txt.s2 # Verify integrity
The tools are cross-compatible with Go's s2c/s2d and offer 12-98x faster performance depending on the operation.
See minlz-tools/README.md for complete documentation.
This Rust implementation delivers exceptional performance, often exceeding the Go reference implementation.
| Mode | Data Size | Pattern | Rust | Go | Speedup |
|---|---|---|---|---|---|
| Standard | 10KB | Random | 2.1 GiB/s | 1280 MB/s | 1.7x |
| Standard | 100KB | Text | 2.2 GiB/s | 1545 MB/s | 1.5x |
| Better | 10KB | Text | 937 MiB/s | 2232 MB/s | 0.4x |
| Best | 10KB | Repeated | 85.1 MiB/s | 7 MB/s | 12x |
| Best | 10KB | Text | 111 MiB/s | 7 MB/s | 16x |
| Data Size | Pattern | Rust | Go | Speedup |
|---|---|---|---|---|
| 1KB | Random | 17.7 GiB/s | 672 MB/s | 28x |
| 10KB | Random | 50.7 GiB/s | 538 MB/s | 98x |
| 10KB | Text | 8.3 GiB/s | 509 MB/s | 17x |
| 100KB | Random | 37.2 GiB/s | 654 MB/s | 59x |
| 100KB | Repeated | 1.05 GiB/s | 685 MB/s | 1.6x |
Key Takeaways:
See BENCHMARKS.md for detailed performance analysis.
This implementation is binary compatible with the Go version. You can compress data with this Rust library and decompress it with the Go library, and vice versa.
Rust side:
use minlz::encode;
use std::fs::File;
use std::io::Write;
let data = b"Hello from Rust!";
let compressed = encode(data);
let mut file = File::create("data.s2")?;
file.write_all(&compressed)?;
Go side:
package main
import (
"os"
"github.com/klauspost/compress/s2"
)
func main() {
compressed, _ := os.ReadFile("data.s2")
decompressed, _ := s2.Decode(nil, compressed)
println(string(decompressed)) // "Hello from Rust!"
}
Run the included examples:
# Basic compression example
cargo run --example basic
# Debug/testing example
cargo run --example debug
This library implements both formats:
Suitable for:
Includes:
Use stream format for file I/O, network streaming, or when you need data integrity validation.
This implementation includes comprehensive testing infrastructure:
# Unit and integration tests (48 tests)
cargo test
# Property-based tests (proptest)
cargo test --test proptest
# Benchmarks
cargo bench
# Fuzz testing
cargo install cargo-fuzz
cargo fuzz run fuzz_roundtrip
cargo fuzz run fuzz_decode
cargo fuzz run fuzz_stream
Total: 108 tests
BSD-3-Clause
Contributions are welcome! Please ensure:
cargo test)cargo fmt)cargo clippy)The current implementation passes all 108 tests, is formatted with rustfmt, and has zero clippy warnings.