Use pooled gzip.{Writer,Reader} in gzip{Compressor,Decompressor} (#1217)

This change saves a lot of memory by reusing the underlying
gzip.{Writer,Reader}, which allocates up to 1.4mb at every instanciation
according to [1]. This was fixed by adding a Reset method by to the
object at [2].

The amount of memory (and GC time) saved is pretty high, as reported by
pprof:

      flat  flat%   sum%        cum   cum%
   28.33GB 85.70% 85.70%    32.74GB 99.05%  compress/flate.NewWriter

      flat  flat%   sum%        cum   cum%
   19.39MB 16.74% 16.74%    22.07MB 19.05%  compress/flate.NewWriter

And the benchmarks:

benchmark                           old ns/op     new ns/op     delta
BenchmarkGZIPCompressor1B-4         215170        22291         -89.64%
BenchmarkGZIPCompressor1KiB-4       225971        27213         -87.96%
BenchmarkGZIPCompressor8KiB-4       246696        54785         -77.79%
BenchmarkGZIPCompressor64KiB-4      444851        286924        -35.50%
BenchmarkGZIPCompressor512KiB-4     2279043       2115863       -7.16%
BenchmarkGZIPCompressor1MiB-4       4412989       4258635       -3.50%

benchmark                           old allocs     new allocs     delta
BenchmarkGZIPCompressor1B-4         17             0              -100.00%
BenchmarkGZIPCompressor1KiB-4       17             0              -100.00%
BenchmarkGZIPCompressor8KiB-4       17             0              -100.00%
BenchmarkGZIPCompressor64KiB-4      17             0              -100.00%
BenchmarkGZIPCompressor512KiB-4     17             0              -100.00%
BenchmarkGZIPCompressor1MiB-4       17             0              -100.00%

benchmark                           old bytes     new bytes     delta
BenchmarkGZIPCompressor1B-4         813872        8             -100.00%
BenchmarkGZIPCompressor1KiB-4       813872        16            -100.00%
BenchmarkGZIPCompressor8KiB-4       813875        27            -100.00%
BenchmarkGZIPCompressor64KiB-4      813918        190           -99.98%
BenchmarkGZIPCompressor512KiB-4     814928        1871          -99.77%
BenchmarkGZIPCompressor1MiB-4       820889        9735          -98.81%

[1] https://github.com/golang/go/issues/6138
[2] https://github.com/golang/go/commit/db12f9d4e406dcab81b476e955c8e119112522fa

Signed-off-by: Steeve Morin <steeve.morin@gmail.com>
2 files changed
tree: d01482e9433bd065a36afc7f97756c8249184c0e
  1. benchmark/
  2. codes/
  3. credentials/
  4. Documentation/
  5. examples/
  6. grpclb/
  7. grpclog/
  8. health/
  9. internal/
  10. interop/
  11. keepalive/
  12. metadata/
  13. naming/
  14. peer/
  15. reflection/
  16. stats/
  17. status/
  18. stress/
  19. tap/
  20. test/
  21. testdata/
  22. transport/
  23. .travis.yml
  24. backoff.go
  25. backoff_test.go
  26. balancer.go
  27. balancer_test.go
  28. call.go
  29. call_test.go
  30. clientconn.go
  31. clientconn_test.go
  32. codec.go
  33. codec_benchmark_test.go
  34. codec_test.go
  35. codegen.sh
  36. CONTRIBUTING.md
  37. coverage.sh
  38. doc.go
  39. go16.go
  40. go17.go
  41. grpclb.go
  42. interceptor.go
  43. LICENSE
  44. Makefile
  45. PATENTS
  46. proxy.go
  47. proxy_test.go
  48. README.md
  49. rpc_util.go
  50. rpc_util_test.go
  51. server.go
  52. server_test.go
  53. stream.go
  54. trace.go
README.md

gRPC-Go

Build Status GoDoc

The Go implementation of gRPC: A high performance, open source, general RPC framework that puts mobile and HTTP/2 first. For more information see the gRPC Quick Start guide.

Installation

To install this package, you need to install Go and setup your Go workspace on your computer. The simplest way to install the library is to run:

$ go get google.golang.org/grpc

Prerequisites

This requires Go 1.6 or later.

Constraints

The grpc package should only depend on standard Go packages and a small number of exceptions. If your contribution introduces new dependencies which are NOT in the list, you need a discussion with gRPC-Go authors and consultants.

Documentation

See API documentation for package and API descriptions and find examples in the examples directory.

Status

GA

FAQ

Compiling error, undefined: grpc.SupportPackageIsVersion

Please update proto package, gRPC package and rebuild the proto files:

  • go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
  • go get -u google.golang.org/grpc
  • protoc --go_out=plugins=grpc:. *.proto