diff --git a/pkg/util/pool/bucketed_pool.go b/pkg/util/pool/bucketed_pool.go index d0be7127bfd..6c10d884e44 100644 --- a/pkg/util/pool/bucketed_pool.go +++ b/pkg/util/pool/bucketed_pool.go @@ -47,7 +47,7 @@ func NewBucketedPool[T ~[]E, E any](minSize, maxSize int, factor float64, makeFu return p } -// Get returns a new slice that fits the given size. +// Get returns a new slice with capacity greater than or equal to size. func (p *BucketedPool[T, E]) Get(size int) T { for i, bktSize := range p.sizes { if size > bktSize { @@ -63,8 +63,9 @@ func (p *BucketedPool[T, E]) Get(size int) T { } // Put adds a slice to the right bucket in the pool. +// If the slice does not belong to any bucket in the pool, it is ignored. func (p *BucketedPool[T, E]) Put(s T) { - if cap(s) == 0 { + if cap(s) < p.sizes[0] { return } diff --git a/pkg/util/pool/bucketed_pool_test.go b/pkg/util/pool/bucketed_pool_test.go index 79d7f007a0d..abafd1c089b 100644 --- a/pkg/util/pool/bucketed_pool_test.go +++ b/pkg/util/pool/bucketed_pool_test.go @@ -54,3 +54,10 @@ func TestBucketedPool_PutEmptySlice(t *testing.T) { s := pool.Get(1) require.GreaterOrEqual(t, cap(s), 1) } + +func TestBucketedPool_PutSliceSmallerThanMinimum(t *testing.T) { + pool := NewBucketedPool(3, 1000, 10, makeFunc) + pool.Put([]int{1, 2}) + s := pool.Get(3) + require.GreaterOrEqual(t, cap(s), 3) +}