Zstandard, LZ4, Brotli and XZ written entirely in C#. No native library and no P/Invoke: the code runs wherever .NET runs on a little-endian processor, trims, and compiles with Native AOT.
-
Brotli at least as fast as the brotli .NET ships at every quality, on x64: compression 1.00 to 1.29× on large inputs and 1.03 to 21× on small ones, with brotli 1.1.1's bytes; decompression 1.07 to 1.37×, with brotli's verdict and error code for every stream (
src/ManagedCompressions.Brotli). -
Same bytes as the native libraries. Every frame, block and stream is byte-for-byte what libzstd 1.5.7, lz4 1.11.1, brotli 1.1.1 and liblzma 5.8.4 write, at every level, so the compression ratios are theirs. The tests check this.
-
Faster where it counts. Zstandard decompresses 1.3 to 1.5× faster than native libzstd on arm64, and 1.0 to 1.2× on x64, where native libzstd has hand-written assembly. LZ4 decompresses 2 to 4 % faster than native lz4 on arm64. On x64 it is within 2 % of it on large blocks, and on 64 KiB pages at its speed or 10 to 14 % slower, depending on the process. On small records, compression is up to 1.5× faster with Zstandard and up to 5.7× with LZ4.
-
At native speed elsewhere. Zstandard compresses large inputs from 6 % slower to 10 % faster than libzstd. LZ4 compresses 3 to 6 % slower at level 1, and up to 15 % slower at its high-compression levels.
-
No allocation per call. A compressor or decompressor is created once and reused.
The speeds are measured against NativeCompressions,
Cysharp's binding of the native libraries, and against .NET 11's own System.IO.Compression Zstandard.
Note
The figures in this README come from two machines: an Apple M4 Pro (arm64) under macOS, and an AMD Ryzen 9 7950X (x64) under Windows 11. The relative speeds differ between them: on x64, native libzstd decodes with hand-written assembly, and the managed decompressor's lead is smaller.
Apple M4 Pro (arm64) and AMD Ryzen 9 7950X (x64). silesia.tar (211 MB) compressed or decompressed in one call, on one thread, with BenchmarkDotNet. Native is NativeCompressions; JIT and AOT are the managed code under the JIT and under Native AOT. Throughputs are in MB/s of uncompressed data, and × is the managed speed divided by the native speed on the same machine: above 1.00 is faster, and bold is at least 10 % faster.
The levels are the ones in use. For Zstandard: 1, the fastest of the regular levels; 3, the default of
zstd, NativeCompressions and .NET; 9; and 19, the highest before zstd's --ultra levels. For LZ4: 1,
LZ4_compress_default; 9, lz4's default high-compression level; and 12, its highest.
Apple M4 Pro, arm64
| Level | Native, MB/s | JIT, MB/s | JIT × | AOT, MB/s | AOT × | |
|---|---|---|---|---|---|---|
| Decompression | 1 | 1,825 | 2,378 | 1.30× | 2,391 | 1.30× |
| 3 | 1,663 | 2,413 | 1.45× | 2,434 | 1.46× | |
| 9 | 1,873 | 2,686 | 1.43× | 2,691 | 1.44× | |
| 19 | 1,728 | 2,566 | 1.49× | 2,567 | 1.47× | |
| Compression | 1 | 759 | 760 | 1.00× | 771 | 1.01× |
| 3 | 452 | 461 | 1.02× | 465 | 1.03× | |
| 9 | 115 | 109 | 0.95× | 112 | 0.96× | |
| 19 | 4.9 | 4.6 | 0.94× | 4.7 | 1.02× |
AMD Ryzen 9 7950X, x64
| Level | Native, MB/s | JIT, MB/s | JIT × | AOT, MB/s | AOT × | |
|---|---|---|---|---|---|---|
| Decompression | 1 | 2,105 | 2,149 | 1.02× | 2,171 | 1.03× |
| 3 | 1,835 | 1,982 | 1.08× | 2,022 | 1.10× | |
| 9 | 1,989 | 2,176 | 1.09× | 2,200 | 1.11× | |
| 19 | 1,830 | 1,988 | 1.09× | 2,032 | 1.10× | |
| Compression | 1 | 690 | 709 | 1.03× | 696 | 1.01× |
| 3 | 435 | 425 | 0.98× | 421 | 0.97× | |
| 9 | 112 | 109 | 0.97× | 112 | 1.03× | |
| 19 | 5.4 | 5.7 | 1.06× | 5.7 | 1.08× |
Apple M4 Pro, arm64
| Level | Native, MB/s | JIT, MB/s | JIT × | AOT, MB/s | AOT × | |
|---|---|---|---|---|---|---|
| Decompression | 1 | 5,950 | 6,174 | 1.04× | 6,167 | 1.03× |
| 9 | 5,687 | 5,871 | 1.03× | 5,889 | 1.03× | |
| 12 | 5,813 | 6,044 | 1.04× | 6,063 | 1.04× | |
| Compression | 1 | 952 | 913 | 0.96× | 919 | 0.96× |
| 9 | 52 | 48 | 0.93× | 48 | 0.92× | |
| 12 | 17.8 | 15.5 | 0.87× | 15.3 | 0.85× |
AMD Ryzen 9 7950X, x64
| Level | Native, MB/s | JIT, MB/s | JIT × | AOT, MB/s | AOT × | |
|---|---|---|---|---|---|---|
| Decompression | 1 | 6,265 | 6,407 | 1.02× | 6,276 | 1.00× |
| 9 | 5,989 | 6,059 | 1.01× | 5,924 | 1.00× | |
| 12 | 6,161 | 6,168 | 1.00× | 6,097 | 0.99× | |
| Compression | 1 | 997 | 954 | 0.96× | 961 | 0.96× |
| 9 | 56 | 53 | 0.95× | 51 | 0.91× | |
| 12 | 18.4 | 16.8 | 0.91× | 16.4 | 0.89× |
.NET 11's own Zstandard (ZstandardEncoder, ZstandardDecoder) is the same libzstd. On arm64 it runs at
NativeCompressions' speed: 0.98× to 1.02× of it, except for level 19's compression under the JIT (0.93×).
That exception is noise. Zstandard's level 19 and LZ4's level 12 take 11 to 45 seconds a call, so they are
measured on three calls instead of ten, and at level 19 NativeCompressions' own time varied by 5 % from one
job to the other (43.0 s, then 45.5 s) where .NET's and the managed code's varied by 1 to 2 %: read level
19's compression row as on par. On x64, .NET's libzstd is slower than NativeCompressions': 0.82× to 1.03×
of it. At level 19 there, NativeCompressions took 39.2 s, then 39.7 s, and .NET 40.2 s, then 38.6 s. The
full BenchmarkDotNet tables, with Brotli and GZip for scale, are in
bench/results/arm64/benchmarkdotnet-silesia.md and
bench/results/x64/benchmarkdotnet-silesia.md.
The packages target .NET 10 and .NET 11:
dotnet add package ManagedCompressions.Zstd
dotnet add package ManagedCompressions.Lz4
dotnet add package ManagedCompressions.Brotliusing ManagedCompressions.Zstd;
var compressor = new ZstdCompressor(level: 3); // -131072 to 22; reuse it, it keeps its tables
byte[] frame = new byte[ZstdCompressor.GetMaxCompressedLength(data.Length)];
compressor.Compress(data, frame, out _, out int frameLength);
var decompressor = new ZstdDecompressor(); // reuse it too
ZstdDecompressor.TryGetFrameContentSize(frame, out ulong contentSize);
byte[] content = new byte[contentSize];
decompressor.Decompress(frame.AsSpan(0, frameLength), content, out _, out _);Dictionaries (new ZstdCompressor(level, dictionary)), checksums (AppendChecksum = true) and parallel
compression (CompressAsync, which writes the same frames as libzstd with worker threads) are covered in
src/ManagedCompressions.Zstd/README.md.
using ManagedCompressions.Lz4;
var compressor = new Lz4Compressor(level: 1); // 1 = LZ4_compress_default, below 1 = faster, 2-12 = high compression
byte[] block = new byte[Lz4Compressor.GetMaxCompressedLength(data.Length)];
int blockLength = compressor.Compress(data, block);
byte[] content = new byte[data.Length]; // a block does not record its size: store it beside
Lz4Block.Decompress(block.AsSpan(0, blockLength), content);Lz4Block.TryDecompress reads into a larger destination and returns false on a corrupt block.
Lz4Compressor.TryCompress returns false when the destination is too small.
LZ4 frames, the .lz4 files of the lz4 program, with lz4frame's bytes:
using (var lz4 = new Lz4Stream(File.Create("data.lz4"), new Lz4FrameCompressionOptions { ContentChecksum = true }))
{
input.CopyTo(lz4);
}
using (var lz4 = new Lz4Stream(File.OpenRead("data.lz4"), CompressionMode.Decompress))
{
lz4.CopyTo(output);
}Lz4FrameCompressor writes a frame in one call, and Lz4FrameEncoder and Lz4FrameDecoder stream
frames with OperationStatus calls: see
src/ManagedCompressions.Lz4/README.md.
using ManagedCompressions.Brotli;
var compressor = new BrotliCompressor(quality: 4); // .NET's default quality; reuse it, it keeps its tables
byte[] stream = new byte[BrotliCompressor.GetMaxCompressedLength(input.Length)];
compressor.Compress(input, stream, out _, out int size); // the bytes BrotliEncoder.TryCompress writes
var decompressor = new BrotliDecompressor(); // reuse it, it keeps its tables
byte[] content = new byte[contentSize]; // a stream does not record its size: store it beside
OperationStatus status = decompressor.Decompress(stream.AsSpan(0, size), content, out int consumed, out int written);decompressor.LastError names the check that refused a stream, as brotli does. BrotliCompressionStream
is a managed BrotliStream, which writes its bytes and reads streams of any size, and BrotliStreamEncoder
and BrotliStreamDecoder work piece by piece, as BrotliEncoder and BrotliDecoder do
(src/ManagedCompressions.Brotli).
The names do not clash with System.IO.Compression's BrotliEncoder, BrotliDecoder and BrotliStream.
using ManagedCompressions.Xz;
var compressor = new XzCompressor(preset: 6); // 0 to 9, xz's presets; liblzma's bytes at each
byte[] file = new byte[XzCompressor.GetMaxCompressedLength(data.Length)];
compressor.Compress(data, file, out _, out int fileLength);
XzDecompressor.TryGetUncompressedLength(file.AsSpan(0, fileLength), out long length); // a claim
byte[] content = new byte[length];
var decompressor = new XzDecompressor(); // reuse it
decompressor.Decompress(file.AsSpan(0, fileLength), content, out _, out int written);Decompress reads a whole .xz file, every stream of it, with liblzma 5.8.4's verdicts: LastError
names the check that refused a file. The destination is the dictionary, so nothing is allocated whatever
the file declares. XzStream and XzDecoder read files of any size as they arrive:
new TarReader(new XzStream(File.OpenRead("x.tar.xz"), CompressionMode.Decompress)). CompressAsync writes
the files of xz -T, liblzma's bytes in blocks compressed on several threads, which DecompressAsync
decodes on several threads
(src/ManagedCompressions.Xz).
Under the JIT, on two machines: an Apple M4 Pro (arm64, macOS) and an AMD Ryzen 9 7950X (x64, Windows). Each cell is the managed speed divided by the native speed (NativeCompressions at its fastest, with its context reused), on that machine. Bold means at least 10 % faster. The workloads are:
- silesia.tar as one 211 MB frame or block;
- the same file cut into 64 KiB pages, each its own frame or block, as a columnar file or a network protocol compresses data;
- 500 JSON records of about 800 bytes each, zstd's own regression data, each its own frame or block;
- zstd only: the same records with their 110 KiB dictionary.
| Decompression | 1 | 3 | 5 | 7 | 9 | 13 | 16 | 19 | |
|---|---|---|---|---|---|---|---|---|---|
| silesia.tar, one frame | arm64 | 1.29× | 1.47× | 1.48× | 1.46× | 1.44× | 1.43× | 1.39× | 1.47× |
| x64 | 1.01× | 1.08× | 1.10× | 1.10× | 1.09× | 1.08× | 1.08× | 1.09× | |
| silesia.tar, 64 KiB pages | arm64 | 1.26× | 1.36× | 1.46× | 1.42× | 1.42× | 1.40× | 1.47× | 1.48× |
| x64 | 1.01× | 1.05× | 1.08× | 1.05× | 1.06× | 1.05× | 1.07× | 1.06× | |
| JSON records | arm64 | 1.49× | 1.50× | 1.51× | 1.50× | 1.51× | 1.50× | 1.48× | 1.50× |
| x64 | 1.19× | 1.13× | 1.13× | 1.13× | 1.13× | 1.15× | 1.14× | 1.15× | |
| JSON records + dictionary | arm64 | 1.44× | 1.46× | 1.49× | 1.42× | 1.42× | 1.41× | 1.53× | 1.52× |
| x64 | 1.15× | 1.12× | 1.20× | 1.14× | 1.16× | 1.14× | 1.18× | 1.20× |
| Compression | 1 | 3 | 5 | 7 | 9 | 13 | 16 | 19 | |
|---|---|---|---|---|---|---|---|---|---|
| silesia.tar, one frame | arm64 | 1.00× | 1.01× | 0.96× | 0.96× | 0.98× | 0.97× | 1.01× | 1.08× |
| x64 | 1.02× | 0.97× | 0.99× | 1.01× | 1.00× | 1.01× | 1.02× | 1.07× | |
| silesia.tar, 64 KiB pages | arm64 | 1.01× | 1.01× | 0.96× | 0.96× | 0.95× | 1.04× | 1.00× | 0.98× |
| x64 | 1.01× | 0.96× | 0.97× | 0.98× | 1.00× | 1.01× | 1.10× | 1.10× | |
| JSON records | arm64 | 1.29× | 1.19× | 1.12× | 1.09× | 1.17× | 1.24× | 1.16× | 1.14× |
| x64 | 1.32× | 1.17× | 1.07× | 1.07× | 1.17× | 1.11× | 1.12× | 1.12× | |
| JSON records + dictionary | arm64 | 1.31× | 1.35× | 0.97× | 0.93× | 0.93× | 1.39× | 1.33× | 1.31× |
| x64 | 1.44× | 1.45× | 0.99× | 0.99× | 1.02× | 1.22× | 1.15× | 1.15× |
Decompression is faster everywhere: 1.3 to 1.5× on arm64, 1.0 to 1.2× on x64. The native side is stronger on x64. NativeCompressions builds its win-x64 libzstd with GCC, which compiles zstd's hand-written assembly Huffman decoder and its BMI2 code paths. Both exist for x86-64 only, so the arm64 build has no counterpart. Compression of large inputs runs at the native speed, give or take 5 %, and up to 10 % faster at levels 16 and 19 on x64: the compressor runs libzstd's algorithms step for step to write its exact bytes, so it does the same work. Small records compress faster because the managed code has a smaller fixed cost per frame.
| Decompression | 1 | 2 | 3 | 6 | 9 | 12 | |
|---|---|---|---|---|---|---|---|
| silesia.tar, one block | arm64 | 1.03× | 1.04× | 1.03× | 1.04× | 1.04× | 1.03× |
| x64 | 0.98× | 0.98× | 0.97× | 0.96× | 0.96× | 0.96× | |
| silesia.tar, 64 KiB pages | arm64 | 1.04× | 1.03× | 1.02× | 1.02× | 1.02× | 1.02× |
| x64 | 0.98× | 0.98× | 0.96× | 0.96× | 0.95× | 0.95× | |
| JSON records | arm64 | 1.10× | 1.11× | 1.10× | 1.10× | 1.10× | 1.08× |
| x64 | 1.02× | 1.05× | 1.04× | 1.00× | 0.99× | 1.05× |
| Compression | 1 | 2 | 3 | 6 | 9 | 12 | |
|---|---|---|---|---|---|---|---|
| silesia.tar, one block | arm64 | 0.96× | 0.92× | 1.00× | 0.93× | 0.88× | 1.00× |
| x64 | 0.91× | 0.91× | 0.99× | 0.97× | 0.98× | 0.97× | |
| silesia.tar, 64 KiB pages | arm64 | 0.95× | 0.92× | 0.95× | 0.92× | 0.87× | 0.86× |
| x64 | 0.94× | 0.93× | 0.99× | 0.98× | 0.95× | 0.96× | |
| JSON records | arm64 | 1.24× | 5.66× | 3.87× | 3.88× | 3.38× | 1.51× |
| x64 | 1.17× | 0.93× | 0.94× | 0.98× | 0.89× | 0.89× |
The x64 rows are against lz4 1.11.1 (NativeCompressions.LZ4 1.3.4), the version the compressors write
the bytes of, its high-compression levels on a state kept from block to block (LZ4_resetStreamHC_fast,
then LZ4_compress_HC_continue), as the managed compressor keeps its tables. The arm64 rows are earlier
measurements against lz4 1.10.0 through LZ4_compress_HC_extStateHC, which clears 256 KiB of tables
before every block: on the JSON records at levels 2 to 12, that clearing is most of what they show, and
they await a new run.
The fast levels clear 16 KiB before every block in lz4 (LZ4_compress_default). The managed compressor
keeps that table from one block to the next for blocks under 1 KiB, and numbers each block's positions past
the last block's, so that whatever earlier blocks left reads exactly as a cleared entry: the bytes are
unchanged, and small blocks skip that cost (level 1 on the JSON records). From 1 KiB on it clears the table
too: a table just cleared, all in the cache, then saves more than its clearing costs.
On large blocks the compressors run at 0.91× to 0.99× of lz4 1.11.1 on x64, levels 1 and 2 the furthest
behind. Level 9 on the JSON records depends on the process on x64: 0.88× to 0.92× in the processes the
table takes the median of, 0.59× to 0.73× in others, the JIT compiling the hash chains differently from one
process to the next. Earlier runs also saw the decompression of the 64 KiB pages depend on the process
(0.86× to 1.01×); this one decoded them at 0.95× to 0.98×
(bench/results/x64/levels.md).
On x64 (AMD Ryzen 9 7950X), against the brotli 1.1.1 .NET ships, through BrotliEncoder.TryCompress
and BrotliDecoder.TryDecompress. The managed compressor writes brotli's bytes; it keeps its tables from
one stream to the next where brotli allocates and clears them at every call, fetches the lines of its hash
tables ahead of the search, writes its bits through a 64-bit accumulator, several codes at a time at the
fast qualities, and runs the block splitter of qualities 10 and 11 on vectors. The decoder writes
straight into the destination where brotli goes through a ring buffer, decodes literals in runs between
its checks, and looks literals up in tables of 10 bits where brotli's have 8
(src/ManagedCompressions.Brotli/README.md).
| Compression | 0 | 1 | 4 | 5 | 9 | 10 | 11 |
|---|---|---|---|---|---|---|---|
| silesia.tar sample, one stream | 1.00× | 1.12× | 1.08× | 1.29× | 1.18× | 1.18× | 1.15× |
| JSON records | 1.09× | 1.06× | 1.16× | 12.4× | 21.0× | 1.13× | 1.03× |
Qualities 4 to 9 move by up to 20 % from one process to the next, on both sides
(bench/results/x64/brotli-compression.md
has every quality).
| Decompression | 1 | 4 | 5 | 9 | 11 |
|---|---|---|---|---|---|
| silesia.tar, one stream | 1.34× | 1.37× | 1.21× | 1.25× | 1.07× |
| silesia.tar, 64 KiB pages | 1.34× | 1.27× | 1.25× | 1.24× | 1.08× |
| JSON records | 1.10× | 1.10× | 1.10× | 1.08× | 1.06× |
On x64 (AMD Ryzen 9 7950X) so far, against liblzma 5.8.4 built from xz-utils' sources, through its buffer
calls (lzma_easy_buffer_encode with a CRC64 check, lzma_stream_buffer_decode), which build and free
their coder at every call where the managed compressor and decompressor keep theirs
(bench/results/x64/xz-levels.md).
The arm64 machine has yet to measure them.
| Compression | 0 | 1 | 3 | 6 | 9 |
|---|---|---|---|---|---|
| silesia.tar, one stream | 1.01× | 0.99× | 1.04× | 0.98× | 0.97× |
| silesia.tar, 64 KiB pages | 1.08× | 1.67× | 2.15× | 1.36× | 1.73× |
| JSON records | 2.14× | 18.6× | 21.6× | 6.89× | 6.79× |
| Decompression | 0 | 1 | 3 | 6 | 9 |
|---|---|---|---|---|---|
| silesia.tar, one stream | 0.90× | 0.92× | 0.91× | 0.92× | 0.93× |
| silesia.tar, 64 KiB pages | 0.92× | 0.96× | 0.97× | 0.95× | 0.96× |
| JSON records | 0.86× | 1.91× | 2.73× | 2.73× | 2.79× |
The managed compressors write the native bytes, so the ratios below hold for both sides, on arm64 and x64 alike.
| Zstandard level | 1 | 3 | 5 | 7 | 9 | 13 | 16 | 19 |
|---|---|---|---|---|---|---|---|---|
| silesia.tar | 2.90 | 3.20 | 3.38 | 3.52 | 3.59 | 3.67 | 3.83 | 4.01 |
| JSON records + dictionary | 9.89 | 9.92 | 10.53 | 10.52 | 10.34 | 10.22 | 10.76 | 10.76 |
| LZ4 level | 1 | 2 | 3 | 6 | 9 | 12 |
|---|---|---|---|---|---|---|
| silesia.tar | 2.10 | 2.38 | 2.61 | 2.70 | 2.72 | 2.74 |
| Brotli quality | 0 | 1 | 2 | 4 | 5 | 9 | 10 | 11 |
|---|---|---|---|---|---|---|---|---|
| silesia.tar, 1 MiB every 16 MiB | 2.25 | 2.44 | 2.60 | 2.66 | 2.88 | 2.95 | 3.21 | 3.31 |
| JSON records | 2.43 | 2.78 | 2.86 | 3.05 | 3.31 | 3.33 | 3.33 | 3.47 |
| XZ preset | 0 | 1 | 3 | 6 | 9 |
|---|---|---|---|---|---|
| silesia.tar | 3.39 | 3.63 | 3.80 | 4.31 | 4.35 |
| JSON records | 2.38 | 2.39 | 2.39 | 2.41 | 2.41 |
Zstandard (ManagedCompressions.Zstd) |
LZ4 (ManagedCompressions.Lz4) |
Brotli (ManagedCompressions.Brotli) |
XZ (ManagedCompressions.Xz) |
|
|---|---|---|---|---|
| Format | RFC 8878 frames, skippable frames | the LZ4 block format (Parquet's LZ4_RAW); LZ4 frames, lz4frame's bytes (Lz4FrameCompressor, Lz4FrameEncoder, Lz4FrameDecoder), skippable frames, legacy frames read on request |
RFC 7932 streams, metadata blocks | .xz files (format 1.2.1): concatenated streams and their padding, LZMA2 alone or after delta and the eight branch filters; .lzma files (LzmaEncoder, LzmaDecompressor, LzmaDecoder, LzmaStream); raw LZMA1 and LZMA2 (LzmaRawEncoder, LzmaRawDecoder) |
| Compression levels | -131072 to 22, libzstd's bytes at each | acceleration 65537 to level 12, lz4's bytes at each | qualities 0 to 11, brotli's bytes at each | presets 0 to 9 and extreme, liblzma's bytes at each |
| Dictionaries | yes, at compression and decompression | frames: at compression and decompression | the static dictionary; no shared dictionaries (RFC 9841) | – |
| Parameters | the level, the window, the long-distance matcher, the header's flags (ZstdCompressionOptions) |
the level; frames: lz4frame's preferences (Lz4FrameCompressionOptions) |
the quality, the window (10 to 24 bits), the mode (generic, text, font) | the preset, the check, or LZMA2's options (LzmaOptions: dictionary, lc, lp, pb, mode, match finder, nice length, depth) |
| Checksums | XXH64, written on request, always verified | not in the block format; a frame's XXH32 of its blocks and of its content, written on request, verified | not in the format | CRC32, CRC64, SHA-256, written (CRC64 by default), verified unless IgnoreChecks; the reserved check IDs skipped, as liblzma does, or refused on request; each index verified against its blocks |
| Parallel compression | CompressAsync, libzstd's multithreaded frames |
no | – | CompressAsync, liblzma's multithreaded files (xz -T); their blocks decode in parallel (DecompressAsync) |
| Streaming | decompression (ZstdDecoder), any size, its memory bounded by the window; compression (ZstdEncoder), libzstd's streamed frames; ZstdStream, both ways |
frames: compression (Lz4FrameEncoder), lz4frame's streamed bytes; decompression (Lz4FrameDecoder), any size, its memory bounded by a block; Lz4Stream, both ways |
BrotliCompressionStream, both ways; compression (BrotliStreamEncoder), brotli's streamed bytes; decompression (BrotliStreamDecoder), any size, its memory bounded by the window and a meta-block |
compression (XzEncoder), liblzma's streamed bytes; decompression (XzDecoder), any size, its memory bounded by the dictionary a block declares; XzStream, both ways |
| Validation | accepts and refuses what libzstd does | refuses any literal or match past either end; frames: accepts and refuses what lz4frame does, with its error codes | accepts and refuses what brotli 1.1.1 does, with its error codes; refuses the large window | accepts and refuses what liblzma 5.8.4 does, with the check that failed |
| Processors | little-endian, in 64-bit processes: x64 and arm64; 32-bit processes and big-endian processors refused | the same | the same | the same |
Not supported: lz4frame's multithreaded compression, Brotli's large window and shared
dictionaries, lzip's .lz files, one-shot inputs over 2 GiB
(ZstdEncoder, BrotliStreamEncoder and XzEncoder stream them), libzstd's finer parameters
(strategy, hash and chain logs, target block size), big-endian processors (IBM Z) and 32-bit processes
(x86, 32-bit Arm, WebAssembly), where the constructors throw PlatformNotSupportedException: the
codecs compare pointers that 32-bit addresses would let wrap around, and nothing tests them there.
The codecs' hot loops read and write through pointers and Unsafe, without the bounds checks of spans:
on x64, ports of the LZ4 and Zstandard decoders to safe code ran at 74 to 87 % of their speed with the
JIT, and the Zstandard one at half of it under Native AOT. Instead, the code states the bounds it relies
on, as Microsoft's
unsafe code best practices
recommend: the decoders, which read untrusted input, those of each access they make without a check; the
compressors, those of their writes, to the destination and to their own buffers, and of what they read
in those buffers. The Debug build asserts them, and the Release build drops the assertions
with their arguments, so that its machine code is the same as without them
(src/Shared/Bounds.cs).
What checks them, on every pull request:
- The whole test suite in Debug, where an access past its bounds fails its test.
- Sources and destinations against inaccessible pages, in the tests and the fuzzing, on Linux and macOS: a byte read or written past either end faults.
- Fuzzing, for two minutes per target, and for 50 minutes each night on the Debug build, where an input
that takes an access past its bounds fails (
fuzz/). - The GC's heap verification over the whole suite (
DOTNET_HeapVerify), which fails a write past an array that damages the heap, whether an assertion states the access or not.
- Machines: an Apple M4 Pro (arm64) under macOS 26.7, on .NET 11.0.0-rc.1, and an AMD Ryzen 9 7950X
(x64, Zen 4) under Windows 11 (build 26300), on .NET 11.0.0, both on one thread. On Windows the
benchmarks run at a high priority, and the level-by-level one keeps its timed thread on one logical
processor (
--core 2), as macOS keeps it on a performance core. - .NET 10: the figures are .NET 11's, and .NET 10 runs the managed codecs at nearly the same speed.
On the Ryzen 9 7950X, at levels 1, 3 and 9 of every level-by-level workload, its speed against the
native side is 0.99 of .NET 11's on average, and two cases are more than 5 % slower, by 7 %, as much as
they vary from one process to the next on one runtime
(
bench/results/x64/dotnet10). The arm64 machine has yet to measure it. - Native side: NativeCompressions.Zstandard 1.3.0 and NativeCompressions.LZ4 1.2.0, which ship zstd
1.5.7 and lz4 1.10.0 built at
-O3: the same libraries as NativeCompressions 1.1.0, which the arm64 figures were measured with. LZ4's x64 figures were measured again against NativeCompressions.LZ4 1.3.4, which ships lz4 1.11.1. On arm64, its libzstd decompresses at the speed of the one .NET ships. A libzstd built from source with Apple clang 21 decompresses some 20 % faster: against that one, Zstandard decompression is 1.11–1.21× as fast, assrc/ManagedCompressions.Zstd/README.mdshows. On x64, NativeCompressions' libraries are built with GCC 16.2 (MSYS2). That libzstd has zstd's assembly Huffman decoder and its BMI2 code, which only exist for x86-64. - Level-by-level tables:
bench/ManagedCompressions.Benchtimes the native and the managed side in alternating rounds and reports each one's median (and on x64 the managed speedup taken round by round), after a warm-up that gives the JIT its final, profiled code. Its first round checks that the managed output matches the native output byte for byte, and that everything decodes back. The raw results are inbench/results/arm64/levels.mdandbench/results/x64/levels.md. - At-a-glance tables:
bench/ManagedCompressions.Benchmarksruns BenchmarkDotNet the way NativeCompressions' README does, under the JIT and under Native AOT. Each case first runs the managed codec on the first MiB of silesia.tar, so that the JIT has given it its final, profiled code, as a running service would have it: a single call of 211 MB would otherwise be measured in an earlier tier. Native AOT compiles for the machine's instruction set:nativeon arm64,x86-64-v4on x64, where ILC withnative, from 11.0.0-rc.1 to 11.0.0, compiles a branch onAvxVnni.IsSupportedinto an infinite loop and BenchmarkDotNet's CPU detection hangs (dotnet/runtime#135603).
To reproduce, start from the data and the native test tools:
bench/zstd-corpus.sh # silesia.zip and zstd's JSON records, downloaded and checked
tools/native-ref/testdata.sh # zstd's golden files, for the tests
tools/native-ref/build.sh mt # libzstd with threads, the oracle of the parallel compression tests
tools/native-ref/brotli-testdata.sh # brotli's test streams, for the tests
tools/native-ref/build-brotli.sh # brotli 1.1.1 with its whole API, the oracle of the Brotli tests
tools/native-ref/build-lz4.sh # lz4 1.11.1 with lz4frame, the oracle of the LZ4 tests
tools/native-ref/xz-testdata.sh # xz-utils' test files, for the tests
tools/native-ref/build-xz.sh # liblzma 5.8.4, the oracle of the XZ testsThen run the benchmarks:
dotnet run -c Release -f net11.0 --project bench/ManagedCompressions.Bench -- --codecs zstd,lz4,brotli --seconds 3
dotnet run -c Release --project bench/ManagedCompressions.Benchmarks -- --filter '*'On Windows, add --core 2 to the first command to keep its timed thread on one logical processor. With
-f net10.0, it measures the managed codecs on .NET 10.
Finally, run the tests:
dotnet test -c Release --solution ManagedCompressions.slnxThe tests check the compressors against the native libraries byte for byte, at every level. They also check the decoders against every frame and block the native libraries write, and against zstd's golden files and decodecorpus frames. Fuzzing runs with each buffer placed against an inaccessible memory page, so that a single byte read or written out of bounds faults.
CI builds everything and runs the tests on every pull request, the benchmarks excepted: their figures
mean something on a quiet machine only. It also runs the repository's prek
hooks, betterleaks for secrets and zizmor for the workflows; prek install runs them before each commit.
Each merged pull request updates a draft GitHub release; publishing it runs the same checks on its tag,
then pushes the packages to nuget.org.
| Path | Contents |
|---|---|
src/ManagedCompressions.Zstd |
Zstandard: decompressor, compressor, dictionaries, parallel compression |
src/ManagedCompressions.Lz4 |
LZ4 block format: Lz4Block (decoder, default compressor), Lz4Compressor (every level) |
src/ManagedCompressions.Brotli |
Brotli: BrotliCompressor (every quality), BrotliDecompressor, streaming (BrotliCompressionStream, BrotliStreamEncoder, BrotliStreamDecoder) |
src/ManagedCompressions.Xz |
XZ: XzCompressor (every preset), XzDecompressor, every stream of an .xz file, LZMA2 and its filters; streaming (XzDecoder, XzStream); parallel compression and decompression; .lzma files; raw LZMA1 and LZMA2 |
src/Shared |
what the four libraries compile in: the unchecked accesses and the bounds they assert |
tests/ |
differential tests against the native libraries, golden files, fuzzing |
fuzz/ |
coverage-guided fuzzing with libFuzzer, against the native libraries |
bench/ManagedCompressions.Bench |
level-by-level comparison with NativeCompressions |
bench/ManagedCompressions.Benchmarks |
BenchmarkDotNet on silesia.tar, JIT and Native AOT (--check verifies every case once) |
bench/ManagedCompressions.Zstd.Perf |
Zstandard against libzstd built from source, and micro-benchmarks |
bench/jit-profile.sh, bench/*-pcprofile.py |
instruction-level profilers, for the JIT and for Native AOT |
bench/results/arm64, bench/results/x64 |
the results above, for each machine |
tools/native-ref |
libzstd 1.5.7, brotli 1.1.1, lz4 1.11.1 and liblzma 5.8.4 built from source, and the writers of the test data |
tools/brotli |
the generator of Brotli's tables, from brotli's sources |
ManagedCompressions is licensed under the MIT License. The compressors are derived from libzstd (BSD-3-Clause) and lz4 (BSD-2-Clause), and Brotli's tables from brotli (MIT), and those parts remain under their licenses: see NOTICE, which each package carries. This project is not affiliated with the zstd, lz4 or brotli projects, nor with Cysharp or Google.
What the decoders guarantee on untrusted input, and how to report a vulnerability, is in SECURITY.md.