Re-use zlib writers to reduce memory usage

benchcmp:

    benchmark                       old ns/op       new ns/op       delta
    BenchmarkArchiveDirectory-4     29624960475     29511001504     -0.38%
    BenchmarkSaveJSON-4             379833          225609          -40.60%

    benchmark                       old allocs     new allocs     delta
    BenchmarkArchiveDirectory-4     546736         540642         -1.11%
    BenchmarkSaveJSON-4             150            126            -16.00%

    benchmark                       old bytes      new bytes     delta
    BenchmarkArchiveDirectory-4     1372476952     438519336     -68.05%
    BenchmarkSaveJSON-4             1462773        9087          -99.38%
This commit is contained in:
Alexander Neumann
2015-02-16 22:35:26 +01:00
parent 06ed5c12b8
commit 731e81ef06
2 changed files with 78 additions and 1 deletions
+10 -1
View File
@@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"sync"
"github.com/restic/restic/backend"
"github.com/restic/restic/debug"
@@ -277,6 +278,12 @@ func (s Server) SaveFrom(t backend.Type, id backend.ID, length uint, rd io.Reade
return blob, nil
}
var zWriterPool = sync.Pool{
New: func() interface{} {
return zlib.NewWriter(nil)
},
}
// SaveJSON serialises item as JSON and encrypts and saves it in the backend as
// type t.
func (s Server) SaveJSON(t backend.Type, item interface{}) (Blob, error) {
@@ -286,7 +293,9 @@ func (s Server) SaveJSON(t backend.Type, item interface{}) (Blob, error) {
}
encWr := s.key.EncryptTo(backendBlob)
wr := zlib.NewWriter(encWr)
wr := zWriterPool.Get().(*zlib.Writer)
defer zWriterPool.Put(wr)
wr.Reset(encWr)
if err != nil {
return Blob{}, fmt.Errorf("zlib.NewWriter: %v", err)
}