lzav

avaneevMIT活跃维护

单头文件的 C/C++ 内存压缩算法,压缩速度 540MB/s、解压 3000MB/s,压缩率优于 LZ4、Snappy 和 Zstd 最低档,适合对吞吐和体积都敏感的场景。

Fast In-Memory Data Compression Algorithm (header-only C/C++) 540+MB/s compress, 3000+MB/s decompress, ratio% better than LZ4, Snappy, and Zstd@--1

暂无版本564star +4 / 7 天26fork同步于 2026-11-10 11:58 UTC

怎么上手

由 README 提炼,执行前请确认
#include "lzav.h"
int max_len = lzav_compress_bound( src_len );
int comp_len = lzav_compress_default( src_buf, comp_buf, src_len, max_len );
硬件要求按 README 说明

README 未说明显存或 GPU 要求;为纯 CPU 算法,在 32 位和 64 位平台均可运行,基准数据来自 2019 年后 4GHz 以上的 64 位处理器

支持 Apple Silicon
下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

纯头文件的 C/C++ 内存压缩算法,压缩率优于 LZ4,解压速度极快

单头文件的 C/C++ 内存压缩算法,压缩速度 540MB/s、解压 3000MB/s,压缩率优于 LZ4、Snappy 和 Zstd 最低档,适合对吞吐和体积都敏感的场景。

核心能力

  • 单头文件 lzav.h,标量实现,跨平台可移植
  • 压缩率比 LZ4 高约 17%,enwik9 上高 24%
  • 解压内置越界检查,可安全处理损坏数据
  • 提供 lzav_compress_hi 高压缩率模式
  • 支持大端、小端、任意内存对齐及 WebAssembly

容易踩的坑

解压时 dstlen 参数必须传入原始未压缩长度,需自行额外保存;单块数据上限为 2 GiB;API 与 LZ4、Snappy 不兼容,不能直接替换

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

0 个版本

暂无 release 数据(可能没有发版,或尚未同步)。

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    LZAV - Fast Data Compression Algorithm (in C/C++)

    Introduction

    LZAV is a fast, general-purpose, in-memory data compression algorithm based on the classic LZ77 lossless compression method. LZAV holds a strong position on the Pareto frontier among many similar in-memory (non-streaming) compression algorithms.

    LZAV's code is portable, cross-platform, scalar, and header-only. It is also compatible with C++. It supports big- and little-endian platforms and any memory alignment model. The algorithm is efficient on both 32- and 64-bit platforms. It is compatible with WebAssembly and, when compiled against WASI libc, achieves roughly half the speed of native code. The algorithm does not measurably increase the size of incompressible data.

    LZAV performs internal out-of-bounds (OOB) checks and does not trade safety for decompression speed. This means that LZAV can be used under strict conditions where OOB memory reads and writes, which could lead to CPU traps, are unacceptable (e.g., in real-time, system-level, and server software). LZAV can be used safely (without crashes or undefined behavior) even when decompressing malformed or damaged data. Consequently, it does not require calculating a checksum (or a hash) of the compressed data. Only a checksum of the uncompressed data may be required, depending on the application's needs.

    The internal functions in lzav.h allow developers to implement and test their own compression algorithms. LZAV's stream format allows for high compression ratios and high decompression speeds.

    Usage

    To compress data:

    #include "lzav.h"
    
    int max_len = lzav_compress_bound( src_len );
    void* comp_buf = malloc( max_len );
    int comp_len = lzav_compress_default( src_buf, comp_buf, src_len, max_len );
    
    if( comp_len == 0 && src_len != 0 )
    {
        // Error handling
    }
    

    To decompress data:

    #include "lzav.h"
    
    void* decomp_buf = malloc( src_len );
    int l = lzav_decompress( comp_buf, decomp_buf, comp_len, src_len );
    
    if( l < 0 )
    {
        // Error handling
    }
    

    To compress data with a higher compression ratio for non-time-critical uses (e.g., compression of an application's static assets):

    #include "lzav.h"
    
    int max_len = lzav_compress_bound_hi( src_len ); // Note the different bound function
    void* comp_buf = malloc( max_len );
    int comp_len = lzav_compress_hi( src_buf, comp_buf, src_len, max_len );
    
    if( comp_len == 0 && src_len != 0 )
    {
        // Error handling
    }
    

    LZAV's source code conforms to ISO C99 and has been tested with Clang, GCC, MSVC, and the Intel C++ compiler on x86, x86-64 (Intel, AMD), and AArch64 (Apple Silicon) systems running Windows 10, Windows 11, AlmaLinux 9.6, and macOS 26.4. Full C++ compatibility is automatically provided when the source code is compiled with a C++ compiler.

    Ports

    Customizing the C++ Namespace

    In C++ environments where it is undesirable to place LZAV symbols in the global namespace, the LZAV_NS_CUSTOM macro can be defined externally:

    #define LZAV_NS_CUSTOM lzav
    #include "lzav.h"
    

    For example, to place LZAV symbols in a custom namespace alongside your other data compression functions:

    #define LZAV_NS_CUSTOM my_compressors
    #include "lzav.h"
    

    As a result, LZAV functions can be referred to as my_compressors::lzav_compress_default(...). Note that because all LZAV functions are declared with the static specifier, there will be no ABI conflicts, even if the lzav.h header is included in multiple C/C++ translation units.

    Comparisons

    The tables below present ballpark performance numbers for the LZAV algorithm.

    While LZ4 compresses faster, LZAV provides 17% greater storage space savings. This is a significant benefit in database and filesystem use cases, since LZAV's compression is only about 30% slower than LZ4's. In practice, slower compression is not a limiting factor because writes of compressed data are deferred to background threads, and disk I/O time is reduced due to better compression. In general, LZAV holds a very strong position in this class of data compression algorithms when one considers all factors: compression and decompression speeds, compression ratio, and, just as importantly, code maintainability. LZAV is highly portable and has a rather small, independent codebase.

    The performance of LZAV is not limited to the ballpark numbers presented. Depending on the data being compressed, LZAV can achieve compression speeds of 800 MB/s and decompression speeds of 5,000 MB/s. Incompressible data is decompressed at a rate of 10,000 MB/s, which is close to the speed of memcpy(). For datasets such as enwik9, LZAV provides 24% greater storage space savings than LZ4.

    The geometric mean speeds of the LZAV algorithm across a variety of datasets are 550 ± 150 MB/s for compression and 3,800 ± 1,300 MB/s for decompression. These numbers apply to 64-bit processors released since 2019 that run at 4 GHz or higher. Note that the algorithm exhibits adaptive qualities, and its actual performance depends on the data being compressed. LZAV may show exceptional performance on your specific data, including, but not limited to, sparse databases, log files, and HTML/XML files.

    It is also worth noting that compression methods like LZAV and LZ4 usually have an advantage over explicit dictionary- and entropy-based methods. Hash-table-based compression has low memory usage and minimal operational overhead, and classic LZ77 decompression has no overhead at all; this is especially relevant for smaller datasets.

    For a more comprehensive benchmark of in-memory compression algorithms, see lzbench.

    The benchmarks below use the Silesia compression corpus.

    Apple clang 15.0.0 arm64, macOS 26.4, Apple M1, 3.5 GHz

    CompressorCompressionDecompressionRatio %
    LZAV 7.1638 MB/s3,800 MB/s39.23
    LZ4 1.9.4700 MB/s4,570 MB/s47.60
    Snappy 1.1.10495 MB/s3,230 MB/s48.22
    LZF 3.6395 MB/s800 MB/s48.15
    LZAV 7.1 HI157 MB/s3,740 MB/s34.55
    LZ4HC 1.9.4 -940 MB/s4,360 MB/s36.75

    LLVM clang 19.1.7 x86-64, AlmaLinux 9.6, Xeon E-2386G (Rocket Lake), 5.1 GHz

    CompressorCompressionDecompressionRatio %
    LZAV 7.1658 MB/s3,650 MB/s39.23
    LZ4 1.9.4848 MB/s4,980 MB/s47.60
    Snappy 1.1.10690 MB/s3,360 MB/s48.22
    LZF 3.6455 MB/s1,000 MB/s48.15
    LZAV 7.1 HI139 MB/s3,390 MB/s34.55
    LZ4HC 1.9.4 -943 MB/s4,920 MB/s36.75

    LLVM clang-cl 18.1.8 x86-64, Windows 10, Ryzen 3700X (Zen 2), 4.2 GHz

    CompressorCompressionDecompressionRatio %
    LZAV 7.1600 MB/s3,270 MB/s39.23
    LZ4 1.9.4675 MB/s4,560 MB/s47.60
    Snappy 1.1.10415 MB/s2,440 MB/s48.22
    LZF 3.6310 MB/s700 MB/s48.15
    LZAV 7.1 HI129 MB/s3,100 MB/s34.55
    LZ4HC 1.9.4 -936 MB/s4,430 MB/s36.75

    Note: The popular Zstd compressor is not included here because it is not a pure LZ77 algorithm, it is much harder to integrate, and it has a much larger codebase. These aspects place it in the same league as zlib. Here are the author's Zstd measurements with TurboBench on a Ryzen 3700X, using the Silesia dataset:

    CompressorCompressionDecompressionRatio %
    zstd 1.5.5 fast -1460 MB/s1,870 MB/s41.0
    zstd 1.5.5 -1436 MB/s1,400 MB/s34.6

    Dataset Benchmarks

    This section presents compression ratio comparisons for various popular datasets. Note that each file within these datasets was compressed individually, which is reflected in the overall ratio.

    DatasetSize (MiB)LZAV 7.1LZ4 1.9.4Snappy 1.1.10LZF 3.6Source
    4SICS 151020 PCAP24.520.2321.8224.9525.34www.netresec.com
    4SICS 151022 PCAP200.036.0137.3540.2441.37www.netresec.com
    Calgary Large3.142.7951.9751.7649.07data-compression.info
    Canterbury2.737.4643.7345.4242.49corpus.canterbury.ac.nz
    Canterbury Large10.636.1851.9748.3754.28corpus.canterbury.ac.nz
    Canterbury Artificial0.333.3833.7436.4834.66corpus.canterbury.ac.nz
    employees_10KB.json0.0122.1524.6823.9223.52sample.json-format.com
    employees_100KB.json0.115.8017.7119.0221.88sample.json-format.com
    employees_50MB.json51.510.6216.4218.5721.44sample.json-format.com
    enwik895.443.3857.2656.5653.95www.mattmahoney.net
    enwik9954.738.4350.9250.7949.30www.mattmahoney.net
    Manzini855.325.8937.3038.5739.04people.unipmn.it/manzini
    chr22.dna (Manzini)33.036.0952.8244.5355.86people.unipmn.it/manzini
    w3c2 HTML (Manzini)99.410.9522.2025.3527.20people.unipmn.it/manzini
    Silesia202.139.2347.6048.1748.15github.com/MiloszKrajewski

    Notes

    1. The LZAV API is not equivalent to the LZ4 and Snappy APIs. For example, the dstlen parameter of the decompression function must specify the original uncompressed length, which must have been previously stored in some way, independently of LZAV.

    2. From a technical point of view, LZAV's peak decompression speed is implicitly limited by its stream format, which is more complex than LZ4's. LZAV decompression requires extensive branching. Another limiting factor is a rather large dynamic LZ77 window (4 MiB to 1 GiB), which is not CPU-cache-friendly. However, without these features, it would not be possible to achieve competitive compression ratios while maintaining high compression speeds.

    3. LZAV supports compression of data blocks up to 2 GiB. Larger datasets should be compressed in chunks of at least 16 MiB. Using smaller chunks may reduce the compression ratio.