Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from May 10, 2017
Merged

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

merged 1 commit into from May 10, 2017

Conversation

steeve
Copy link
Contributor

@steeve steeve commented May 3, 2017

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 for the benchmarks below:

      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] golang/go#6138
[2] golang/go@db12f9d

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If you signed the CLA as a corporation, please let us know the company's name.

@steeve
Copy link
Contributor Author

steeve commented May 3, 2017

I signed it!

@googlebot
Copy link

CLAs look good, thanks!

@steeve
Copy link
Contributor Author

steeve commented May 5, 2017

On a side note, this fixed the GC issues we had in production. Processes went from pretty much doing GC all the time back to a normal pattern.

Scheduling was also significantly improved as traces showed up to 800 runable goroutines all the time down to ~20 during GC, and 0 most of the time.

@dfawley dfawley self-requested a review May 5, 2017 20:25
@dfawley dfawley self-assigned this May 5, 2017
dfawley
dfawley previously approved these changes May 10, 2017
Copy link
Member

@dfawley dfawley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution.

LGTM

Copy link
Member

@dfawley dfawley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, I just noticed the test failure. TestCompress is instantiating its own compressor, so it should be changed to use NewGZIPCompressor() instead.

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] golang/go#6138
[2] golang/go@db12f9d

Signed-off-by: Steeve Morin <[email protected]>
@steeve
Copy link
Contributor Author

steeve commented May 10, 2017

Seems it was not pushed, fixed

Copy link
Member

@dfawley dfawley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

@dfawley dfawley merged commit 600406e into grpc:master May 10, 2017
@menghanl menghanl added 1.4 Type: Performance Performance improvements (CPU, network, memory, etc) labels Jun 7, 2017
@lock lock bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Type: Performance Performance improvements (CPU, network, memory, etc)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants