From 8d76363085552a669042fe2912160843d0d3766b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Sat, 21 Aug 2021 03:24:39 +0200 Subject: [PATCH] Avoid a 32 kB file allocation on every bitBucketFilePutter.Put MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit io.Copy usually allocates a 32kB buffer, and due to the large number of files processed by tar-split, this shows up in Go profiles as a very large alloc_space total. It doesn't seem to actually be a measurable problem in any way, but we can allocate the buffer only once per tar-split creation, at no additional cost to existing allocations, so let's do so, and remove the distraction. Signed-off-by: Miloslav Trmač --- tar/storage/getter.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tar/storage/getter.go b/tar/storage/getter.go index ae11f8f..9fed24a 100644 --- a/tar/storage/getter.go +++ b/tar/storage/getter.go @@ -92,11 +92,12 @@ func NewDiscardFilePutter() FilePutter { } type bitBucketFilePutter struct { + buffer [32 * 1024]byte // 32 kB is the buffer size currently used by io.Copy, as of August 2021. } func (bbfp *bitBucketFilePutter) Put(name string, r io.Reader) (int64, []byte, error) { c := crc64.New(CRCTable) - i, err := io.Copy(c, r) + i, err := io.CopyBuffer(c, r, bbfp.buffer[:]) return i, c.Sum(nil), err }