Skip to content

Commit 22a1a36

Browse files
committed
fix bug on the reuse
Signed-off-by: Alan Protasio <[email protected]>
1 parent 02729b1 commit 22a1a36

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

pkg/cortexpb/slicesPool.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func (sp *byteSlicePools) getSlice(size int) *[]byte {
4040
return &buf
4141
}
4242

43+
// if the size is < than the minPoolSizePower we return an array from the first pool
4344
if index < 0 {
4445
index = 0
4546
}
@@ -52,13 +53,9 @@ func (sp *byteSlicePools) getSlice(size int) *[]byte {
5253
func (sp *byteSlicePools) reuseSlice(s *[]byte) {
5354
index := int(math.Floor(math.Log2(float64(cap(*s))))) - minPoolSizePower
5455

55-
if index >= len(sp.pools) {
56+
if index >= len(sp.pools) || index < 0 {
5657
return
5758
}
5859

59-
if index < 0 {
60-
index = 0
61-
}
62-
6360
sp.pools[index].Put(s)
6461
}

pkg/cortexpb/slicesPool_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,12 @@ func TestFuzzyByteSlicePools(t *testing.T) {
1919
sut.reuseSlice(s)
2020
}
2121
}
22+
23+
func TestReturnSliceSmallerThanMin(t *testing.T) {
24+
sut := newSlicePool(20)
25+
size := 3
26+
buff := make([]byte, 0, size)
27+
sut.reuseSlice(&buff)
28+
buff2 := sut.getSlice(size * 2)
29+
assert.Equal(t, len(*buff2), size*2)
30+
}

pkg/cortexpb/timeseries.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ func ReuseWriteRequest(req *PreallocWriteRequest) {
9898
bytePool.reuseSlice(req.data)
9999
req.data = nil
100100
}
101-
101+
req.Source = 0
102+
req.Metadata = nil
103+
req.Timeseries = nil
102104
writeRequestPool.Put(req)
103105
}
104106

0 commit comments

Comments
 (0)