1
0
mirror of https://github.com/golang/go synced 2024-11-16 17:54:39 -07:00

net/http: use copyBufPool in transferWriter.doBodyCopy()

This is a followup to CL 14177. It applies copyBufPool optimization to
transferWriter.doBodyCopy(). The function is used every time Request or
Response is written.

Without this patch for every Request and Response processed, if there is
a body, we need to allocate and GC a 32k buffer. This is quickly causing
GC pressure.

Fixes #57202
This commit is contained in:
Michał Matczuk 2022-12-09 15:58:56 +01:00 committed by Michał Matczuk
parent 4e896d179d
commit 908573cdbe
No known key found for this signature in database

View File

@ -410,7 +410,11 @@ func (t *transferWriter) writeBody(w io.Writer) (err error) {
//
// This function is only intended for use in writeBody.
func (t *transferWriter) doBodyCopy(dst io.Writer, src io.Reader) (n int64, err error) {
n, err = io.Copy(dst, src)
bufp := copyBufPool.Get().(*[]byte)
buf := *bufp
defer copyBufPool.Put(bufp)
n, err = io.CopyBuffer(dst, src, buf)
if err != nil && err != io.EOF {
t.bodyReadError = err
}