-
Notifications
You must be signed in to change notification settings - Fork 19
/
bufpool.go
149 lines (136 loc) · 3.2 KB
/
bufpool.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
package goev
import (
"sync"
"sync/atomic"
"time"
)
// Use sync.Pool for underlying memory reuse and employs finer-grained granularity management.
const (
bufPoolMaxMBytes int = 16
bufPoolActiveTimes int32 = 5 // Active is enable, inactive is disable, periodic dynamic adjustment
)
var (
// 0 [1, 128*1] 128*1
// 1 [128*1+1, 128*2] 128*2
// 2 [128*2+1, 128*3] 128*3
// ...
// 6 [128*6+1, 128*7] 128*7
btPool [7]buffPool
// 0 [128*7+1, 1024*1] 1K
// 1 [1024*1+1, 1024*2] 2K
// 2 [1024*2+1), 1024*3] 3K
// ...
// 1022 [1024*1022+1), 1024*1023] 1023K
kbPool [1023]buffPool
// 0 [1024*1023+1, 1024*1024*1] 1M
// 1 [1024*1024*1+1, 1024*1024*2] 2M
// 2 [1024*1024*2+1, 1024*1024*3] 3M
// ...
// 15 (1024*1024*15+1, 1024*1024*16] 16M
mbPool [bufPoolMaxMBytes]buffPool // 1024K x n <= 16M
)
func init() {
for i := 0; i < len(btPool); i++ {
btPool[i].init((i + 1) * 128)
}
for i := 0; i < len(kbPool); i++ {
kbPool[i].init((i + 1) * 1024)
}
for i := 0; i < len(mbPool); i++ {
mbPool[i].init((i + 1) * 1024 * 1024)
}
go buffPoolAdjustTiming(time.Minute * 2)
}
// BMalloc allocates memory of the specified size and maximizes the utilization of memory reuse
//
// Returned []byte, with len representing the actual requested size,
// and cap being greater than or equal to the size.
// NOTE the []byte buffer cannot use 'append'
func BMalloc(s int) []byte {
if s < 1 {
return make([]byte, 0)
}
if s < (128*7 + 1) {
i := (s+127)/128 - 1
return btPool[i].alloc(s)
} else if s < (1024*1024 + 1) {
i := (s+1023)/1024 - 1
return kbPool[i].alloc(s)
} else if s < (1024*1024*bufPoolMaxMBytes + 1) {
i := (s+(1024*1024-1))/(1024*1024) - 1
return mbPool[i].alloc(s)
}
return make([]byte, s)
}
// BFree free memory
func BFree(bf []byte) {
s := cap(bf)
if s < (128*7 + 1) {
i := (s+127)/128 - 1
btPool[i].free(s, bf)
} else if s < (1024*1024 + 1) {
i := (s+1023)/1024 - 1
kbPool[i].free(s, bf)
} else if s < (1024*1024*bufPoolMaxMBytes + 1) {
i := (s+(1024*1024-1))/(1024*1024) - 1
mbPool[i].free(s, bf)
}
}
func buffPoolAdjustTiming(period time.Duration) {
ticker := time.NewTicker(period)
defer ticker.Stop()
for {
select {
case <-ticker.C:
buffPoolAdjust()
}
}
}
func buffPoolAdjust() {
for i := 0; i < len(btPool); i++ {
btPool[i].adjust()
}
for i := 0; i < len(kbPool); i++ {
kbPool[i].adjust()
}
for i := 0; i < len(mbPool); i++ {
mbPool[i].adjust()
}
}
type buffPool struct {
disabled int32
allocTimes int32
size int
pool sync.Pool
}
func (bp *buffPool) init(size int) {
bp.size = size
bp.pool.New = func() any { return make([]byte, size) }
}
func (bp *buffPool) alloc(s int) []byte {
atomic.AddInt32(&(bp.allocTimes), 1)
if atomic.LoadInt32(&(bp.disabled)) == 1 {
return make([]byte, s)
}
bf := bp.pool.Get().([]byte)
if s == bp.size {
return bf
}
return bf[:s]
}
func (bp *buffPool) free(c int, bf []byte) {
if c == bp.size {
if len(bf) < c {
bp.pool.Put(bf[:c])
return
}
bp.pool.Put(bf)
}
}
func (bp *buffPool) adjust() {
if atomic.SwapInt32(&(bp.allocTimes), 0) < bufPoolActiveTimes {
atomic.CompareAndSwapInt32(&(bp.disabled), 0, 1)
} else {
atomic.CompareAndSwapInt32(&(bp.disabled), 1, 0)
}
}