-
Notifications
You must be signed in to change notification settings - Fork 1
/
bytes_pool.go
194 lines (161 loc) 路 3.65 KB
/
bytes_pool.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package bytespool
import (
"math"
"reflect"
"runtime"
"sync"
"unsafe"
)
const (
minCapacity = 2
defaultMinSize = 2
defaultMaxSize = 8192
)
var defaultCapacityPools = NewCapacityPools(defaultMinSize, defaultMaxSize)
type CapacityPools struct {
minSize int
maxSize int
maxIndex int
pools []*bytesPool
}
type bytesPool struct {
capacity int
pool sync.Pool
}
// InitDefaultPools initialize to the default pool.
func InitDefaultPools(minSize, maxSize int) {
defaultCapacityPools = NewCapacityPools(minSize, maxSize)
}
// NewCapacityPools divide into multiple pools according to the capacity scale.
func NewCapacityPools(minSize, maxSize int) *CapacityPools {
var pools []*bytesPool
if minSize < minCapacity {
minSize = minCapacity
}
if maxSize < minSize {
maxSize = minSize
}
for i := minSize; i < maxSize; i *= 2 {
pools = append(pools, newBytesPool(i))
}
pools = append(pools, newBytesPool(maxSize))
return &CapacityPools{
minSize: minSize,
maxSize: maxSize,
maxIndex: len(pools) - 1,
pools: pools,
}
}
func newBytesPool(size int) *bytesPool {
return &bytesPool{
capacity: size,
pool: sync.Pool{
New: func() interface{} {
buf := make([]byte, size, size)
return &buf
},
},
}
}
// Make return an empty bytes pointer variable.
// Length is 0, default capacity is maxSize.
func (p *CapacityPools) Make(capacity int) []byte {
return p.New(capacity)[:0]
}
func (p *CapacityPools) Make64(capacity uint64) []byte {
return p.New(int(capacity))[:0]
}
func (p *CapacityPools) MakeMax() []byte {
return p.New(p.maxSize)[:0]
}
func (p *CapacityPools) MakeMin() []byte {
return p.New(p.minSize)[:0]
}
// New return bytes of the specified size.
// Length is size, may contain old data.
func (p *CapacityPools) New(size int) (buf []byte) {
bp := p.getPool(size)
if bp == nil {
return make([]byte, size)
}
ptr, _ := bp.pool.Get().(unsafe.Pointer)
if ptr == nil {
return make([]byte, size, bp.capacity)
}
slice := (*reflect.SliceHeader)(unsafe.Pointer(&buf))
slice.Data = uintptr(ptr)
slice.Len = size
slice.Cap = bp.capacity
runtime.KeepAlive(ptr)
return
}
func (p *CapacityPools) New64(size uint64) []byte {
return p.New(int(size))
}
func (p *CapacityPools) NewMax() []byte {
return p.New(p.maxSize)
}
func (p *CapacityPools) NewMin() []byte {
return p.New(p.minSize)
}
// Release put it back into the pool of the corresponding scale.
// Discard buffer larger than the maximum capacity.
func (p *CapacityPools) Release(buf []byte) bool {
n := cap(buf)
if n == 0 || n > p.maxSize {
return false
}
bp := p.getPool(n)
if n != bp.capacity {
return false
}
// array pointer
bp.pool.Put(unsafe.Pointer(&buf[:1][0]))
return true
}
func (p *CapacityPools) getPool(size int) *bytesPool {
if size <= p.minSize {
return p.pools[0]
}
if size == p.maxSize {
return p.pools[p.maxIndex]
}
if size > p.maxSize {
return nil
}
idx := int(math.Ceil(math.Log2(float64(size) / float64(p.minSize))))
if idx < 0 {
idx = 0
}
if idx > p.maxIndex {
return nil
}
return p.pools[idx]
}
func Make(size int) []byte {
return defaultCapacityPools.Make(size)
}
func Make64(size uint64) []byte {
return defaultCapacityPools.Make64(size)
}
func MakeMax() []byte {
return defaultCapacityPools.MakeMax()
}
func MakeMin() []byte {
return defaultCapacityPools.MakeMin()
}
func New(size int) []byte {
return defaultCapacityPools.New(size)
}
func New64(size uint64) []byte {
return defaultCapacityPools.New64(size)
}
func NewMax() []byte {
return defaultCapacityPools.NewMax()
}
func NewMin() []byte {
return defaultCapacityPools.NewMin()
}
func Release(buf []byte) bool {
return defaultCapacityPools.Release(buf)
}