allow pre-allocating buffers to reduce frequent GCs during growth (#18686)

This PR also increases per node bpool memory from 1024 entries
to 2048 entries; along with that, it also moves the byte pool
centrally instead of being per pool.
This commit is contained in:
Harshavardhana
2023-12-21 08:59:38 -08:00
committed by GitHub
parent 56b7045c20
commit 7c948adf88
12 changed files with 88 additions and 68 deletions

View File

@@ -1,4 +1,4 @@
// Copyright (c) 2015-2021 MinIO, Inc.
// Copyright (c) 2015-2023 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
@@ -17,6 +17,8 @@
package bpool
import "github.com/klauspost/reedsolomon"
// BytePoolCap implements a leaky pool of []byte in the form of a bounded channel.
type BytePoolCap struct {
c chan []byte
@@ -27,6 +29,12 @@ type BytePoolCap struct {
// NewBytePoolCap creates a new BytePool bounded to the given maxSize, with new
// byte arrays sized based on width.
func NewBytePoolCap(maxSize int, width int, capwidth int) (bp *BytePoolCap) {
if capwidth > 0 && capwidth < 64 {
panic("buffer capped with smaller than 64 bytes is not supported")
}
if capwidth > 0 && width > capwidth {
panic("buffer length cannot be > capacity of the buffer")
}
return &BytePoolCap{
c: make(chan []byte, maxSize),
w: width,
@@ -34,18 +42,25 @@ func NewBytePoolCap(maxSize int, width int, capwidth int) (bp *BytePoolCap) {
}
}
// Populate - populates and pre-warms the byte pool, this function is non-blocking.
func (bp *BytePoolCap) Populate() {
for _, buf := range reedsolomon.AllocAligned(cap(bp.c), bp.wcap) {
bp.Put(buf[:bp.w])
}
}
// Get gets a []byte from the BytePool, or creates a new one if none are
// available in the pool.
func (bp *BytePoolCap) Get() (b []byte) {
select {
case b = <-bp.c:
// reuse existing buffer
// reuse existing buffer
default:
// create new buffer
// create new aligned buffer
if bp.wcap > 0 {
b = make([]byte, bp.w, bp.wcap)
b = reedsolomon.AllocAligned(1, bp.wcap)[0][:bp.w]
} else {
b = make([]byte, bp.w)
b = reedsolomon.AllocAligned(1, bp.w)[0]
}
}
return

View File

@@ -22,8 +22,8 @@ import "testing"
// Tests - bytePool functionality.
func TestBytePool(t *testing.T) {
size := 4
width := 10
capWidth := 16
width := 1024
capWidth := 2048
bufPool := NewBytePoolCap(size, width, capWidth)
@@ -43,7 +43,7 @@ func TestBytePool(t *testing.T) {
t.Fatalf("bytepool length invalid: got %v want %v", len(b), width)
}
if cap(b) != capWidth {
t.Fatalf("bytepool length invalid: got %v want %v", cap(b), capWidth)
t.Fatalf("bytepool cap invalid: got %v want %v", cap(b), capWidth)
}
bufPool.Put(b)