ceremonyclient/go-libp2p/p2p/metricshelper/pool.go

27 lines
439 B
Go
Raw Normal View History

2023-08-21 03:50:38 +00:00
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)
}