Skip to content

Commit

Permalink
perf(blooms): mempool no longer zeroes out buffers unnecessarily (#13282
Browse files Browse the repository at this point in the history
)
  • Loading branch information
owen-d authored Jun 21, 2024
1 parent d36e1d5 commit eb1cd4c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
9 changes: 0 additions & 9 deletions pkg/util/mempool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,6 @@ func (s *slab) get(size int) ([]byte, error) {
return nil, errSlabExhausted
}

// Taken from https://github.com/ortuman/nuke/blob/main/monotonic_arena.go#L37-L48
// This piece of code will be translated into a runtime.memclrNoHeapPointers
// invocation by the compiler, which is an assembler optimized implementation.
// Architecture specific code can be found at src/runtime/memclr_$GOARCH.s
// in Go source (since https://codereview.appspot.com/137880043).
for i := range buf {
buf[i] = 0
}

return buf[:size], nil
}

Expand Down
42 changes: 24 additions & 18 deletions pkg/util/mempool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"unsafe"

"github.com/stretchr/testify/require"

"github.com/grafana/loki/v3/pkg/util/flagext"
)

func TestMemPool(t *testing.T) {
Expand Down Expand Up @@ -43,24 +45,6 @@ func TestMemPool(t *testing.T) {
require.Equal(t, 512, cap(res))
})

t.Run("buffer is cleared when returned", func(t *testing.T) {
pool := New("test", []Bucket{
{Size: 1, Capacity: 64},
}, nil)
res, err := pool.Get(8)
require.NoError(t, err)
require.Equal(t, 8, len(res))
source := []byte{0, 1, 2, 3, 4, 5, 6, 7}
copy(res, source)

pool.Put(res)

res, err = pool.Get(8)
require.NoError(t, err)
require.Equal(t, 8, len(res))
require.Equal(t, make([]byte, 8), res)
})

t.Run("pool returns error when no buffer is available", func(t *testing.T) {
pool := New("test", []Bucket{
{Size: 1, Capacity: 64},
Expand Down Expand Up @@ -131,3 +115,25 @@ func TestMemPool(t *testing.T) {
t.Log("finished")
})
}

func BenchmarkSlab(b *testing.B) {
for _, sz := range []int{
1 << 10, // 1KB
1 << 20, // 1MB
128 << 20, // 128MB
} {
b.Run(flagext.ByteSize(uint64(sz)).String(), func(b *testing.B) {
slab := newSlab(sz, 1, newMetrics(nil, "test"))
slab.init()
b.ResetTimer()

for i := 0; i < b.N; i++ {
b, err := slab.get(sz)
if err != nil {
panic(err)
}
slab.put(b)
}
})
}
}

0 comments on commit eb1cd4c

Please sign in to comment.