diff --git a/pkg/util/mempool/pool.go b/pkg/util/mempool/pool.go index 1bc5b9a369a15..f565fdecd8277 100644 --- a/pkg/util/mempool/pool.go +++ b/pkg/util/mempool/pool.go @@ -1,25 +1,20 @@ package mempool import ( - "errors" "fmt" "sync" "time" - "unsafe" "github.com/dustin/go-humanize" "github.com/prometheus/client_golang/prometheus" ) var ( - errSlabExhausted = errors.New("slab exhausted") - - reasonSizeExceeded = "size-exceeded" - reasonSlabExhausted = "slab-exhausted" + reasonSizeExceeded = "size-exceeded" ) type slab struct { - buffer chan unsafe.Pointer + buffer chan []byte size, count int once sync.Once metrics *metrics @@ -39,11 +34,10 @@ func newSlab(bufferSize, bufferCount int, m *metrics) *slab { } func (s *slab) init() { - s.buffer = make(chan unsafe.Pointer, s.count) + s.buffer = make(chan []byte, s.count) for i := 0; i < s.count; i++ { buf := make([]byte, 0, s.size) - ptr := unsafe.Pointer(unsafe.SliceData(buf)) //#nosec G103 -- Simple arena allocator implementation, does not appear to allow for any unsafe operations. - s.buffer <- ptr + s.buffer <- buf } s.metrics.availableBuffersPerSlab.WithLabelValues(s.name).Set(float64(s.count)) } @@ -54,8 +48,7 @@ func (s *slab) get(size int) ([]byte, error) { waitStart := time.Now() // wait for available buffer on channel - ptr := <-s.buffer - buf := unsafe.Slice((*byte)(ptr), s.size) //#nosec G103 -- Simple arena allocator implementation, does not appear to allow for any unsafe operations. + buf := <-s.buffer s.metrics.waitDuration.WithLabelValues(s.name).Observe(time.Since(waitStart).Seconds()) return buf[:size], nil @@ -67,9 +60,8 @@ func (s *slab) put(buf []byte) { panic("slab is not initialized") } - ptr := unsafe.Pointer(unsafe.SliceData(buf)) //#nosec G103 -- Simple arena allocator implementation, does not appear to allow for any unsafe operations. // Note that memory is NOT zero'd on return, but since all allocations are of defined widths and we only ever then read a record of exactly that width into the allocation, it will always be overwritten before use and can't leak. - s.buffer <- ptr + s.buffer <- buf } // MemPool is an Allocator implementation that uses a fixed size memory pool