Skip to content

Commit

Permalink
net/http: use copyBufPool in transferWriter.doBodyCopy()
Browse files Browse the repository at this point in the history
This patch is a followup to 6fd82d8.
It applies copyBufPool optimization to transferWriter.doBodyCopy().
The function is very frequently 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
  • Loading branch information
mmatczuk committed Nov 2, 2023
1 parent 4e896d1 commit d02a78b
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/net/http/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit d02a78b

Please sign in to comment.