blob: b84d8a8ffa57d6db2b1a504ce4d21cd6b89c865f [file] [log] [blame] [view]
Yann Collet4b100f42015-10-30 15:49:48 +01001 **Zstd**, short for Zstandard, is a fast lossless compression algorithm, targeting real-time compression scenarios at zlib-level compression ratio.
Yann Collet4856a002015-01-24 01:58:16 +01002
3It is provided as a BSD-license package, hosted on Github.
4
5|Branch |Status |
6|------------|---------|
7|master | [![Build Status](https://travis-ci.org/Cyan4973/zstd.svg?branch=master)](https://travis-ci.org/Cyan4973/zstd) |
8|dev | [![Build Status](https://travis-ci.org/Cyan4973/zstd.svg?branch=dev)](https://travis-ci.org/Cyan4973/zstd) |
9
Yann Collet45ff4302016-02-05 15:24:57 +010010As a reference, several fast compression algorithms were tested and compared to [zlib] on a Core i7-3930K CPU @ 4.5GHz, using [lzbench], an open-source in-memory benchmark by @inikep compiled with gcc 5.2.1, on the [Silesia compression corpus].
11
12[lzbench]: https://github.com/inikep/lzbench
13[Silesia compression corpus]: http://sun.aei.polsl.pl/~sdeor/index.php?page=silesia
14
Yann Collet4856a002015-01-24 01:58:16 +010015
Yann Collet4b100f42015-10-30 15:49:48 +010016|Name | Ratio | C.speed | D.speed |
17|-----------------|-------|--------:|--------:|
18| | | MB/s | MB/s |
Yann Collet31dd08c2016-02-16 16:06:53 +010019|**zstd 0.5.1 -1**|**2.876**|**330**| **890** |
Yann Collet45ff4302016-02-05 15:24:57 +010020| [zlib] 1.2.8 -1 | 2.730 | 95 | 360 |
21| brotli -0 | 2.708 | 220 | 430 |
22| QuickLZ 1.5 | 2.237 | 510 | 605 |
23| LZO 2.09 | 2.106 | 610 | 870 |
24| [LZ4] r131 | 2.101 | 620 | 3100 |
25| Snappy 1.1.3 | 2.091 | 480 | 1600 |
26| LZF 3.6 | 2.077 | 375 | 790 |
Yann Collet56213d82015-08-07 20:15:27 +010027
Yann Collet66d22b82015-11-02 02:36:10 +010028[zlib]:http://www.zlib.net/
Yann Collet45ff4302016-02-05 15:24:57 +010029[LZ4]: http://www.lz4.org/
Yann Collet4856a002015-01-24 01:58:16 +010030
Yann Collet45ff4302016-02-05 15:24:57 +010031Zstd can also offer stronger compression ratio at the cost of compression speed.
32Speed vs Compression trade-off is configurable by small increment. Decompression speed is preserved and remain roughly the same at all settings, a property shared by most LZ compression algorithms, such as [zlib].
Yann Collet4856a002015-01-24 01:58:16 +010033
Yann Collet45ff4302016-02-05 15:24:57 +010034The following test is run on a Core i7-3930K CPU @ 4.5GHz, using [lzbench], an open-source in-memory benchmark by @inikep compiled with gcc 5.2.1, on the [Silesia compression corpus].
Yann Collet7671f392015-11-02 12:17:39 +010035
36Compression Speed vs Ratio | Decompression Speed
Yann Collet8d8d59e2015-11-02 02:44:43 +010037---------------------------|--------------------
Yann Collet067a83a2016-02-17 18:03:17 +010038![Compression Speed vs Ratio](images/Cspeed4.png "Compression Speed vs Ratio") | ![Decompression Speed](images/Dspeed4.png "Decompression Speed")
39
40Several algorithms can produce higher compression ratio at slower speed, falling outside of the graph.
41For a larger picture including very slow modes, [click on this link](images/DCspeed5.png) .
Yann Collet8d8d59e2015-11-02 02:44:43 +010042
Yann Collet4856a002015-01-24 01:58:16 +010043
Yann Collet45ff4302016-02-05 15:24:57 +010044### The case for Small Data compression
45
Yann Collet31dd08c2016-02-16 16:06:53 +010046Above chart provides results applicable to large files or large streams scenarios (200 MB for this case).
Yann Collet45ff4302016-02-05 15:24:57 +010047Small data (< 64 KB) come with different perspectives.
48The smaller the amount of data to compress, the more difficult it is to achieve any significant compression.
49On reaching the 1 KB region, it becomes almost impossible to compress anything.
Yann Collet31dd08c2016-02-16 16:06:53 +010050This problem is common to any compression algorithms, and throwing CPU power at it achieves little gains.
Yann Collet45ff4302016-02-05 15:24:57 +010051
52The reason is, compression algorithms learn from past data how to compress future data.
53But at the beginning of a new file, there is no "past" to build upon.
54
Yann Collet31dd08c2016-02-16 16:06:53 +010055To solve this situation, Zstd now offers a __training mode__,
56which can be used to make the algorithm fit a selected type of data, by providing it with some samples.
57The result of the training is a file called "dictionary", which can be loaded before compression and decompression.
58Using this dictionary, the compression ratio achievable on small data improves dramatically :
Yann Collet45ff4302016-02-05 15:24:57 +010059
Yann Collet31dd08c2016-02-16 16:06:53 +010060| Collection Name | Direct compression | Dictionary Compression | Gains | Average unit | Range |
61| --------------- | ------------------ | ---------------------- | --------- | ------------:| ----- |
62| Small JSON records | x1.331 - x1.366 | x5.860 - x6.830 | ~ __x4.7__ | 300 | 200 - 400 |
63| Mercurial events | x2.322 - x2.538 | x3.377 - x4.462 | ~ __x1.5__ | 1.5 KB | 20 - 200 KB |
64| Large JSON docs | x3.813 - x4.043 | x8.935 - x13.366 | ~ __x2.8__ | 6 KB | 800 - 20 KB |
Yann Collet45ff4302016-02-05 15:24:57 +010065
Yann Collet31dd08c2016-02-16 16:06:53 +010066These compression gains are achieved without any speed loss, and prove in general a bit faster to compress and decompress.
Yann Collet45ff4302016-02-05 15:24:57 +010067
68Dictionary work if there is some correlation in a family of small data (there is no _universal dictionary_).
69Hence, deploying one dictionary per type of data will provide the greater benefits.
70
71Large documents will benefit proportionally less, since dictionary gains are mostly effective in the first few KB.
Yann Collet31dd08c2016-02-16 16:06:53 +010072Then, the compression algorithm will rely more and more on already decoded content to compress the rest of the file.
73
74#### Dictionary compression How To :
75
76##### _Using the Command Line Utility_ :
77
781) Create the dictionary
79
80`zstd --train FullPathToTrainingSet/* -o dictionaryName`
81
822) Compression with dictionary
83
84`zstd FILE -D dictionaryName`
85
863) Decompress with dictionary
87
88`zstd --decompress FILE.zst -D dictionaryName`
89
90##### _Using API_ :
91
921) Create dictionary
93
94```
95#include "zdict.h"
96(...)
97/* Train a dictionary from a memory buffer `samplesBuffer`,
98 where `nbSamples` samples have been stored concatenated. */
99size_t dictSize = ZDICT_trainFromBuffer(dictBuffer, dictBufferCapacity,
100 samplesBuffer, samplesSizes, nbSamples);
101```
102
1032) Compression with dictionary
104
105```
106#include "zstd.h"
107(...)
108ZSTD_CCtx* context = ZSTD_createCCtx();
109size_t compressedSize = ZSTD_compress_usingDict(context, dst, dstCapacity, src, srcSize, dict, dictSize, compressionLevel);
110```
111
1123) Decompress with dictionary
113
114```
115#include "zstd.h"
116(...)
117ZSTD_DCtx* context = ZSTD_createDCtx();
118size_t regeneratedSize = ZSTD_decompress_usingDict(context, dst, dstCapacity, cSrc, cSrcSize, dict, dictSize);
119```
Yann Collet45ff4302016-02-05 15:24:57 +0100120
121
122### Status
123
124Zstd has not yet reached "stable format" status. It doesn't guarantee yet that its current compression format will remain stable in future versions. During this period, it can still change to adapt new optimizations still being investigated. "Stable Format" is projected H1 2016, and will be tagged `v1.0`.
125
126That being said, the library is now fairly robust, able to withstand hazards situations, including invalid inputs. It also features legacy support, so that documents compressed with current and previous version of zstd can still be decoded in the future.
Yann Collet0a1740a2016-02-05 16:04:10 +0100127Library reliability has been tested using [Fuzz Testing](https://en.wikipedia.org/wiki/Fuzz_testing), with both [internal tools](programs/fuzzer.c) and [external ones](http://lcamtuf.coredump.cx/afl). Therefore, Zstandard is considered safe for testings, even within production environments.
Yann Collet45ff4302016-02-05 15:24:57 +0100128
129### Branch Policy
130
131The "dev" branch is the one where all contributions will be merged before reaching "master". If you plan to propose a patch, please commit into the "dev" branch or its own feature branch. Direct commit to "master" are not permitted.
132
133
134### Trivia
135
Yann Colletacd222c2015-11-06 12:39:39 +0100136Zstd entropy stage is provided by [Huff0 and FSE, from Finite State Entropy library](https://github.com/Cyan4973/FiniteStateEntropy).
Yann Colletf506c8b2015-01-24 10:31:47 +0100137
Yann Colletacd222c2015-11-06 12:39:39 +0100138Its memory requirement can be configured to fit into low-memory hardware configurations, or servers handling multiple connections/contexts in parallel.
Yann Colletdb9559f2015-08-24 00:14:40 +0100139