Skip to content

Commit

Permalink
copyZeroAlloc: Try WriteTo and ReadFrom before acquiring a buffer (#1673
Browse files Browse the repository at this point in the history
)

These are the same statements at the beginning of io.CopyBuffer, but
by doing them ourselves first we trade off a little cpu for not holding
the 4kb buffer during the write.
  • Loading branch information
Jille authored Dec 2, 2023
1 parent 9b4e42a commit 2ac2a39
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2120,6 +2120,12 @@ func writeBodyFixedSize(w *bufio.Writer, r io.Reader, size int64) error {
}

func copyZeroAlloc(w io.Writer, r io.Reader) (int64, error) {
if wt, ok := r.(io.WriterTo); ok {
return wt.WriteTo(w)
}
if rt, ok := w.(io.ReaderFrom); ok {
return rt.ReadFrom(r)
}
vbuf := copyBufPool.Get()
buf := vbuf.([]byte)
n, err := io.CopyBuffer(w, r, buf)
Expand Down

0 comments on commit 2ac2a39

Please sign in to comment.