From 908573cdbe2e8b6f91ce026cf8632ff5f2c41110 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Matczuk?= Date: Fri, 9 Dec 2022 15:58:56 +0100 Subject: [PATCH] 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 --- src/net/http/transfer.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/net/http/transfer.go b/src/net/http/transfer.go index b24998174f6ec..dffff56b31e94 100644 --- a/src/net/http/transfer.go +++ b/src/net/http/transfer.go @@ -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 }