1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
package metrics
import (
"fmt"
"sync"
)
const capacity = 4
// The stringPool is used to avoid allocations when passing labels to Prometheus.
var stringPool = sync.Pool{New: func() any {
s := make([]string, 0, capacity)
return &s
}}
func getStringSlice() *[]string {
s := stringPool.Get().(*[]string)
*s = (*s)[:0]
return s
}
func putStringSlice(s *[]string) {
if c := cap(*s); c < capacity {
panic(fmt.Sprintf("unexpected slice cap: %d", c))
}
stringPool.Put(s)
}
|