From 55533693be0f268d7d7a2d339369ce8617398542 Mon Sep 17 00:00:00 2001 From: unclejack Date: Sun, 17 Aug 2014 14:29:46 +0300 Subject: [PATCH] pkg/tarsum: avoid buf2 allocation in Read Docker-DCO-1.1-Signed-off-by: Cristian Staretu (github: unclejack) --- pkg/tarsum/tarsum.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/tarsum/tarsum.go b/pkg/tarsum/tarsum.go index 32b5e42589..05b9ef4e2c 100644 --- a/pkg/tarsum/tarsum.go +++ b/pkg/tarsum/tarsum.go @@ -23,6 +23,7 @@ type TarSum struct { gz writeCloseFlusher bufTar *bytes.Buffer bufGz *bytes.Buffer + bufData [8192]byte h hash.Hash sums map[string]string currentFile string @@ -92,7 +93,12 @@ func (ts *TarSum) Read(buf []byte) (int, error) { if ts.finished { return ts.bufGz.Read(buf) } - buf2 := make([]byte, len(buf), cap(buf)) + var buf2 []byte + if len(buf) > 8192 { + buf2 = make([]byte, len(buf), cap(buf)) + } else { + buf2 = ts.bufData[:len(buf)-1] + } n, err := ts.tarR.Read(buf2) if err != nil {