From 8526cc66479ec651f8876c5b476ca7302959ac6e Mon Sep 17 00:00:00 2001 From: greatroar <@> Date: Wed, 26 Feb 2020 23:26:11 +0100 Subject: [PATCH] Remove sync.Pool from internal/repository MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The pool was used improperly, causing more allocations to be performed than without it. name old time/op new time/op delta SaveAndEncrypt-8 36.8ms ± 2% 36.9ms ± 2% ~ (p=0.218 n=10+10) name old speed new speed delta SaveAndEncrypt-8 114MB/s ± 2% 114MB/s ± 2% ~ (p=0.218 n=10+10) name old alloc/op new alloc/op delta SaveAndEncrypt-8 21.1MB ± 0% 21.0MB ± 0% -0.44% (p=0.000 n=10+10) name old allocs/op new allocs/op delta SaveAndEncrypt-8 79.0 ± 0% 77.0 ± 0% -2.53% (p=0.000 n=10+10) --- internal/repository/pool.go | 21 --------------------- internal/repository/repository.go | 7 ++----- internal/repository/repository_test.go | 1 + 3 files changed, 3 insertions(+), 26 deletions(-) delete mode 100644 internal/repository/pool.go diff --git a/internal/repository/pool.go b/internal/repository/pool.go deleted file mode 100644 index b87791f14..000000000 --- a/internal/repository/pool.go +++ /dev/null @@ -1,21 +0,0 @@ -package repository - -import ( - "sync" - - "github.com/restic/chunker" -) - -var bufPool = sync.Pool{ - New: func() interface{} { - return make([]byte, chunker.MaxSize/3) - }, -} - -func getBuf() []byte { - return bufPool.Get().([]byte) -} - -func freeBuf(data []byte) { - bufPool.Put(data) -} diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 67df4b3b2..3457f54d6 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -225,13 +225,10 @@ func (r *Repository) SaveAndEncrypt(ctx context.Context, t restic.BlobType, data debug.Log("save id %v (%v, %d bytes)", id, t, len(data)) - // get buf from the pool - ciphertext := getBuf() - - ciphertext = ciphertext[:0] nonce := crypto.NewRandomNonce() + + ciphertext := make([]byte, 0, restic.CiphertextLength(len(data))) ciphertext = append(ciphertext, nonce...) - defer freeBuf(ciphertext) // encrypt blob ciphertext = r.key.Seal(ciphertext, nonce, data, nil) diff --git a/internal/repository/repository_test.go b/internal/repository/repository_test.go index 43d70c533..4ec71e726 100644 --- a/internal/repository/repository_test.go +++ b/internal/repository/repository_test.go @@ -104,6 +104,7 @@ func BenchmarkSaveAndEncrypt(t *testing.B) { id := restic.ID(sha256.Sum256(data)) + t.ReportAllocs() t.ResetTimer() t.SetBytes(int64(size))