2017-08-25 03:36:43 -07:00
|
|
|
// 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
|
|
|
|
|
2018-02-13 12:44:51 -08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
)
|
2017-08-25 03:36:43 -07:00
|
|
|
|
2018-02-13 12:44:51 -08:00
|
|
|
// Pool is a bucketed pool for variably sized byte slices.
|
|
|
|
type Pool struct {
|
2017-08-25 03:36:43 -07:00
|
|
|
buckets []sync.Pool
|
|
|
|
sizes []int
|
2018-02-19 04:38:40 -08:00
|
|
|
// make is the function used to create an empty slice when none exist yet.
|
|
|
|
make func(int) interface{}
|
2017-08-25 03:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-02-13 12:44:51 -08:00
|
|
|
// New returns a new Pool with size buckets for minSize to maxSize
|
2017-08-25 03:36:43 -07:00
|
|
|
// increasing by the given factor.
|
2018-02-19 04:38:40 -08:00
|
|
|
func New(minSize, maxSize int, factor float64, makeFunc func(int) interface{}) *Pool {
|
2017-08-25 03:36:43 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-02-13 12:44:51 -08:00
|
|
|
p := &Pool{
|
2018-02-19 04:38:40 -08:00
|
|
|
buckets: make([]sync.Pool, len(sizes)),
|
|
|
|
sizes: sizes,
|
|
|
|
make: makeFunc,
|
2017-08-25 03:36:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns a new byte slices that fits the given size.
|
2018-02-13 12:44:51 -08:00
|
|
|
func (p *Pool) Get(sz int) interface{} {
|
2017-08-25 03:36:43 -07:00
|
|
|
for i, bktSize := range p.sizes {
|
|
|
|
if sz > bktSize {
|
|
|
|
continue
|
|
|
|
}
|
2018-02-13 12:44:51 -08:00
|
|
|
b := p.buckets[i].Get()
|
|
|
|
if b == nil {
|
2018-02-19 04:38:40 -08:00
|
|
|
b = p.make(bktSize)
|
2017-08-25 03:36:43 -07:00
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
2018-02-19 04:38:40 -08:00
|
|
|
return p.make(sz)
|
2017-08-25 03:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-02-13 12:44:51 -08:00
|
|
|
// Put adds a slice to the right bucket in the pool.
|
2018-02-19 04:31:04 -08:00
|
|
|
func (p *Pool) Put(s interface{}) {
|
2018-02-13 12:44:51 -08:00
|
|
|
slice := reflect.ValueOf(s)
|
2018-02-19 04:31:04 -08:00
|
|
|
|
|
|
|
if slice.Kind() != reflect.Slice {
|
|
|
|
panic(fmt.Sprintf("%+v is not a slice", slice))
|
|
|
|
}
|
|
|
|
for i, size := range p.sizes {
|
|
|
|
if slice.Cap() > size {
|
|
|
|
continue
|
2017-08-25 03:36:43 -07:00
|
|
|
}
|
2018-02-19 04:31:04 -08:00
|
|
|
p.buckets[i].Put(slice.Slice(0, 0).Interface())
|
2017-08-25 03:36:43 -07:00
|
|
|
}
|
|
|
|
}
|