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

Commit b1c398a

Browse files
committed
Don't use pools for result buffers
Don't return them either in benchmarks License: MIT Signed-off-by: Jakub Sztandera <[email protected]>
1 parent 9e34967 commit b1c398a

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

buzhash.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ func (b *Buzhash) NextBytes() ([]byte, error) {
3838
if err != nil {
3939
if err == io.ErrUnexpectedEOF {
4040
b.err = io.EOF
41-
return buf[:n+b.n], nil
41+
res := make([]byte, n+b.n)
42+
copy(res, buf)
43+
44+
pool.Put(b.buf)
45+
b.buf = nil
46+
return res, nil
4247
} else {
4348
b.err = err
4449
pool.Put(buf)
@@ -60,8 +65,9 @@ func (b *Buzhash) NextBytes() ([]byte, error) {
6065
state = bits.RotateLeft32(state, 1) ^ bytehash[buf[i-32]] ^ bytehash[buf[i]]
6166
}
6267

63-
res := buf[:i]
64-
b.buf = pool.Get(buzMax)
68+
res := make([]byte, i)
69+
copy(res, b.buf)
70+
6571
b.n = copy(b.buf, buf[i:])
6672

6773
return res, nil

buzhash_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"testing"
88

99
util "github.com/ipfs/go-ipfs-util"
10-
pool "github.com/libp2p/go-buffer-pool"
1110
)
1211

1312
func TestBuzhashChunking(t *testing.T) {
@@ -49,10 +48,10 @@ func TestBuzhashChunkReuse(t *testing.T) {
4948
}
5049

5150
func BenchmarkBuzhash(b *testing.B) {
52-
data := make([]byte, 16<<20)
51+
data := make([]byte, 1<<10)
5352
util.NewTimeSeededRand().Read(data)
5453

55-
b.SetBytes(16 << 20)
54+
b.SetBytes(int64(len(data)))
5655
b.ReportAllocs()
5756
b.ResetTimer()
5857

@@ -70,7 +69,6 @@ func BenchmarkBuzhash(b *testing.B) {
7069
b.Fatal(err)
7170
}
7271
res = res + uint64(len(chunk))
73-
pool.Put(chunk)
7472
}
7573
}
7674
Res = Res + res

rabin_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,11 @@ func TestRabinChunkReuse(t *testing.T) {
9696
var Res uint64
9797

9898
func BenchmarkRabin(b *testing.B) {
99-
data := make([]byte, 16<<20)
99+
const size = 1 << 10
100+
data := make([]byte, size)
100101
util.NewTimeSeededRand().Read(data)
101102

102-
b.SetBytes(16 << 20)
103+
b.SetBytes(size)
103104
b.ReportAllocs()
104105
b.ResetTimer()
105106

splitting_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
u "github.com/ipfs/go-ipfs-util"
99
util "github.com/ipfs/go-ipfs-util"
10-
pool "github.com/libp2p/go-buffer-pool"
1110
)
1211

1312
func randBuf(t *testing.T, size int) []byte {
@@ -122,10 +121,11 @@ func (s *clipReader) Read(buf []byte) (int, error) {
122121
}
123122

124123
func BenchmarkDefault(b *testing.B) {
125-
data := make([]byte, 16<<20)
124+
const size = 1 << 10
125+
data := make([]byte, size)
126126
util.NewTimeSeededRand().Read(data)
127127

128-
b.SetBytes(16 << 20)
128+
b.SetBytes(size)
129129
b.ReportAllocs()
130130
b.ResetTimer()
131131

@@ -143,7 +143,6 @@ func BenchmarkDefault(b *testing.B) {
143143
b.Fatal(err)
144144
}
145145
res = res + uint64(len(chunk))
146-
pool.Put(chunk)
147146
}
148147
}
149148
Res = Res + res

0 commit comments

Comments
 (0)