From ea73dc6f6fa236134d68544a93700a459358aee2 Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Thu, 13 Aug 2015 11:42:14 -0700 Subject: [PATCH 1/2] tar/storage: Benchmark for bufferFileGetPutter.Put Signed-off-by: Alexander Morozov --- tar/storage/getter_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tar/storage/getter_test.go b/tar/storage/getter_test.go index 5a6fcc7..c06cff0 100644 --- a/tar/storage/getter_test.go +++ b/tar/storage/getter_test.go @@ -2,7 +2,9 @@ package storage import ( "bytes" + "fmt" "io/ioutil" + "strings" "testing" ) @@ -39,6 +41,7 @@ func TestGetter(t *testing.T) { } } } + func TestPutter(t *testing.T) { fp := NewDiscardFilePutter() // map[filename]map[body]crc64sum @@ -60,3 +63,22 @@ func TestPutter(t *testing.T) { } } } + +func BenchmarkPutter(b *testing.B) { + files := []string{ + strings.Repeat("foo", 1000), + strings.Repeat("bar", 1000), + strings.Repeat("baz", 1000), + strings.Repeat("fooz", 1000), + strings.Repeat("vbatts", 1000), + strings.Repeat("systemd", 1000), + } + for i := 0; i < b.N; i++ { + fgp := NewBufferFileGetPutter() + for n, body := range files { + if _, _, err := fgp.Put(fmt.Sprintf("%d", n), bytes.NewBufferString(body)); err != nil { + b.Fatal(err) + } + } + } +} From 45399711c2466973d96d650eb2c9971fbf3816d7 Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Thu, 13 Aug 2015 11:42:43 -0700 Subject: [PATCH 2/2] 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 {