From 6f6c23482be2f57fc1011187403b4aa5cdfc1a14 Mon Sep 17 00:00:00 2001 From: Christian Haudum Date: Mon, 24 Jun 2024 10:17:02 +0200 Subject: [PATCH] perf(blooms): Replace sync.Mutex with sync.Once Signed-off-by: Christian Haudum --- pkg/util/mempool/pool.go | 8 ++------ pkg/util/mempool/pool_test.go | 1 - 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/pkg/util/mempool/pool.go b/pkg/util/mempool/pool.go index 3d1a89ba2813a..4ac5b96062c05 100644 --- a/pkg/util/mempool/pool.go +++ b/pkg/util/mempool/pool.go @@ -20,7 +20,7 @@ var ( type slab struct { buffer chan unsafe.Pointer size, count int - mtx sync.Mutex + once sync.Once metrics *metrics name string } @@ -49,11 +49,7 @@ func (s *slab) init() { func (s *slab) get(size int) ([]byte, error) { s.metrics.accesses.WithLabelValues(s.name, opTypeGet).Inc() - s.mtx.Lock() - if s.buffer == nil { - s.init() - } - defer s.mtx.Unlock() + s.once.Do(s.init) // wait for available buffer on channel var buf []byte diff --git a/pkg/util/mempool/pool_test.go b/pkg/util/mempool/pool_test.go index c5de6e8ae0df1..289479078df66 100644 --- a/pkg/util/mempool/pool_test.go +++ b/pkg/util/mempool/pool_test.go @@ -124,7 +124,6 @@ func BenchmarkSlab(b *testing.B) { } { 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++ {