forked from cortexproject/cortex
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reuse write request from distributor to Ingesters (cortexproject#5193)
* wip Signed-off-by: Alan Protasio <alanprot@gmail.com> * branchmark Signed-off-by: Alan Protasio <alanprot@gmail.com> * fix some linting / test Signed-off-by: Alan Protasio <alanprot@gmail.com> * No allocation Signed-off-by: Alan Protasio <alanprot@gmail.com> * min pool size Signed-off-by: Alan Protasio <alanprot@gmail.com> * min pool size Signed-off-by: Alan Protasio <alanprot@gmail.com> * fuzzy test Signed-off-by: Alan Protasio <alanprot@gmail.com> * changelog Signed-off-by: Alan Protasio <alanprot@gmail.com> * more benchmark Signed-off-by: Alan Protasio <alanprot@gmail.com> * fix bug on the reuse Signed-off-by: Alan Protasio <alanprot@gmail.com> --------- Signed-off-by: Alan Protasio <alanprot@gmail.com> Signed-off-by: Alex Le <leqiyue@amazon.com>
- Loading branch information
Showing
8 changed files
with
220 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package cortexpb | ||
|
||
import ( | ||
"math" | ||
"sync" | ||
) | ||
|
||
const ( | ||
minPoolSizePower = 5 | ||
) | ||
|
||
type byteSlicePools struct { | ||
pools []sync.Pool | ||
} | ||
|
||
func newSlicePool(pools int) *byteSlicePools { | ||
sp := byteSlicePools{} | ||
sp.init(pools) | ||
return &sp | ||
} | ||
|
||
func (sp *byteSlicePools) init(pools int) { | ||
sp.pools = make([]sync.Pool, pools) | ||
for i := 0; i < pools; i++ { | ||
size := int(math.Pow(2, float64(i+minPoolSizePower))) | ||
sp.pools[i] = sync.Pool{ | ||
New: func() interface{} { | ||
buf := make([]byte, 0, size) | ||
return &buf | ||
}, | ||
} | ||
} | ||
} | ||
|
||
func (sp *byteSlicePools) getSlice(size int) *[]byte { | ||
index := int(math.Ceil(math.Log2(float64(size)))) - minPoolSizePower | ||
|
||
if index >= len(sp.pools) { | ||
buf := make([]byte, size) | ||
return &buf | ||
} | ||
|
||
// if the size is < than the minPoolSizePower we return an array from the first pool | ||
if index < 0 { | ||
index = 0 | ||
} | ||
|
||
s := sp.pools[index].Get().(*[]byte) | ||
*s = (*s)[:size] | ||
return s | ||
} | ||
|
||
func (sp *byteSlicePools) reuseSlice(s *[]byte) { | ||
index := int(math.Floor(math.Log2(float64(cap(*s))))) - minPoolSizePower | ||
|
||
if index >= len(sp.pools) || index < 0 { | ||
return | ||
} | ||
|
||
sp.pools[index].Put(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package cortexpb | ||
|
||
import ( | ||
"math" | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFuzzyByteSlicePools(t *testing.T) { | ||
sut := newSlicePool(20) | ||
maxByteSize := int(math.Pow(2, 20+minPoolSizePower-1)) | ||
|
||
for i := 0; i < 1000; i++ { | ||
size := rand.Int() % maxByteSize | ||
s := sut.getSlice(size) | ||
assert.Equal(t, len(*s), size) | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters