Skip to content

Commit a2ebee3

Browse files
committed
replicate io.Copy optimizations
Signed-off-by: Amr Mahdi <[email protected]> (cherry picked from commit f6834d4) Signed-off-by: Amr Mahdi <[email protected]>
1 parent 9b2156a commit a2ebee3

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

content/helpers.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,15 @@ func seekReader(r io.Reader, offset, size int64) (io.Reader, error) {
230230
}
231231

232232
func copyWithBuffer(dst io.Writer, src io.Reader) (written int64, err error) {
233+
// If the reader has a WriteTo method, use it to do the copy.
234+
// Avoids an allocation and a copy.
235+
if wt, ok := src.(io.WriterTo); ok {
236+
return wt.WriteTo(dst)
237+
}
238+
// Similarly, if the writer has a ReadFrom method, use it to do the copy.
239+
if rt, ok := dst.(io.ReaderFrom); ok {
240+
return rt.ReadFrom(src)
241+
}
233242
bufRef := bufPool.Get().(*[]byte)
234243
defer bufPool.Put(bufRef)
235244
buf := *bufRef

0 commit comments

Comments
 (0)