From 45399711c2466973d96d650eb2c9971fbf3816d7 Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Thu, 13 Aug 2015 11:42:43 -0700 Subject: [PATCH] tar/storage: Replace TeeReader with MultiWriter It uses slightly less memory and more understandable. Benchmar results: benchmark old ns/op new ns/op delta BenchmarkPutter-4 57272 52375 -8.55% benchmark old allocs new allocs delta BenchmarkPutter-4 21 19 -9.52% benchmark old bytes new bytes delta BenchmarkPutter-4 19416 13336 -31.31% Signed-off-by: Alexander Morozov --- tar/storage/getter.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tar/storage/getter.go b/tar/storage/getter.go index 70fd378..ae11f8f 100644 --- a/tar/storage/getter.go +++ b/tar/storage/getter.go @@ -59,15 +59,15 @@ func (bfgp bufferFileGetPutter) Get(name string) (io.ReadCloser, error) { } func (bfgp *bufferFileGetPutter) Put(name string, r io.Reader) (int64, []byte, error) { - c := crc64.New(CRCTable) - tRdr := io.TeeReader(r, c) - b := bytes.NewBuffer([]byte{}) - i, err := io.Copy(b, tRdr) + crc := crc64.New(CRCTable) + buf := bytes.NewBuffer(nil) + cw := io.MultiWriter(crc, buf) + i, err := io.Copy(cw, r) if err != nil { return 0, nil, err } - bfgp.files[name] = b.Bytes() - return i, c.Sum(nil), nil + bfgp.files[name] = buf.Bytes() + return i, crc.Sum(nil), nil } type readCloserWrapper struct {