From 3a6825e1a0371a5c541f40f3968ac83a363b71da Mon Sep 17 00:00:00 2001 From: Derek McGowan Date: Thu, 19 Apr 2018 15:43:00 -0700 Subject: [PATCH] content: remove unnecessary defer in helpers Deferring the put back of the buffer is not necessary since it can be done immediately after use, before checking error. This decreases the amount of the time the buffer is out of the pool and not in use. Signed-off-by: Derek McGowan --- content/helpers.go | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/content/helpers.go b/content/helpers.go index cfe309126..3b0de7ac1 100644 --- a/content/helpers.go +++ b/content/helpers.go @@ -136,10 +136,7 @@ func Copy(ctx context.Context, cw Writer, r io.Reader, size int64, expected dige } } - buf := bufPool.Get().(*[]byte) - defer bufPool.Put(buf) - - if _, err := io.CopyBuffer(cw, r, *buf); err != nil { + if _, err := copyWithBuffer(cw, r); err != nil { return err } @@ -160,11 +157,7 @@ func CopyReaderAt(cw Writer, ra ReaderAt, n int64) error { return err } - buf := bufPool.Get().(*[]byte) - defer bufPool.Put(buf) - - _, err = io.CopyBuffer(cw, io.NewSectionReader(ra, ws.Offset, n), *buf) - + _, err = copyWithBuffer(cw, io.NewSectionReader(ra, ws.Offset, n)) return err } @@ -195,10 +188,7 @@ func seekReader(r io.Reader, offset, size int64) (io.Reader, error) { } // well then, let's just discard up to the offset - buf := bufPool.Get().(*[]byte) - defer bufPool.Put(buf) - - n, err := io.CopyBuffer(ioutil.Discard, io.LimitReader(r, offset), *buf) + n, err := copyWithBuffer(ioutil.Discard, io.LimitReader(r, offset)) if err != nil { return nil, errors.Wrap(err, "failed to discard to offset") } @@ -208,3 +198,10 @@ func seekReader(r io.Reader, offset, size int64) (io.Reader, error) { return r, nil } + +func copyWithBuffer(dst io.Writer, src io.Reader) (written int64, err error) { + buf := bufPool.Get().(*[]byte) + written, err = io.CopyBuffer(dst, src, *buf) + bufPool.Put(buf) + return +}