-
Notifications
You must be signed in to change notification settings - Fork 3
/
bytes.go
226 lines (180 loc) · 3.92 KB
/
bytes.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package buf
import (
"io"
"sync"
)
const (
bitSize = 32 << (^uint(0) >> 63)
maxIntHeadBit = 1 << (bitSize - 2)
)
type Bytes struct {
p *bytesPool
ri int
b []byte
}
func (bb *Bytes) Write(p []byte) (n int, err error) {
bb.b = append(bb.b, p...)
return len(p), nil
}
func (bb *Bytes) Read(p []byte) (n int, err error) {
if bb.ri >= len(bb.b)-1 {
return 0, io.EOF
}
n = copy(p, bb.b[bb.ri:])
bb.ri += n
return n, nil
}
func newBytes(p *bytesPool, n, c int) *Bytes {
if n > c {
panic("requested length is greater than capacity")
}
return &Bytes{p: p, b: make([]byte, n, c)}
}
func (bb *Bytes) Reset() {
bb.ri = 0
bb.b = bb.b[:bb.ri]
}
func (bb *Bytes) Bytes() *[]byte {
return &bb.b
}
func (bb *Bytes) SetBytes(b *[]byte) {
if b == nil {
return
}
bb.b = *b
}
func (bb *Bytes) Fill(data []byte, start, end int) {
copy(bb.b[start:end], data)
}
func (bb *Bytes) CopyFromWithOffset(data []byte, offset int) {
copy(bb.b[offset:], data)
}
func (bb *Bytes) CopyFrom(data []byte) {
copy(bb.b, data)
}
func (bb *Bytes) CopyTo(data []byte) []byte {
copy(data, bb.b)
return data
}
func (bb *Bytes) AppendFrom(data []byte) {
bb.b = append(bb.b, data...)
}
func (bb *Bytes) AppendTo(data []byte) []byte {
data = append(data, bb.b...)
return data
}
func (bb *Bytes) AppendByte(b byte) {
bb.b = append(bb.b, b)
}
func (bb *Bytes) AppendString(s string) {
bb.b = append(bb.b, s...)
}
func (bb *Bytes) Len() int {
return len(bb.b)
}
func (bb Bytes) Cap() int {
return cap(bb.b)
}
func (bb *Bytes) Release() {
bb.p.put(bb)
}
// bytesPool. contains logic of reusing objects distinguishable by size in generic
// way.
type bytesPool struct {
pool map[int]*sync.Pool
}
var defaultPool = NewBytesPool(32, 1<<20)
// NewBytesPool creates new bytesPool that reuses objects which size is in logarithmic range
// [min, max].
func NewBytesPool(min, max int) *bytesPool {
p := &bytesPool{
pool: make(map[int]*sync.Pool),
}
logarithmicRange(min, max, func(n int) {
p.pool[n] = &sync.Pool{}
})
return p
}
// Get returns probably reused slice of bytes with at least capacity of c and
// exactly len of n.
func (p *bytesPool) Get(n, c int) *Bytes {
if n > c {
panic("requested length is greater than capacity")
}
size := ceilToPowerOfTwo(c)
if pool := p.pool[size]; pool != nil {
v := pool.Get()
if v != nil {
bb := v.(*Bytes) //nolint:forcetypeassert
bb.b = bb.b[:n]
return bb
} else {
return newBytes(p, n, size)
}
}
return newBytes(p, n, c)
}
func Get(n, c int) *Bytes {
return defaultPool.Get(n, c)
}
// GetCap returns probably reused slice of bytes with at least capacity of n.
func (p *bytesPool) GetCap(c int) *Bytes {
return p.Get(0, c)
}
func GetCap(c int) *Bytes {
return defaultPool.Get(0, c)
}
// GetLen returns probably reused slice of bytes with at least capacity of n
// and exactly len of n.
func (p *bytesPool) GetLen(n int) *Bytes {
return p.Get(n, n)
}
func GetLen(n int) *Bytes {
return defaultPool.Get(n, n)
}
func FromBytes(in []byte) *Bytes {
b := defaultPool.GetCap(len(in))
b.AppendFrom(in)
return b
}
// put returns given Bytes to reuse pool.
// It does not reuse bytes whose size is not power of two or is out of pool
// min/max range.
func (p *bytesPool) put(bb *Bytes) {
if pool := p.pool[cap(bb.b)]; pool != nil {
pool.Put(bb)
}
}
// logarithmicRange iterates from ceil to power of two min to max,
// calling cb on each iteration.
func logarithmicRange(min, max int, cb func(int)) {
if min == 0 {
min = 1
}
for n := ceilToPowerOfTwo(min); n <= max; n <<= 1 {
cb(n)
}
}
// ceilToPowerOfTwo returns the least power of two integer value greater than
// or equal to n.
func ceilToPowerOfTwo(n int) int {
if n&maxIntHeadBit != 0 && n > maxIntHeadBit {
panic("argument is too large")
}
if n <= 2 {
return n
}
n--
n = fillBits(n)
n++
return n
}
func fillBits(n int) int {
n |= n >> 1
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16
n |= n >> 32
return n
}