Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(blooms): mempool no longer zeroes out buffers unnecessarily #13282

Merged
merged 3 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions pkg/util/mempool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,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)
}
})
}
}
Loading