-
Notifications
You must be signed in to change notification settings - Fork 11
/
bufferpool.go
58 lines (50 loc) · 997 Bytes
/
bufferpool.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
package roulette
import (
"bytes"
"sync"
)
// wrapper over a bytes buffer pool
type bytesPool struct {
sp sync.Pool
}
func newBytesPool() *bytesPool {
return &bytesPool{
sp: sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
},
}
}
func (pool *bytesPool) get() *bytes.Buffer {
return pool.sp.Get().(*bytes.Buffer)
}
func (pool *bytesPool) put(buffer *bytes.Buffer) {
buffer.Reset()
pool.sp.Put(buffer)
}
// wrapper over a map[string]interface{} pool
type mapPool struct {
sp sync.Pool
}
func newMapPool() *mapPool {
return &mapPool{
sp: sync.Pool{
New: func() interface{} {
return make(map[string]interface{})
},
},
}
}
func (pool *mapPool) get() map[string]interface{} {
return pool.sp.Get().(map[string]interface{})
}
func (pool *mapPool) put(buffer map[string]interface{}) {
pool.sp.Put(buffer)
}
func (pool *mapPool) putReset(buffer map[string]interface{}) {
for k := range buffer {
delete(buffer, k)
}
pool.sp.Put(buffer)
}