-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpools.go
67 lines (56 loc) · 1.14 KB
/
pools.go
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package metrics
import (
"bytes"
"sync"
)
var (
memoryReuse = true
)
// SetMemoryReuseEnabled defines if memory reuse will be enabled (default -- enabled).
func SetMemoryReuseEnabled(isEnabled bool) {
memoryReuse = isEnabled
}
type bytesBuffer struct {
bytes.Buffer
}
type stringSlice []string
func (p stringSlice) Len() int { return len(p) }
func (p stringSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p stringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
var (
bytesBufferPool = &sync.Pool{
New: func() interface{} {
return &bytesBuffer{}
},
}
stringSlicePool = &sync.Pool{
New: func() interface{} {
return &stringSlice{}
},
}
metricCountPool = &sync.Pool{
New: func() interface{} {
return &MetricCount{}
},
}
)
func newBytesBuffer() *bytesBuffer {
return bytesBufferPool.Get().(*bytesBuffer)
}
func (buf *bytesBuffer) Release() {
if !memoryReuse {
return
}
buf.Reset()
bytesBufferPool.Put(buf)
}
func newStringSlice() *stringSlice {
return stringSlicePool.Get().(*stringSlice)
}
func (s *stringSlice) Release() {
if !memoryReuse {
return
}
*s = (*s)[:0]
stringSlicePool.Put(s)
}