mirror of https://github.com/prometheus/prometheus
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.0 KiB
88 lines
2.0 KiB
7 years ago
|
// 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
|
||
|
|
||
7 years ago
|
import (
|
||
|
"fmt"
|
||
|
"reflect"
|
||
|
"sync"
|
||
|
)
|
||
7 years ago
|
|
||
7 years ago
|
// Pool is a bucketed pool for variably sized byte slices.
|
||
|
type Pool struct {
|
||
7 years ago
|
buckets []sync.Pool
|
||
|
sizes []int
|
||
7 years ago
|
// make is the function used to create an empty slice when none exist yet.
|
||
|
make func(int) interface{}
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
// New returns a new Pool with size buckets for minSize to maxSize
|
||
7 years ago
|
// increasing by the given factor.
|
||
7 years ago
|
func New(minSize, maxSize int, factor float64, makeFunc func(int) interface{}) *Pool {
|
||
7 years ago
|
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)
|
||
|
}
|
||
|
|
||
7 years ago
|
p := &Pool{
|
||
7 years ago
|
buckets: make([]sync.Pool, len(sizes)),
|
||
|
sizes: sizes,
|
||
|
make: makeFunc,
|
||
7 years ago
|
}
|
||
|
|
||
|
return p
|
||
|
}
|
||
|
|
||
|
// Get returns a new byte slices that fits the given size.
|
||
7 years ago
|
func (p *Pool) Get(sz int) interface{} {
|
||
7 years ago
|
for i, bktSize := range p.sizes {
|
||
|
if sz > bktSize {
|
||
|
continue
|
||
|
}
|
||
7 years ago
|
b := p.buckets[i].Get()
|
||
|
if b == nil {
|
||
7 years ago
|
b = p.make(bktSize)
|
||
7 years ago
|
}
|
||
|
return b
|
||
|
}
|
||
7 years ago
|
return p.make(sz)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
// Put adds a slice to the right bucket in the pool.
|
||
7 years ago
|
func (p *Pool) Put(s interface{}) {
|
||
7 years ago
|
slice := reflect.ValueOf(s)
|
||
7 years ago
|
|
||
|
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
|
||
7 years ago
|
}
|
||
7 years ago
|
p.buckets[i].Put(slice.Slice(0, 0).Interface())
|
||
7 years ago
|
return
|
||
7 years ago
|
}
|
||
|
}
|