tree: 59271f998922bb0a2844a268db7c1b8255913ef5 [path history] [tgz]
  1. .travis.yml
  2. LICENSE
  3. README.md
  4. ZSTD_LICENSE
  5. bitstream.h
  6. compiler.h
  7. cover.c
  8. cpu.h
  9. divsufsort.c
  10. divsufsort.h
  11. entropy_common.c
  12. error_private.c
  13. error_private.h
  14. errors.go
  15. fse.h
  16. fse_compress.c
  17. fse_decompress.c
  18. huf.h
  19. huf_compress.c
  20. huf_decompress.c
  21. mem.h
  22. pool.c
  23. pool.h
  24. threading.c
  25. threading.h
  26. update.txt
  27. xxhash.c
  28. xxhash.h
  29. zbuff.h
  30. zbuff_common.c
  31. zbuff_compress.c
  32. zbuff_decompress.c
  33. zdict.c
  34. zdict.h
  35. zstd.go
  36. zstd.h
  37. zstd_common.c
  38. zstd_compress.c
  39. zstd_compress_internal.h
  40. zstd_decompress.c
  41. zstd_double_fast.c
  42. zstd_double_fast.h
  43. zstd_errors.h
  44. zstd_fast.c
  45. zstd_fast.h
  46. zstd_internal.h
  47. zstd_lazy.c
  48. zstd_lazy.h
  49. zstd_ldm.c
  50. zstd_ldm.h
  51. zstd_legacy.h
  52. zstd_opt.c
  53. zstd_opt.h
  54. zstd_stream.go
  55. zstd_v01.c
  56. zstd_v01.h
  57. zstd_v02.c
  58. zstd_v02.h
  59. zstd_v03.c
  60. zstd_v03.h
  61. zstd_v04.c
  62. zstd_v04.h
  63. zstd_v05.c
  64. zstd_v05.h
  65. zstd_v06.c
  66. zstd_v06.h
  67. zstd_v07.c
  68. zstd_v07.h
  69. zstdmt_compress.c
  70. zstdmt_compress.h
vendor/github.com/DataDog/zstd/README.md

Zstd Go Wrapper

C Zstd Homepage

The current headers and C files are from v1.3.4 (Commit 2555975).

Usage

There are two main APIs:

  • simple Compress/Decompress
  • streaming API (io.Reader/io.Writer)

The compress/decompress APIs mirror that of lz4, while the streaming API was designed to be a drop-in replacement for zlib.

Simple Compress/Decompress

// Compress compresses the byte array given in src and writes it to dst.
// If you already have a buffer allocated, you can pass it to prevent allocation
// If not, you can pass nil as dst.
// If the buffer is too small, it will be reallocated, resized, and returned bu the function
// If dst is nil, this will allocate the worst case size (CompressBound(src))
Compress(dst, src []byte) ([]byte, error)
// CompressLevel is the same as Compress but you can pass another compression level
CompressLevel(dst, src []byte, level int) ([]byte, error)
// Decompress will decompress your payload into dst.
// If you already have a buffer allocated, you can pass it to prevent allocation
// If not, you can pass nil as dst (allocates a 4*src size as default).
// If the buffer is too small, it will retry 3 times by doubling the dst size
// After max retries, it will switch to the slower stream API to be sure to be able
// to decompress. Currently switches if compression ratio > 4*2**3=32.
Decompress(dst, src []byte) ([]byte, error)

Stream API

// NewWriter creates a new object that can optionally be initialized with
// a precomputed dictionary. If dict is nil, compress without a dictionary.
// The dictionary array should not be changed during the use of this object.
// You MUST CALL Close() to write the last bytes of a zstd stream and free C objects.
NewWriter(w io.Writer) *Writer
NewWriterLevel(w io.Writer, level int) *Writer
NewWriterLevelDict(w io.Writer, level int, dict []byte) *Writer

// Write compresses the input data and write it to the underlying writer
(w *Writer) Write(p []byte) (int, error)

// Close flushes the buffer and frees C zstd objects
(w *Writer) Close() error
// NewReader returns a new io.ReadCloser that will decompress data from the
// underlying reader.  If a dictionary is provided to NewReaderDict, it must
// not be modified until Close is called.  It is the caller's responsibility
// to call Close, which frees up C objects.
NewReader(r io.Reader) io.ReadCloser
NewReaderDict(r io.Reader, dict []byte) io.ReadCloser

Benchmarks (benchmarked with v0.5.0)

The author of Zstd also wrote lz4. Zstd is intended to occupy a speed/ratio level similar to what zlib currently provides. In our tests, the can always be made to be better than zlib by chosing an appropriate level while still keeping compression and decompression time faster than zlib.

You can run the benchmarks against your own payloads by using the Go benchmarks tool. Just export your payload filepath as the PAYLOAD environment variable and run the benchmarks:

go test -bench .

Compression of a 7Mb pdf zstd (this wrapper) vs czlib:

BenchmarkCompression               5     221056624 ns/op      67.34 MB/s
BenchmarkDecompression           100      18370416 ns/op     810.32 MB/s

BenchmarkFzlibCompress             2     610156603 ns/op      24.40 MB/s
BenchmarkFzlibDecompress          20      81195246 ns/op     183.33 MB/s

Ratio is also better by a margin of ~20%. Compression speed is always better than zlib on all the payloads we tested; However, czlib has optimisations that make it faster at decompressiong small payloads:

Testing with size: 11... czlib: 8.97 MB/s, zstd: 3.26 MB/s
Testing with size: 27... czlib: 23.3 MB/s, zstd: 8.22 MB/s
Testing with size: 62... czlib: 31.6 MB/s, zstd: 19.49 MB/s
Testing with size: 141... czlib: 74.54 MB/s, zstd: 42.55 MB/s
Testing with size: 323... czlib: 155.14 MB/s, zstd: 99.39 MB/s
Testing with size: 739... czlib: 235.9 MB/s, zstd: 216.45 MB/s
Testing with size: 1689... czlib: 116.45 MB/s, zstd: 345.64 MB/s
Testing with size: 3858... czlib: 176.39 MB/s, zstd: 617.56 MB/s
Testing with size: 8811... czlib: 254.11 MB/s, zstd: 824.34 MB/s
Testing with size: 20121... czlib: 197.43 MB/s, zstd: 1339.11 MB/s
Testing with size: 45951... czlib: 201.62 MB/s, zstd: 1951.57 MB/s

zstd starts to shine with payloads > 1KB

Stability - Current state: STABLE

The C library seems to be pretty stable and according to the author has been tested and fuzzed.

For the Go wrapper, the test cover most usual cases and we have succesfully tested it on all staging and prod data.