zstd.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Package smallzstd produces zstd encoders and decoders optimized for
  4. // low memory usage, at the expense of compression efficiency.
  5. //
  6. // This package is optimized primarily for the memory cost of
  7. // compressing and decompressing data. We reduce this cost in two
  8. // major ways: disable parallelism within the library (i.e. don't use
  9. // multiple CPU cores to decompress), and drop the compression window
  10. // down from the defaults of 4-16MiB, to 8kiB.
  11. //
  12. // Decompressors cost 2x the window size in RAM to run, so by using an
  13. // 8kiB window, we can run ~1000 more decompressors per unit of memory
  14. // than with the defaults.
  15. //
  16. // Depending on context, the benefit is either being able to run more
  17. // decoders (e.g. in our logs processing system), or having a lower
  18. // memory footprint when using compression in network protocols
  19. // (e.g. in tailscaled, which should have a minimal RAM cost).
  20. package smallzstd
  21. import (
  22. "io"
  23. "github.com/klauspost/compress/zstd"
  24. )
  25. // WindowSize is the window size used for zstd compression. Decoder
  26. // memory usage scales linearly with WindowSize.
  27. const WindowSize = 8 << 10 // 8kiB
  28. // NewDecoder returns a zstd.Decoder configured for low memory usage,
  29. // at the expense of decompression performance.
  30. func NewDecoder(r io.Reader, options ...zstd.DOption) (*zstd.Decoder, error) {
  31. defaults := []zstd.DOption{
  32. // Default is GOMAXPROCS, which costs many KiB in stacks.
  33. zstd.WithDecoderConcurrency(1),
  34. // Default is to allocate more upfront for performance. We
  35. // prefer lower memory use and a bit of GC load.
  36. zstd.WithDecoderLowmem(true),
  37. // You might expect to see zstd.WithDecoderMaxMemory
  38. // here. However, it's not terribly safe to use if you're
  39. // doing stateless decoding, because it sets the maximum
  40. // amount of memory the decompressed data can occupy, rather
  41. // than the window size of the zstd stream. This means a very
  42. // compressible piece of data might violate the max memory
  43. // limit here, even if the window size (and thus total memory
  44. // required to decompress the data) is small.
  45. //
  46. // As a result, we don't set a decoder limit here, and rely on
  47. // the encoder below producing "cheap" streams. Callers are
  48. // welcome to set their own max memory setting, if
  49. // contextually there is a clearly correct value (e.g. it's
  50. // known from the upper layer protocol that the decoded data
  51. // can never be more than 1MiB).
  52. }
  53. return zstd.NewReader(r, append(defaults, options...)...)
  54. }
  55. // NewEncoder returns a zstd.Encoder configured for low memory usage,
  56. // both during compression and at decompression time, at the expense
  57. // of performance and compression efficiency.
  58. func NewEncoder(w io.Writer, options ...zstd.EOption) (*zstd.Encoder, error) {
  59. defaults := []zstd.EOption{
  60. // Default is GOMAXPROCS, which costs many KiB in stacks.
  61. zstd.WithEncoderConcurrency(1),
  62. // Default is several MiB, which bloats both encoders and
  63. // their corresponding decoders.
  64. zstd.WithWindowSize(WindowSize),
  65. // Encode zero-length inputs in a way that the `zstd` utility
  66. // can read, because interoperability is handy.
  67. zstd.WithZeroFrames(true),
  68. }
  69. return zstd.NewWriter(w, append(defaults, options...)...)
  70. }