Skip to content

Commit

Permalink
fix bug on the reuse
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Protasio <alanprot@gmail.com>
  • Loading branch information
alanprot committed Mar 7, 2023
1 parent 02729b1 commit 22a1a36
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
7 changes: 2 additions & 5 deletions pkg/cortexpb/slicesPool.go
Expand Up @@ -40,6 +40,7 @@ func (sp *byteSlicePools) getSlice(size int) *[]byte {
return &buf
}

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

if index >= len(sp.pools) {
if index >= len(sp.pools) || index < 0 {
return
}

if index < 0 {
index = 0
}

sp.pools[index].Put(s)
}
9 changes: 9 additions & 0 deletions pkg/cortexpb/slicesPool_test.go
Expand Up @@ -19,3 +19,12 @@ func TestFuzzyByteSlicePools(t *testing.T) {
sut.reuseSlice(s)
}
}

func TestReturnSliceSmallerThanMin(t *testing.T) {
sut := newSlicePool(20)
size := 3
buff := make([]byte, 0, size)
sut.reuseSlice(&buff)
buff2 := sut.getSlice(size * 2)
assert.Equal(t, len(*buff2), size*2)
}
4 changes: 3 additions & 1 deletion pkg/cortexpb/timeseries.go
Expand Up @@ -98,7 +98,9 @@ func ReuseWriteRequest(req *PreallocWriteRequest) {
bytePool.reuseSlice(req.data)
req.data = nil
}

req.Source = 0
req.Metadata = nil
req.Timeseries = nil
writeRequestPool.Put(req)
}

Expand Down

0 comments on commit 22a1a36

Please sign in to comment.