| 1 |
# zstd |
| 2 |
|
| 3 |
[Zstandard](https://facebook.github.io/zstd/) is a real-time compression algorithm, providing high compression ratios. |
| 4 |
It offers a very wide range of compression / speed trade-off, while being backed by a very fast decoder. |
| 5 |
A high performance compression algorithm is implemented. For now focused on speed. |
| 6 |
|
| 7 |
This package provides [compression](#Compressor) to and [decompression](#Decompressor) of Zstandard content. |
| 8 |
|
| 9 |
This package is pure Go. Use `noasm` and `nounsafe` to disable relevant features. |
| 10 |
|
| 11 |
The `zstd` package is provided as open source software using a Go standard license. |
| 12 |
|
| 13 |
Currently the package is heavily optimized for 64 bit processors and will be significantly slower on 32 bit processors. |
| 14 |
|
| 15 |
For seekable zstd streams, see [this excellent package](https://github.com/SaveTheRbtz/zstd-seekable-format-go). |
| 16 |
|
| 17 |
## Installation |
| 18 |
|
| 19 |
Install using `go get -u github.com/klauspost/compress`. The package is located in `github.com/klauspost/compress/zstd`. |
| 20 |
|
| 21 |
[](https://pkg.go.dev/github.com/klauspost/compress/zstd) |
| 22 |
|
| 23 |
## Compressor |
| 24 |
|
| 25 |
### Status: |
| 26 |
|
| 27 |
STABLE - there may always be subtle bugs, a wide variety of content has been tested and the library is actively |
| 28 |
used by several projects. This library is being [fuzz-tested](https://github.com/klauspost/compress-fuzz) for all updates. |
| 29 |
|
| 30 |
There may still be specific combinations of data types/size/settings that could lead to edge cases, |
| 31 |
so as always, testing is recommended. |
| 32 |
|
| 33 |
For now, a high speed (fastest) and medium-fast (default) compressor has been implemented. |
| 34 |
|
| 35 |
* The "Fastest" compression ratio is roughly equivalent to zstd level 1. |
| 36 |
* The "Default" compression ratio is roughly equivalent to zstd level 3 (default). |
| 37 |
* The "Better" compression ratio is roughly equivalent to zstd level 7. |
| 38 |
* The "Best" compression ratio is roughly equivalent to zstd level 11. |
| 39 |
|
| 40 |
In terms of speed, it is typically 2x as fast as the stdlib deflate/gzip in its fastest mode. |
| 41 |
The compression ratio compared to stdlib is around level 3, but usually 3x as fast. |
| 42 |
|
| 43 |
|
| 44 |
### Usage |
| 45 |
|
| 46 |
An Encoder can be used for either compressing a stream via the |
| 47 |
`io.WriteCloser` interface supported by the Encoder or as multiple independent |
| 48 |
tasks via the `EncodeAll` function. |
| 49 |
Smaller encodes are encouraged to use the EncodeAll function. |
| 50 |
Use `NewWriter` to create a new instance that can be used for both. |
| 51 |
|
| 52 |
To create a writer with default options, do like this: |
| 53 |
|
| 54 |
```Go |
| 55 |
// Compress input to output. |
| 56 |
func Compress(in io.Reader, out io.Writer) error { |
| 57 |
enc, err := zstd.NewWriter(out) |
| 58 |
if err != nil { |
| 59 |
return err |
| 60 |
} |
| 61 |
_, err = io.Copy(enc, in) |
| 62 |
if err != nil { |
| 63 |
enc.Close() |
| 64 |
return err |
| 65 |
} |
| 66 |
return enc.Close() |
| 67 |
} |
| 68 |
``` |
| 69 |
|
| 70 |
Now you can encode by writing data to `enc`. The output will be finished writing when `Close()` is called. |
| 71 |
Even if your encode fails, you should still call `Close()` to release any resources that may be held up. |
| 72 |
|
| 73 |
The above is fine for big encodes. However, whenever possible try to *reuse* the writer. |
| 74 |
|
| 75 |
To reuse the encoder, you can use the `Reset(io.Writer)` function to change to another output. |
| 76 |
This will allow the encoder to reuse all resources and avoid wasteful allocations. |
| 77 |
|
| 78 |
By default, stream encoding has 'light' concurrency, meaning up to 2 goroutines can be working on part |
| 79 |
of a stream. This is independent of the `WithEncoderConcurrency(n)`, but that is likely to change |
| 80 |
in the future. So if you want to limit concurrency for future updates, specify the concurrency |
| 81 |
you would like. |
| 82 |
|
| 83 |
If you would like stream encoding to be done without spawning async goroutines, use `WithEncoderConcurrency(1)` |
| 84 |
which will compress input as each block is completed, blocking on writes until each has completed. |
| 85 |
|
| 86 |
#### Parallel Stream Compression |
| 87 |
|
| 88 |
For maximum throughput on large streams, use `WithConcurrentBlocks(true)` together with |
| 89 |
`WithEncoderConcurrency(n)` where n is the number of CPU cores you want to use. |
| 90 |
This splits the input into large sections (jobs) that are compressed simultaneously by multiple goroutines, |
| 91 |
similar to how the C zstd library does multithreaded compression. |
| 92 |
|
| 93 |
```Go |
| 94 |
enc, err := zstd.NewWriter(out, |
| 95 |
zstd.WithEncoderLevel(zstd.SpeedDefault), |
| 96 |
zstd.WithEncoderConcurrency(runtime.GOMAXPROCS(0)), |
| 97 |
zstd.WithConcurrentBlocks(true), |
| 98 |
) |
| 99 |
``` |
| 100 |
|
| 101 |
Each non-first job receives an overlap prefix from the previous job for match context, |
| 102 |
so compression ratio is only marginally affected. Output is flushed in order, |
| 103 |
producing a valid single-frame zstd stream. |
| 104 |
|
| 105 |
Benchmark on 1.8GB GOB stream (AMD Ryzen 9 9950X): |
| 106 |
|
| 107 |
| Level | 1 thread | 4 threads | 16 threads | 1T ratio | 16T ratio | |
| 108 |
|---------|:----------:|:------------------:|:-------------------:|:--------:|:---------:| |
| 109 |
| fastest | 783 MB/s | 2950 MB/s (3.8×) | 6939 MB/s (8.9×) | 12.24% | 12.26% | |
| 110 |
| default | 728 MB/s | 2533 MB/s (3.5×) | 5340 MB/s (7.3×) | 10.67% | 10.68% | |
| 111 |
| better | 434 MB/s | 1105 MB/s (2.5×) | 2206 MB/s (5.1×) | 9.14% | 9.21% | |
| 112 |
| best | 129 MB/s | 367 MB/s (2.8×) | 884 MB/s (6.8×) | 8.48% | 8.63% | |
| 113 |
|
| 114 |
Notes: |
| 115 |
* Not compatible with dictionary encoding. |
| 116 |
* `Flush()` dispatches the current partial job, so latency-sensitive callers can force output. |
| 117 |
* `EncodeAll` is unaffected — it uses its own concurrency via the encoder pool. |
| 118 |
|
| 119 |
You can specify your desired compression level using `WithEncoderLevel()` option. Currently only pre-defined |
| 120 |
compression settings can be specified. |
| 121 |
|
| 122 |
#### Future Compatibility Guarantees |
| 123 |
|
| 124 |
This will be an evolving project. When using this package it is important to note that both the compression efficiency and speed may change. |
| 125 |
|
| 126 |
The goal will be to keep the default efficiency at the default zstd (level 3). |
| 127 |
However the encoding should never be assumed to remain the same, |
| 128 |
and you should not use hashes of compressed output for similarity checks. |
| 129 |
|
| 130 |
The Encoder can be assumed to produce the same output from the exact same code version. |
| 131 |
However, the may be modes in the future that break this, |
| 132 |
although they will not be enabled without an explicit option. |
| 133 |
|
| 134 |
This encoder is not designed to (and will probably never) output the exact same bitstream as the reference encoder. |
| 135 |
|
| 136 |
Also note, that the cgo decompressor currently does not [report all errors on invalid input](https://github.com/DataDog/zstd/issues/59), |
| 137 |
[omits error checks](https://github.com/DataDog/zstd/issues/61), [ignores checksums](https://github.com/DataDog/zstd/issues/43) |
| 138 |
and seems to ignore concatenated streams, even though [it is part of the spec](https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md#frames). |
| 139 |
|
| 140 |
#### Blocks |
| 141 |
|
| 142 |
For compressing small blocks, the returned encoder has a function called `EncodeAll(src, dst []byte) []byte`. |
| 143 |
|
| 144 |
`EncodeAll` will encode all input in src and append it to dst. |
| 145 |
This function can be called concurrently. |
| 146 |
Each call will only run on a same goroutine as the caller. |
| 147 |
|
| 148 |
Encoded blocks can be concatenated and the result will be the combined input stream. |
| 149 |
Data compressed with EncodeAll can be decoded with the Decoder, using either a stream or `DecodeAll`. |
| 150 |
|
| 151 |
Especially when encoding blocks you should take special care to reuse the encoder. |
| 152 |
This will effectively make it run without allocations after a warmup period. |
| 153 |
To make it run completely without allocations, supply a destination buffer with space for all content. |
| 154 |
|
| 155 |
```Go |
| 156 |
import "github.com/klauspost/compress/zstd" |
| 157 |
|
| 158 |
// Create a writer that caches compressors. |
| 159 |
// For this operation type we supply a nil Reader. |
| 160 |
var encoder, _ = zstd.NewWriter(nil) |
| 161 |
|
| 162 |
// Compress a buffer. |
| 163 |
// If you have a destination buffer, the allocation in the call can also be eliminated. |
| 164 |
func Compress(src []byte) []byte { |
| 165 |
return encoder.EncodeAll(src, make([]byte, 0, len(src))) |
| 166 |
} |
| 167 |
``` |
| 168 |
|
| 169 |
You can control the maximum number of concurrent encodes using the `WithEncoderConcurrency(n)` |
| 170 |
option when creating the writer. |
| 171 |
|
| 172 |
Using the Encoder for both a stream and individual blocks concurrently is safe. |
| 173 |
|
| 174 |
### Performance |
| 175 |
|
| 176 |
I have collected some speed examples to compare speed and compression against other compressors. |
| 177 |
|
| 178 |
* `file` is the input file. |
| 179 |
* `out` is the compressor used. `zskp` is this package. `zstd` is the Datadog cgo library. `gzstd/gzkp` is gzip standard and this library. |
| 180 |
* `level` is the compression level used. For `zskp` level 1 is "fastest", level 2 is "default"; 3 is "better", 4 is "best". |
| 181 |
* `insize`/`outsize` is the input/output size. |
| 182 |
* `millis` is the number of milliseconds used for compression. |
| 183 |
* `mb/s` is megabytes (2^20 bytes) per second. |
| 184 |
|
| 185 |
``` |
| 186 |
Silesia Corpus: |
| 187 |
http://sun.aei.polsl.pl/~sdeor/corpus/silesia.zip |
| 188 |
|
| 189 |
This package: |
| 190 |
file out level insize outsize millis mb/s |
| 191 |
silesia.tar zskp 1 211947520 73821326 634 318.47 |
| 192 |
silesia.tar zskp 2 211947520 67655404 1508 133.96 |
| 193 |
silesia.tar zskp 3 211947520 64746933 3000 67.37 |
| 194 |
silesia.tar zskp 4 211947520 60073508 16926 11.94 |
| 195 |
|
| 196 |
cgo zstd: |
| 197 |
silesia.tar zstd 1 211947520 73605392 543 371.56 |
| 198 |
silesia.tar zstd 3 211947520 66793289 864 233.68 |
| 199 |
silesia.tar zstd 6 211947520 62916450 1913 105.66 |
| 200 |
silesia.tar zstd 9 211947520 60212393 5063 39.92 |
| 201 |
|
| 202 |
gzip, stdlib/this package: |
| 203 |
silesia.tar gzstd 1 211947520 80007735 1498 134.87 |
| 204 |
silesia.tar gzkp 1 211947520 80088272 1009 200.31 |
| 205 |
|
| 206 |
GOB stream of binary data. Highly compressible. |
| 207 |
https://files.klauspost.com/compress/gob-stream.7z |
| 208 |
|
| 209 |
file out level insize outsize millis mb/s |
| 210 |
gob-stream zskp 1 1911399616 233948096 3230 564.34 |
| 211 |
gob-stream zskp 2 1911399616 203997694 4997 364.73 |
| 212 |
gob-stream zskp 3 1911399616 173526523 13435 135.68 |
| 213 |
gob-stream zskp 4 1911399616 162195235 47559 38.33 |
| 214 |
|
| 215 |
gob-stream zstd 1 1911399616 249810424 2637 691.26 |
| 216 |
gob-stream zstd 3 1911399616 208192146 3490 522.31 |
| 217 |
gob-stream zstd 6 1911399616 193632038 6687 272.56 |
| 218 |
gob-stream zstd 9 1911399616 177620386 16175 112.70 |
| 219 |
|
| 220 |
gob-stream gzstd 1 1911399616 357382013 9046 201.49 |
| 221 |
gob-stream gzkp 1 1911399616 359136669 4885 373.08 |
| 222 |
|
| 223 |
The test data for the Large Text Compression Benchmark is the first |
| 224 |
10^9 bytes of the English Wikipedia dump on Mar. 3, 2006. |
| 225 |
http://mattmahoney.net/dc/textdata.html |
| 226 |
|
| 227 |
file out level insize outsize millis mb/s |
| 228 |
enwik9 zskp 1 1000000000 343833605 3687 258.64 |
| 229 |
enwik9 zskp 2 1000000000 317001237 7672 124.29 |
| 230 |
enwik9 zskp 3 1000000000 291915823 15923 59.89 |
| 231 |
enwik9 zskp 4 1000000000 261710291 77697 12.27 |
| 232 |
|
| 233 |
enwik9 zstd 1 1000000000 358072021 3110 306.65 |
| 234 |
enwik9 zstd 3 1000000000 313734672 4784 199.35 |
| 235 |
enwik9 zstd 6 1000000000 295138875 10290 92.68 |
| 236 |
enwik9 zstd 9 1000000000 278348700 28549 33.40 |
| 237 |
|
| 238 |
enwik9 gzstd 1 1000000000 382578136 8608 110.78 |
| 239 |
enwik9 gzkp 1 1000000000 382781160 5628 169.45 |
| 240 |
|
| 241 |
Highly compressible JSON file. |
| 242 |
https://files.klauspost.com/compress/github-june-2days-2019.json.zst |
| 243 |
|
| 244 |
file out level insize outsize millis mb/s |
| 245 |
github-june-2days-2019.json zskp 1 6273951764 697439532 9789 611.17 |
| 246 |
github-june-2days-2019.json zskp 2 6273951764 610876538 18553 322.49 |
| 247 |
github-june-2days-2019.json zskp 3 6273951764 517662858 44186 135.41 |
| 248 |
github-june-2days-2019.json zskp 4 6273951764 464617114 165373 36.18 |
| 249 |
|
| 250 |
github-june-2days-2019.json zstd 1 6273951764 766284037 8450 708.00 |
| 251 |
github-june-2days-2019.json zstd 3 6273951764 661889476 10927 547.57 |
| 252 |
github-june-2days-2019.json zstd 6 6273951764 642756859 22996 260.18 |
| 253 |
github-june-2days-2019.json zstd 9 6273951764 601974523 52413 114.16 |
| 254 |
|
| 255 |
github-june-2days-2019.json gzstd 1 6273951764 1164397768 26793 223.32 |
| 256 |
github-june-2days-2019.json gzkp 1 6273951764 1120631856 17693 338.16 |
| 257 |
|
| 258 |
VM Image, Linux mint with a few installed applications: |
| 259 |
https://files.klauspost.com/compress/rawstudio-mint14.7z |
| 260 |
|
| 261 |
file out level insize outsize millis mb/s |
| 262 |
rawstudio-mint14.tar zskp 1 8558382592 3718400221 18206 448.29 |
| 263 |
rawstudio-mint14.tar zskp 2 8558382592 3326118337 37074 220.15 |
| 264 |
rawstudio-mint14.tar zskp 3 8558382592 3163842361 87306 93.49 |
| 265 |
rawstudio-mint14.tar zskp 4 8558382592 2970480650 783862 10.41 |
| 266 |
|
| 267 |
rawstudio-mint14.tar zstd 1 8558382592 3609250104 17136 476.27 |
| 268 |
rawstudio-mint14.tar zstd 3 8558382592 3341679997 29262 278.92 |
| 269 |
rawstudio-mint14.tar zstd 6 8558382592 3235846406 77904 104.77 |
| 270 |
rawstudio-mint14.tar zstd 9 8558382592 3160778861 140946 57.91 |
| 271 |
|
| 272 |
rawstudio-mint14.tar gzstd 1 8558382592 3926234992 51345 158.96 |
| 273 |
rawstudio-mint14.tar gzkp 1 8558382592 3960117298 36722 222.26 |
| 274 |
|
| 275 |
CSV data: |
| 276 |
https://files.klauspost.com/compress/nyc-taxi-data-10M.csv.zst |
| 277 |
|
| 278 |
file out level insize outsize millis mb/s |
| 279 |
nyc-taxi-data-10M.csv zskp 1 3325605752 641319332 9462 335.17 |
| 280 |
nyc-taxi-data-10M.csv zskp 2 3325605752 588976126 17570 180.50 |
| 281 |
nyc-taxi-data-10M.csv zskp 3 3325605752 529329260 32432 97.79 |
| 282 |
nyc-taxi-data-10M.csv zskp 4 3325605752 474949772 138025 22.98 |
| 283 |
|
| 284 |
nyc-taxi-data-10M.csv zstd 1 3325605752 687399637 8233 385.18 |
| 285 |
nyc-taxi-data-10M.csv zstd 3 3325605752 598514411 10065 315.07 |
| 286 |
nyc-taxi-data-10M.csv zstd 6 3325605752 570522953 20038 158.27 |
| 287 |
nyc-taxi-data-10M.csv zstd 9 3325605752 517554797 64565 49.12 |
| 288 |
|
| 289 |
nyc-taxi-data-10M.csv gzstd 1 3325605752 928654908 21270 149.11 |
| 290 |
nyc-taxi-data-10M.csv gzkp 1 3325605752 922273214 13929 227.68 |
| 291 |
``` |
| 292 |
|
| 293 |
## Decompressor |
| 294 |
|
| 295 |
Status: STABLE - there may still be subtle bugs, but a wide variety of content has been tested. |
| 296 |
|
| 297 |
This library is being continuously [fuzz-tested](https://github.com/klauspost/compress-fuzz), |
| 298 |
kindly supplied by [fuzzit.dev](https://fuzzit.dev/). |
| 299 |
The main purpose of the fuzz testing is to ensure that it is not possible to crash the decoder, |
| 300 |
or run it past its limits with ANY input provided. |
| 301 |
|
| 302 |
### Usage |
| 303 |
|
| 304 |
The package has been designed for two main usages, big streams of data and smaller in-memory buffers. |
| 305 |
There are two main usages of the package for these. Both of them are accessed by creating a `Decoder`. |
| 306 |
|
| 307 |
For streaming use a simple setup could look like this: |
| 308 |
|
| 309 |
```Go |
| 310 |
import "github.com/klauspost/compress/zstd" |
| 311 |
|
| 312 |
func Decompress(in io.Reader, out io.Writer) error { |
| 313 |
d, err := zstd.NewReader(in) |
| 314 |
if err != nil { |
| 315 |
return err |
| 316 |
} |
| 317 |
defer d.Close() |
| 318 |
|
| 319 |
// Copy content... |
| 320 |
_, err = io.Copy(out, d) |
| 321 |
return err |
| 322 |
} |
| 323 |
``` |
| 324 |
|
| 325 |
It is important to use the "Close" function when you no longer need the Reader to stop running goroutines, |
| 326 |
when running with default settings. |
| 327 |
Goroutines will exit once an error has been returned, including `io.EOF` at the end of a stream. |
| 328 |
|
| 329 |
Streams are decoded concurrently in 4 asynchronous stages to give the best possible throughput. |
| 330 |
However, if you prefer synchronous decompression, use `WithDecoderConcurrency(1)` which will decompress data |
| 331 |
as it is being requested only. |
| 332 |
|
| 333 |
For decoding buffers, it could look something like this: |
| 334 |
|
| 335 |
```Go |
| 336 |
import "github.com/klauspost/compress/zstd" |
| 337 |
|
| 338 |
// Create a reader that caches decompressors. |
| 339 |
// For this operation type we supply a nil Reader. |
| 340 |
var decoder, _ = zstd.NewReader(nil, zstd.WithDecoderConcurrency(0)) |
| 341 |
|
| 342 |
// Decompress a buffer. We don't supply a destination buffer, |
| 343 |
// so it will be allocated by the decoder. |
| 344 |
func Decompress(src []byte) ([]byte, error) { |
| 345 |
return decoder.DecodeAll(src, nil) |
| 346 |
} |
| 347 |
``` |
| 348 |
|
| 349 |
Both of these cases should provide the functionality needed. |
| 350 |
The decoder can be used for *concurrent* decompression of multiple buffers. |
| 351 |
By default 4 decompressors will be created. |
| 352 |
|
| 353 |
It will only allow a certain number of concurrent operations to run. |
| 354 |
To tweak that yourself use the `WithDecoderConcurrency(n)` option when creating the decoder. |
| 355 |
It is possible to use `WithDecoderConcurrency(0)` to create GOMAXPROCS decoders. |
| 356 |
|
| 357 |
### Dictionaries |
| 358 |
|
| 359 |
Data compressed with [dictionaries](https://github.com/facebook/zstd#the-case-for-small-data-compression) can be decompressed. |
| 360 |
|
| 361 |
Dictionaries are added individually to Decoders. |
| 362 |
Dictionaries are generated by the `zstd --train` command and contains an initial state for the decoder. |
| 363 |
To add a dictionary use the `WithDecoderDicts(dicts ...[]byte)` option with the dictionary data. |
| 364 |
Several dictionaries can be added at once. |
| 365 |
|
| 366 |
The dictionary will be used automatically for the data that specifies them. |
| 367 |
A re-used Decoder will still contain the dictionaries registered. |
| 368 |
|
| 369 |
When registering multiple dictionaries with the same ID, the last one will be used. |
| 370 |
|
| 371 |
It is possible to use dictionaries when compressing data. |
| 372 |
|
| 373 |
To enable a dictionary use `WithEncoderDict(dict []byte)`. Here only one dictionary will be used |
| 374 |
and it will likely be used even if it doesn't improve compression. |
| 375 |
|
| 376 |
The used dictionary must be used to decompress the content. |
| 377 |
|
| 378 |
For any real gains, the dictionary should be built with similar data. |
| 379 |
If an unsuitable dictionary is used the output may be slightly larger than using no dictionary. |
| 380 |
Use the [zstd commandline tool](https://github.com/facebook/zstd/releases) to build a dictionary from sample data. |
| 381 |
For information see [zstd dictionary information](https://github.com/facebook/zstd#the-case-for-small-data-compression). |
| 382 |
|
| 383 |
For now there is a fixed startup performance penalty for compressing content with dictionaries. |
| 384 |
This will likely be improved over time. Just be aware to test performance when implementing. |
| 385 |
|
| 386 |
### Allocation-less operation |
| 387 |
|
| 388 |
The decoder has been designed to operate without allocations after a warmup. |
| 389 |
|
| 390 |
This means that you should *store* the decoder for best performance. |
| 391 |
To re-use a stream decoder, use the `Reset(r io.Reader) error` to switch to another stream. |
| 392 |
A decoder can safely be re-used even if the previous stream failed. |
| 393 |
|
| 394 |
To release the resources, you must call the `Close()` function on a decoder. |
| 395 |
After this it can *no longer be reused*, but all running goroutines will be stopped. |
| 396 |
So you *must* use this if you will no longer need the Reader. |
| 397 |
|
| 398 |
For decompressing smaller buffers a single decoder can be used. |
| 399 |
When decoding buffers, you can supply a destination slice with length 0 and your expected capacity. |
| 400 |
In this case no unneeded allocations should be made. |
| 401 |
|
| 402 |
### Concurrency |
| 403 |
|
| 404 |
The buffer decoder does everything on the same goroutine and does nothing concurrently. |
| 405 |
It can however decode several buffers concurrently. Use `WithDecoderConcurrency(n)` to limit that. |
| 406 |
|
| 407 |
The stream decoder will create goroutines that: |
| 408 |
|
| 409 |
1) Reads input and splits the input into blocks. |
| 410 |
2) Decompression of literals. |
| 411 |
3) Decompression of sequences. |
| 412 |
4) Reconstruction of output stream. |
| 413 |
|
| 414 |
So effectively this also means the decoder will "read ahead" and prepare data to always be available for output. |
| 415 |
|
| 416 |
The concurrency level will, for streams, determine how many blocks ahead the compression will start. |
| 417 |
|
| 418 |
Since "blocks" are quite dependent on the output of the previous block stream decoding will only have limited concurrency. |
| 419 |
|
| 420 |
In practice this means that concurrency is often limited to utilizing about 3 cores effectively. |
| 421 |
|
| 422 |
### Benchmarks |
| 423 |
|
| 424 |
The first two are streaming decodes and the last are smaller inputs. |
| 425 |
|
| 426 |
Running on AMD Ryzen 9 3950X 16-Core Processor. AMD64 assembly used. |
| 427 |
|
| 428 |
``` |
| 429 |
BenchmarkDecoderSilesia-32 5 206878840 ns/op 1024.50 MB/s 49808 B/op 43 allocs/op |
| 430 |
BenchmarkDecoderEnwik9-32 1 1271809000 ns/op 786.28 MB/s 72048 B/op 52 allocs/op |
| 431 |
|
| 432 |
Concurrent blocks, performance: |
| 433 |
|
| 434 |
BenchmarkDecoder_DecodeAllParallel/kppkn.gtb.zst-32 67356 17857 ns/op 10321.96 MB/s 22.48 pct 102 B/op 0 allocs/op |
| 435 |
BenchmarkDecoder_DecodeAllParallel/geo.protodata.zst-32 266656 4421 ns/op 26823.21 MB/s 11.89 pct 19 B/op 0 allocs/op |
| 436 |
BenchmarkDecoder_DecodeAllParallel/plrabn12.txt.zst-32 20992 56842 ns/op 8477.17 MB/s 39.90 pct 754 B/op 0 allocs/op |
| 437 |
BenchmarkDecoder_DecodeAllParallel/lcet10.txt.zst-32 27456 43932 ns/op 9714.01 MB/s 33.27 pct 524 B/op 0 allocs/op |
| 438 |
BenchmarkDecoder_DecodeAllParallel/asyoulik.txt.zst-32 78432 15047 ns/op 8319.15 MB/s 40.34 pct 66 B/op 0 allocs/op |
| 439 |
BenchmarkDecoder_DecodeAllParallel/alice29.txt.zst-32 65800 18436 ns/op 8249.63 MB/s 37.75 pct 88 B/op 0 allocs/op |
| 440 |
BenchmarkDecoder_DecodeAllParallel/html_x_4.zst-32 102993 11523 ns/op 35546.09 MB/s 3.637 pct 143 B/op 0 allocs/op |
| 441 |
BenchmarkDecoder_DecodeAllParallel/paper-100k.pdf.zst-32 1000000 1070 ns/op 95720.98 MB/s 80.53 pct 3 B/op 0 allocs/op |
| 442 |
BenchmarkDecoder_DecodeAllParallel/fireworks.jpeg.zst-32 749802 1752 ns/op 70272.35 MB/s 100.0 pct 5 B/op 0 allocs/op |
| 443 |
BenchmarkDecoder_DecodeAllParallel/urls.10K.zst-32 22640 52934 ns/op 13263.37 MB/s 26.25 pct 1014 B/op 0 allocs/op |
| 444 |
BenchmarkDecoder_DecodeAllParallel/html.zst-32 226412 5232 ns/op 19572.27 MB/s 14.49 pct 20 B/op 0 allocs/op |
| 445 |
BenchmarkDecoder_DecodeAllParallel/comp-data.bin.zst-32 923041 1276 ns/op 3194.71 MB/s 31.26 pct 0 B/op 0 allocs/op |
| 446 |
``` |
| 447 |
|
| 448 |
This reflects the performance around May 2022, but this may be out of date. |
| 449 |
|
| 450 |
## Zstd inside ZIP files |
| 451 |
|
| 452 |
It is possible to use zstandard to compress individual files inside zip archives. |
| 453 |
While this isn't widely supported it can be useful for internal files. |
| 454 |
|
| 455 |
To support the compression and decompression of these files you must register a compressor and decompressor. |
| 456 |
|
| 457 |
It is highly recommended registering the (de)compressors on individual zip Reader/Writer and NOT |
| 458 |
use the global registration functions. The main reason for this is that 2 registrations from |
| 459 |
different packages will result in a panic. |
| 460 |
|
| 461 |
It is a good idea to only have a single compressor and decompressor, since they can be used for multiple zip |
| 462 |
files concurrently, and using a single instance will allow reusing some resources. |
| 463 |
|
| 464 |
See [this example](https://pkg.go.dev/github.com/klauspost/compress/zstd#example-ZipCompressor) for |
| 465 |
how to compress and decompress files inside zip archives. |
| 466 |
|
| 467 |
# Contributions |
| 468 |
|
| 469 |
Contributions are always welcome. |
| 470 |
For new features/fixes, remember to add tests and for performance enhancements include benchmarks. |
| 471 |
|
| 472 |
For general feedback and experience reports, feel free to open an issue or write me on [Twitter](https://twitter.com/sh0dan). |
| 473 |
|
| 474 |
This package includes the excellent [`github.com/cespare/xxhash`](https://github.com/cespare/xxhash) package Copyright (c) 2016 Caleb Spare. |