From 2ac2a3911bc0d9218d2ba2699c143a9a19540e76 Mon Sep 17 00:00:00 2001 From: Jille Timmermans Date: Sat, 2 Dec 2023 18:05:12 +0100 Subject: [PATCH] copyZeroAlloc: Try WriteTo and ReadFrom before acquiring a buffer (#1673) 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. --- http.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/http.go b/http.go index d134b62d11..4f8718c445 100644 --- a/http.go +++ b/http.go @@ -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)