File: pool.go

package info (click to toggle)
golang-github-lucas-clemente-quic-go 0.54.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,312 kB
  • sloc: sh: 54; makefile: 7
file content (27 lines) | stat: -rw-r--r-- 481 bytes parent folder | download | duplicates (3)
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)
}