-
Notifications
You must be signed in to change notification settings - Fork 18k
io: consider reusing buffers for io.Copy to reduce GC pressure #12450
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
Comments
Go 1.5 introduced io.CopyBuffer to let you reuse buffers. @bradfitz probably remembers why sync.Pool didn't work here. |
@crawshaw io.Copy is used at least in net/http internals — I'm mostly interested in cases where one cannot replace io.Copy with io.CopyBuffer without forking part of stdlib (or building from modified goroot). See for example:
at least some of those cases falls to io.copyBuffer(dst, src, nil) when neither io.WriterTo nor io.ReaderFrom optimizations apply (which allocates non-reusable []byte per function call). |
It looks like at least some of those uses of io.Copy in net/http could be replaced by io.CopyBuffer, reusing a []byte held by the objects in http. Want to give it a shot? |
I can work on replacing some of io.Copy calls with io.CopyBuffer in net/http, but would like to have @bradfitz to comment on this first. I think now that changing net/http would make more sense than modifying io.Copy, as net/http is the most heavy user of io.Copy throughout stdlib. |
See old discussion in #5509 and the patches it references. |
@bradfitz that covers the first part of this, thanks. What about the later question, replacing some uses of io.Copy in net/http with io.CopyBuffer? |
Closing this bug as a dup of #5509. Please open a separate bug (or just send CLs) for net/http performance improvements, which I can review. |
I wonder whether the possibility of pooling (reusing) buffers was considered for io package. io.Copy is used in standard library internals (i.e. net/http), and some workloads can benefit from buffer reusage inside io package, which reduces GC pressure.
Consider the following patch:
I have an app using net/http which receives requests at a high rate. Before applying this patch the GC triggered every ~17 seconds reporting 6% cpu usage:
after applying the patch above, the same app running under the same workload sees significantly less frequent GC runs: >90 seconds intervals and cpu usage reported at about 1-2%:
The text was updated successfully, but these errors were encountered: