-
Notifications
You must be signed in to change notification settings - Fork 0
/
bpool.go
83 lines (74 loc) · 1.51 KB
/
bpool.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
package sendremotefile
import (
"sync"
)
const (
OneMB uint = 1024 * 1024 * 1
FiveMB uint = 1024 * 1024 * 5
TenMB uint = 1024 * 1024 * 10
)
type bpool struct {
*sync.Map
}
func NewBytePool() *bpool {
var frameChunkedPool = &sync.Map{}
var preallocate = &sync.Once{}
preallocate.Do(internalAllocate(frameChunkedPool))
return &bpool{
frameChunkedPool,
}
}
func internalAllocate(frameChunkedPool *sync.Map) func() {
return func() {
pool1 := &sync.Pool{
New: func() any {
data := make([]byte, OneMB)
return &data
},
}
pool5 := &sync.Pool{
New: func() any {
data := make([]byte, FiveMB)
return &data
},
}
pool10 := &sync.Pool{
New: func() any {
data := make([]byte, TenMB)
return &data
},
}
frameChunkedPool.Store(OneMB, pool1)
frameChunkedPool.Store(FiveMB, pool5)
frameChunkedPool.Store(TenMB, pool10)
}
}
func (bpool *bpool) get(size uint) *[]byte {
switch {
case size <= OneMB:
val, _ := bpool.Load(OneMB)
return val.(*sync.Pool).Get().(*[]byte)
case size <= FiveMB:
val, _ := bpool.Load(FiveMB)
return val.(*sync.Pool).Get().(*[]byte)
default:
val, _ := bpool.Load(TenMB)
return val.(*sync.Pool).Get().(*[]byte)
}
}
func (bpool *bpool) put(size uint, data *[]byte) {
switch {
case size <= OneMB:
pool, _ := bpool.Load(OneMB)
pool.(*sync.Pool).Put(data)
return
case size <= FiveMB:
pool, _ := bpool.Load(FiveMB)
pool.(*sync.Pool).Put(data)
return
default:
pool, _ := bpool.Load(TenMB)
pool.(*sync.Pool).Put(data)
return
}
}