Home

Awesome

ZstdSharp

NuGet package NuGet package

ZstdSharp is a port of zstd compression library to С#
Based on Zstandard v1.5.6
Supports .NET Core 3.1, .NET 5+, .NET Standard 2.0+, .NET Framework 4.6.1+

Usage

ZstdSharp has an unsafe API much the same as zstd.
There are also safe wrappers.

Compress data:

var src = File.ReadAllBytes("dickens");
using var compressor = new Compressor(level);
var compressed = compressor.Wrap(src);

Decompress data:

var src = File.ReadAllBytes("dickens.zst");
using var decompressor = new Decompressor();
var decompressed = decompressor.Unwrap(src);

Streaming compression:

using var input = File.OpenRead("dickens");
using var output = File.OpenWrite("dickens.zst");
using var compressionStream = new CompressionStream(output, level);
input.CopyTo(compressionStream);

Streaming decompression:

using var input = File.OpenRead("dickens.zst");
using var output = File.OpenWrite("dickens");
using var decompressionStream = new DecompressionStream(input);
decompressionStream.CopyTo(output);

Multi-threaded compression:

using var input = File.OpenRead("dickens");
using var output = File.OpenWrite("dickens.zst");
using var compressionStream = new CompressionStream(output, level);
compressionStream.SetParameter(ZSTD_cParameter.ZSTD_c_nbWorkers, Environment.ProcessorCount);
input.CopyTo(compressionStream);

Benchmark

Best performance is achieved on .NET. System.Runtime.Intrinsics namespace is required for hardware accelerated bit and vector operations. .NET Standard and .NET Framework will use software implementation

Comparision zstd (native) and ZstdSharp

BenchmarkDotNet v0.14.0, Windows 10 (10.0.19045.5011/22H2/2022Update)
12th Gen Intel Core i7-12700, 1 CPU, 20 logical and 12 physical cores
.NET SDK 9.0.100
  [Host]   : .NET 8.0.11 (8.0.1124.51707), X64 RyuJIT AVX2
  .NET 9.0 : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2

Compression level 1

MethodMeanErrorStdDevRatio
CompressNative24.596 ms0.0341 ms0.0285 ms1.00
CompressSharp25.318 ms0.0329 ms0.0275 ms1.03
DecompressNative5.642 ms0.0145 ms0.0128 ms1.00
DecompressSharp5.896 ms0.0053 ms0.0045 ms1.05

Compression level 5

MethodMeanErrorStdDevRatio
CompressNative69.903 ms0.3729 ms0.3306 ms1.00
CompressSharp78.997 ms0.1981 ms0.1756 ms1.13
DecompressNative6.214 ms0.0154 ms0.0137 ms1.00
DecompressSharp6.746 ms0.0083 ms0.0069 ms1.09

Compression level 15

MethodMeanErrorStdDevRatio
CompressNative2,237.101 ms10.1419 ms9.4868 ms1.00
CompressSharp2,008.744 ms6.2452 ms5.5362 ms0.90
DecompressNative5.385 ms0.0088 ms0.0074 ms1.00
DecompressSharp5.687 ms0.0203 ms0.0180 ms1.06