From b1c398a9fac50efee5232df2c9a09ba6da7af448 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Sun, 6 Oct 2019 23:34:06 +0200 Subject: [PATCH] Don't use pools for result buffers Don't return them either in benchmarks License: MIT Signed-off-by: Jakub Sztandera --- buzhash.go | 12 +++++++++--- buzhash_test.go | 6 ++---- rabin_test.go | 5 +++-- splitting_test.go | 7 +++---- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/buzhash.go b/buzhash.go index 40b2e93..edfc4c0 100644 --- a/buzhash.go +++ b/buzhash.go @@ -38,7 +38,12 @@ func (b *Buzhash) NextBytes() ([]byte, error) { if err != nil { if err == io.ErrUnexpectedEOF { b.err = io.EOF - return buf[:n+b.n], nil + res := make([]byte, n+b.n) + copy(res, buf) + + pool.Put(b.buf) + b.buf = nil + return res, nil } else { b.err = err pool.Put(buf) @@ -60,8 +65,9 @@ func (b *Buzhash) NextBytes() ([]byte, error) { state = bits.RotateLeft32(state, 1) ^ bytehash[buf[i-32]] ^ bytehash[buf[i]] } - res := buf[:i] - b.buf = pool.Get(buzMax) + res := make([]byte, i) + copy(res, b.buf) + b.n = copy(b.buf, buf[i:]) return res, nil diff --git a/buzhash_test.go b/buzhash_test.go index 15b46b7..8e2b166 100644 --- a/buzhash_test.go +++ b/buzhash_test.go @@ -7,7 +7,6 @@ import ( "testing" util "github.com/ipfs/go-ipfs-util" - pool "github.com/libp2p/go-buffer-pool" ) func TestBuzhashChunking(t *testing.T) { @@ -49,10 +48,10 @@ func TestBuzhashChunkReuse(t *testing.T) { } func BenchmarkBuzhash(b *testing.B) { - data := make([]byte, 16<<20) + data := make([]byte, 1<<10) util.NewTimeSeededRand().Read(data) - b.SetBytes(16 << 20) + b.SetBytes(int64(len(data))) b.ReportAllocs() b.ResetTimer() @@ -70,7 +69,6 @@ func BenchmarkBuzhash(b *testing.B) { b.Fatal(err) } res = res + uint64(len(chunk)) - pool.Put(chunk) } } Res = Res + res diff --git a/rabin_test.go b/rabin_test.go index 7aa8a13..9eb8c66 100644 --- a/rabin_test.go +++ b/rabin_test.go @@ -96,10 +96,11 @@ func TestRabinChunkReuse(t *testing.T) { var Res uint64 func BenchmarkRabin(b *testing.B) { - data := make([]byte, 16<<20) + const size = 1 << 10 + data := make([]byte, size) util.NewTimeSeededRand().Read(data) - b.SetBytes(16 << 20) + b.SetBytes(size) b.ReportAllocs() b.ResetTimer() diff --git a/splitting_test.go b/splitting_test.go index 27afe59..f2de774 100644 --- a/splitting_test.go +++ b/splitting_test.go @@ -7,7 +7,6 @@ import ( u "github.com/ipfs/go-ipfs-util" util "github.com/ipfs/go-ipfs-util" - pool "github.com/libp2p/go-buffer-pool" ) func randBuf(t *testing.T, size int) []byte { @@ -122,10 +121,11 @@ func (s *clipReader) Read(buf []byte) (int, error) { } func BenchmarkDefault(b *testing.B) { - data := make([]byte, 16<<20) + const size = 1 << 10 + data := make([]byte, size) util.NewTimeSeededRand().Read(data) - b.SetBytes(16 << 20) + b.SetBytes(size) b.ReportAllocs() b.ResetTimer() @@ -143,7 +143,6 @@ func BenchmarkDefault(b *testing.B) { b.Fatal(err) } res = res + uint64(len(chunk)) - pool.Put(chunk) } } Res = Res + res -- GitLab