diff --git a/buzhash.go b/buzhash.go index 40b2e9301d1c07af89808f9a6d8912c1d2a8bd6a..edfc4c03df6f955fcfee5e76faf9d6699254a5ba 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 15b46b7a2671952501131062da80ceee24c70cae..8e2b166326532a39a31b5ba66c9b5c15896ab794 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 7aa8a1387855e4e6094813d93a47351644f6070b..9eb8c669390e1b22604f386c2383b67f6cc4cdab 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 27afe5967bce4e4f97eb7e534526bfe68a28fc5f..f2de774421487b82c71a5b4e8ea38d0aaf446cf1 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