-
Notifications
You must be signed in to change notification settings - Fork 1
/
bytespool.go
69 lines (60 loc) · 1.39 KB
/
bytespool.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
package binaryutil
import (
"github.com/fufuok/bytespool"
"math"
)
// BytesPool 字节对象池,用于减少GC提高编解码性能
var BytesPool = bytespool.NewCapacityPools(32, math.MaxInt32)
// MakeRecycleBytes 创建可回收字节对象
func MakeRecycleBytes(bytes []byte) RecycleBytes {
return RecycleBytes{
data: bytes,
low: 0,
high: len(bytes),
recyclable: true,
}
}
// MakeNonRecycleBytes 创建不可回收字节对象
func MakeNonRecycleBytes(bytes []byte) RecycleBytes {
return RecycleBytes{
data: bytes,
low: 0,
high: len(bytes),
recyclable: false,
}
}
// SliceRecycleBytes 切片操作字节对象
func SliceRecycleBytes(bytes RecycleBytes, low, high int) RecycleBytes {
if low < 0 || high < 0 {
panic("negative index")
}
if high == 0 {
high = len(bytes.data) - bytes.low
}
return RecycleBytes{
data: bytes.data,
low: bytes.low + low,
high: bytes.low + high,
recyclable: bytes.recyclable,
}
}
// RecycleBytes 可回收字节对象
type RecycleBytes struct {
data []byte
low, high int
recyclable bool
}
// Data 数据
func (b RecycleBytes) Data() []byte {
return b.data[b.low:b.high]
}
// Recyclable 可否回收
func (b RecycleBytes) Recyclable() bool {
return b.recyclable
}
// Release 释放字节对象
func (b RecycleBytes) Release() {
if b.recyclable {
BytesPool.Put(b.data)
}
}