Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
Don't use pools for result buffers
Browse files Browse the repository at this point in the history
Don't return them either in benchmarks

License: MIT
Signed-off-by: Jakub Sztandera <[email protected]>
  • Loading branch information
Jakub Sztandera committed Oct 6, 2019
1 parent 9e34967 commit b1c398a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
12 changes: 9 additions & 3 deletions buzhash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions buzhash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()

Expand All @@ -70,7 +69,6 @@ func BenchmarkBuzhash(b *testing.B) {
b.Fatal(err)
}
res = res + uint64(len(chunk))
pool.Put(chunk)
}
}
Res = Res + res
Expand Down
5 changes: 3 additions & 2 deletions rabin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
7 changes: 3 additions & 4 deletions splitting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()

Expand All @@ -143,7 +143,6 @@ func BenchmarkDefault(b *testing.B) {
b.Fatal(err)
}
res = res + uint64(len(chunk))
pool.Put(chunk)
}
}
Res = Res + res
Expand Down

0 comments on commit b1c398a

Please sign in to comment.