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
net/http: pool GZIP readers to amortise initialisation costs #46181
Comments
This is not an API change so it doesn't have to go through the approval process. This is an optimization suggestion. |
Do you have any data to show the improvement? |
SGTM. I thought we actually already did this. :) |
We pool the connections in HTTP, but not any of the GZIP-level components. Side note, never add issues before coffee. It looks like only the HTTP clients from stdlib are impacted here if we change in net/http. net/http's server never encodes responses as GZIP unless done in application code. My bad |
It's employer code atm, working on getting the OK to work on outside OS work. Benchmark is as follows:
The speed benchmark likely isn't accurate, as my laptop is currently busy doing a bunch of other stuff. Benchmarked solution had to also do a bunch of work cloning the request that stdlib doesn't, so there is some overhead there to lose. |
Interesting. I did a simple benchmark. |
In the grand scheme of things, we are only talking about an allocation of roughly what, 50kb? Once you're measuring body sizes of ~1mb, that may as well be a rounding error Edit: thinking about it more, I'd expect a variance of about ~5% per request that uses a pooled reader. Is this (roughly) what you found? |
These are 50k, 100k, and 1024k files.
|
I ran into the same issue when optimizing seaweedfs, pooling makes a lot difference for sub 100k contents. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What did you do?
Ran a HTTP server that returns a mix of small and large responses.
What did you expect to see?
Less of a memory spike when returning results
What did you see instead?
The GZIP Reader and Writer taking up ~50% of the total allocations for my server.
I propose we use a
sync.Pool
(or similar) to pool the GZIP readers and writers in the underlying HTTP client and server. This should be able to reduce GC churn and make memory allocations for clients and servers more in line with the size of the body associated with the request.Alternatively, we could do this at the
flate
layer and bring the reduction of GC churn to anything that uses Huffman Encoding.Happy to submit a PR for this. It's burnt me enough to justify just fixing it once upstream rather than re-fixing it in every HTTP service I review or write.
The text was updated successfully, but these errors were encountered: