From f6834d4c0b9a0433d6723ea172ad333ddc44936b Mon Sep 17 00:00:00 2001 From: Amr Mahdi Date: Tue, 27 Oct 2020 01:25:36 +0000 Subject: [PATCH] replicate io.Copy optimizations Signed-off-by: Amr Mahdi --- content/helpers.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/content/helpers.go b/content/helpers.go index 46ca556e9..d15c98224 100644 --- a/content/helpers.go +++ b/content/helpers.go @@ -230,6 +230,15 @@ func seekReader(r io.Reader, offset, size int64) (io.Reader, error) { } func copyWithBuffer(dst io.Writer, src io.Reader) (written int64, err error) { + // If the reader has a WriteTo method, use it to do the copy. + // Avoids an allocation and a copy. + if wt, ok := src.(io.WriterTo); ok { + return wt.WriteTo(dst) + } + // Similarly, if the writer has a ReadFrom method, use it to do the copy. + if rt, ok := dst.(io.ReaderFrom); ok { + return rt.ReadFrom(src) + } bufRef := bufPool.Get().(*[]byte) defer bufPool.Put(bufRef) buf := *bufRef