-
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81ceb1a
commit bac08fd
Showing
5 changed files
with
223 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2017 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package pool | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// BucketedPool is a bucketed pool for variably sized slices. | ||
type BucketedPool[T any] struct { | ||
buckets []sync.Pool | ||
sizes []int | ||
// make is the function used to create an empty slice when none exist yet. | ||
make func(int) []T | ||
} | ||
|
||
// NewBucketedPool returns a new BucketedPool with size buckets for minSize to maxSize | ||
// increasing by the given factor. | ||
func NewBucketedPool[T any](minSize, maxSize int, factor float64, makeFunc func(int) []T) *BucketedPool[T] { | ||
if minSize < 1 { | ||
panic("invalid minimum pool size") | ||
} | ||
if maxSize < 1 { | ||
panic("invalid maximum pool size") | ||
} | ||
if factor < 1 { | ||
panic("invalid factor") | ||
} | ||
|
||
var sizes []int | ||
|
||
for s := minSize; s <= maxSize; s = int(float64(s) * factor) { | ||
sizes = append(sizes, s) | ||
} | ||
|
||
p := &BucketedPool[T]{ | ||
buckets: make([]sync.Pool, len(sizes)), | ||
sizes: sizes, | ||
make: makeFunc, | ||
} | ||
return p | ||
} | ||
|
||
// Get returns a new slice with capacity greater than or equal to size. | ||
func (p *BucketedPool[T]) Get(size int) []T { | ||
for i, bktSize := range p.sizes { | ||
if size > bktSize { | ||
continue | ||
} | ||
buff := p.buckets[i].Get() | ||
if buff == nil { | ||
buff = p.make(bktSize) | ||
} | ||
return buff.([]T) | ||
} | ||
return p.make(size) | ||
} | ||
|
||
// 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]) Put(s []T) { | ||
sCap := cap(s) | ||
if sCap < p.sizes[0] { | ||
return | ||
} | ||
|
||
for i, size := range p.sizes { | ||
if sCap > size { | ||
continue | ||
} | ||
|
||
if sCap == size { | ||
// Buffer is exactly the minimum size for this bucket. Add it to this bucket. | ||
p.buckets[i].Put(s) | ||
} else { | ||
// Buffer belongs in previous bucket. | ||
p.buckets[i-1].Put(s) | ||
} | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Provenance-includes-location: https://github.com/prometheus/prometheus/blob/main/util/pool/pool_test.go | ||
// Provenance-includes-copyright: The Prometheus Authors | ||
|
||
package pool | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func makeFunc(size int) []int { | ||
return make([]int, 0, size) | ||
} | ||
|
||
func TestBucketedPool_HappyPath(t *testing.T) { | ||
testPool := NewBucketedPool(1, 8, 2, makeFunc) | ||
cases := []struct { | ||
size int | ||
expectedCap int | ||
}{ | ||
{ | ||
size: -1, | ||
expectedCap: 1, | ||
}, | ||
{ | ||
size: 3, | ||
expectedCap: 4, | ||
}, | ||
{ | ||
size: 10, | ||
expectedCap: 10, | ||
}, | ||
} | ||
for _, c := range cases { | ||
ret := testPool.Get(c.size) | ||
if cap(ret) < c.expectedCap { | ||
t.Fatalf("expected cap >= %d, got %d", c.expectedCap, cap(ret)) | ||
} | ||
testPool.Put(ret) | ||
} | ||
} | ||
|
||
func TestBucketedPool_SliceNotAlignedToBuckets(t *testing.T) { | ||
pool := NewBucketedPool(1, 1000, 10, makeFunc) | ||
pool.Put(make([]int, 0, 2)) | ||
s := pool.Get(3) | ||
if cap(s) < 3 { | ||
t.Fatalf("expected cap >= 3, got %d", cap(s)) | ||
} | ||
} | ||
|
||
func TestBucketedPool_PutEmptySlice(t *testing.T) { | ||
pool := NewBucketedPool(1, 1000, 10, makeFunc) | ||
pool.Put([]int{}) | ||
s := pool.Get(1) | ||
if cap(s) < 1 { | ||
t.Fatalf("expected cap >= 1, got %d", cap(s)) | ||
} | ||
} | ||
|
||
func TestBucketedPool_PutSliceSmallerThanMinimum(t *testing.T) { | ||
pool := NewBucketedPool(3, 1000, 10, makeFunc) | ||
pool.Put([]int{1, 2}) | ||
s := pool.Get(3) | ||
if cap(s) < 3 { | ||
t.Fatalf("expected cap >= 3, got %d", cap(s)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package kgo | ||
|
||
import ( | ||
"bytes" | ||
"sync" | ||
) | ||
|
||
// shared general purpose byte buff | ||
var byteBuffers = sync.Pool{New: func() any { return bytes.NewBuffer(make([]byte, 8<<10)) }} |