Pure Go implementation of LZO1Z compression and decompression.
LZO1Z is a variant of the LZO1X compression algorithm used in real-time data feeds and other applications requiring fast compression/decompression.
This package provides both compression and decompression, fully compatible with the liblzo2 library.
- Pure Go - no CGO, no external dependencies
- Zero allocations per call
- ~420 MB/s compression, ~1 GB/s decompression
- Compatible with liblzo2
- Cross-compilation friendly
go get github.com/rhnvrm/lzo1zinput := []byte("Hello, World! Hello, World! Hello, World!")
// Allocate buffer for compressed data
compressed := make([]byte, lzo1z.MaxCompressedSize(len(input)))
// Compress
n, err := lzo1z.Compress(input, compressed)
if err != nil {
log.Fatal(err)
}
compressed = compressed[:n]
fmt.Printf("%d bytes -> %d bytes\n", len(input), n)
// Output: 41 bytes -> 21 bytes// Allocate buffer (must know or estimate decompressed size)
output := make([]byte, expectedSize)
// Decompress
n, err := lzo1z.Decompress(compressed, output)
if err != nil {
log.Fatal(err)
}
result := output[:n]Use MaxCompressedSize to allocate compression buffers:
bufSize := lzo1z.MaxCompressedSize(inputLen)
// Returns: inputLen + inputLen/16 + 64 + 3For decompression, you must know or estimate the output size. LZO does not store the decompressed size in the stream.
Benchmarks on Intel i7-1355U:
BenchmarkCompress-12 114550 10700 ns/op 420 MB/s 0 B/op 0 allocs/op
BenchmarkDecompress-12 13102768 94 ns/op 1060 MB/s 0 B/op 0 allocs/op
| Input | Size | Compressed | Ratio |
|---|---|---|---|
| Repeated "A" | 40 B | 9 B | 4.4x |
| "ABCD" x 100 | 400 B | 16 B | 25x |
| English text | 17 KB | 322 B | 53x |
| Random bytes | 256 B | 261 B | 0.98x |
LZO1Z differs from LZO1X in offset encoding:
| Aspect | LZO1Z | LZO1X |
|---|---|---|
| Offset encoding | (b0 << 6) + (b1 >> 2) |
(b0 >> 2) + (b1 << 6) |
| M2 offset reuse | Yes | No |
| M2_MAX_OFFSET | 1792 | 2048 |
These differences mean LZO1X and LZO1Z are not compatible.
- Buffer sizing - caller must provide appropriately sized buffers
- No streaming - data must fit in memory
go test -v ./... # Run tests
go test -bench=. -benchmem # Run benchmarksTest vectors are verified against liblzo2 for both compression and decompression.
Based on the LZO algorithm by Markus Franz Xaver Johannes Oberhumer.
MIT License - see LICENSE.