diff --git a/pkg/util/mempool/pool.go b/pkg/util/mempool/pool.go index b6a37b9aaf5a7..3d1a89ba2813a 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 da0fc361dd4a4..c5de6e8ae0df1 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) + } + }) + } +}