/
bytes_pool.go
161 lines (136 loc) · 3.32 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
package bytespool
import (
"math"
"math/bits"
"reflect"
"runtime"
"sync"
"unsafe"
)
const (
minCapacity = 1 << 0
smallBufferSize = 64
largeBufferSize = math.MaxInt32
defaultBufferSize = 8 << 20 // 8 MiB
)
var (
defaultMaxSize = defaultBufferSize
// 默认的各容量刻度字节数组池
defaultCapacityPools CapacityPools
)
type CapacityPools struct {
pools [32]sync.Pool
}
// SetMaxSize 设置回收时允许的最大字节
func SetMaxSize(size int) bool {
if size >= smallBufferSize && size <= largeBufferSize {
defaultMaxSize = size
return true
}
return false
}
// Make 返回 len 为 0, cap >= 给定值的 []byte
func (p *CapacityPools) Make(capacity int) []byte {
return p.Get(capacity)[:0]
}
// Get 完全同 New()
func (p *CapacityPools) Get(size int) []byte {
return p.New(size)
}
// New 返回指定长度的 []byte
// 注:
// 1. 返回的 buf != nil
// 2. 由于复用底层数组, buf 可能残留有旧数据
func (p *CapacityPools) New(size int) (buf []byte) {
if size < minCapacity {
return p.Make(minCapacity)
}
if size > defaultMaxSize {
return make([]byte, size, size)
}
idx := getIndex(size)
ptr, _ := p.pools[idx].Get().(unsafe.Pointer)
if ptr == nil {
return make([]byte, size, 1<<idx)
}
sh := (*reflect.SliceHeader)(unsafe.Pointer(&buf))
sh.Data = uintptr(ptr)
sh.Len = size
sh.Cap = 1 << idx
runtime.KeepAlive(ptr)
return
}
// NewBytes returns a byte slice of the specified content.
func (p *CapacityPools) NewBytes(bs []byte) []byte {
buf := p.Make(len(bs))
return append(buf, bs...)
}
// NewString returns a byte slice of the specified content.
func (p *CapacityPools) NewString(s string) []byte {
buf := p.Make(len(s))
return append(buf, s...)
}
// Append 与内置的 append 功能相同, 当底层数组需要重建时, 会回收原数组
func (p *CapacityPools) Append(buf []byte, elems ...byte) []byte {
return p.AppendString(buf, *(*string)(unsafe.Pointer(&elems)))
}
func (p *CapacityPools) AppendString(buf []byte, elems string) []byte {
n := len(buf)
c := cap(buf)
m := n + len(elems)
if c < m && c <= defaultMaxSize {
bbuf := p.Get(m)
copy(bbuf, buf)
copy(bbuf[n:], elems)
p.Put(buf)
return bbuf
}
return append(buf, elems...)
}
// Put 同 Release(), 不返回是否回收成功
func (p *CapacityPools) Put(buf []byte) {
p.Release(buf)
}
func (p *CapacityPools) Release(buf []byte) bool {
n := cap(buf)
if n == 0 || n > defaultMaxSize {
return false
}
idx := getIndex(n)
if n != 1<<idx {
idx--
}
// array pointer
p.pools[idx].Put(unsafe.Pointer(&buf[:1][0]))
return true
}
func getIndex(n int) int {
return bits.Len32(uint32(n) - 1)
}
func Make(size int) []byte {
return defaultCapacityPools.Make(size)
}
func Get(size int) []byte {
return defaultCapacityPools.Get(size)
}
func New(size int) []byte {
return defaultCapacityPools.New(size)
}
func NewBytes(bs []byte) []byte {
return defaultCapacityPools.NewBytes(bs)
}
func NewString(s string) []byte {
return defaultCapacityPools.NewString(s)
}
func Append(buf []byte, elems ...byte) []byte {
return defaultCapacityPools.Append(buf, elems...)
}
func AppendString(buf []byte, elems string) []byte {
return defaultCapacityPools.AppendString(buf, elems)
}
func Release(buf []byte) bool {
return defaultCapacityPools.Release(buf)
}
func Put(buf []byte) {
defaultCapacityPools.Put(buf)
}