From eb1cd4c8555a42cc12c02124ee39a3be0120587b Mon Sep 17 00:00:00 2001 From: Owen Diehl Date: Fri, 21 Jun 2024 13:02:46 -0700 Subject: [PATCH] perf(blooms): mempool no longer zeroes out buffers unnecessarily (#13282) --- pkg/util/mempool/pool.go | 9 -------- pkg/util/mempool/pool_test.go | 42 ++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/pkg/util/mempool/pool.go b/pkg/util/mempool/pool.go index b6a37b9aaf5a..3d1a89ba2813 100644 --- a/pkg/util/mempool/pool.go +++ b/pkg/util/mempool/pool.go @@ -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 } diff --git a/pkg/util/mempool/pool_test.go b/pkg/util/mempool/pool_test.go index da0fc361dd4a..c5de6e8ae0df 100644 --- a/pkg/util/mempool/pool_test.go +++ b/pkg/util/mempool/pool_test.go @@ -8,6 +8,8 @@ import ( "unsafe" "github.com/stretchr/testify/require" + + "github.com/grafana/loki/v3/pkg/util/flagext" ) func TestMemPool(t *testing.T) { @@ -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}, @@ -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) + } + }) + } +}