ceremonyclient/go-libp2p/p2p/metricshelper/pool.go
2023-08-20 23:07:43 -05:00

27 lines
439 B
Go

package metricshelper
import (
"fmt"
"sync"
)
const capacity = 8
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("expected a string slice with capacity 8 or greater, got %d", c))
}
stringPool.Put(s)
}