Skip to content

Commit

Permalink
Clarify BucketedPool contract, and add test case for case where min s…
Browse files Browse the repository at this point in the history
…ize is greater than 1.
  • Loading branch information
charleskorn committed Apr 2, 2024
1 parent 06ebc4d commit 7bb3ca8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/util/pool/bucketed_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/util/pool/bucketed_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 7bb3ca8

Please sign in to comment.