/
pool.go
58 lines (45 loc) · 914 Bytes
/
pool.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 pool
import (
"bytes"
"math/big"
"sync"
)
var bytesBufferPool = sync.Pool{
New: allocBytesBuffer,
}
func allocBytesBuffer() interface{} {
return &bytes.Buffer{}
}
func GetBytesBuffer() *bytes.Buffer {
return bytesBufferPool.Get().(*bytes.Buffer)
}
func ReleaseBytesBuffer(b *bytes.Buffer) {
b.Reset()
bytesBufferPool.Put(b)
}
var bigIntPool = sync.Pool{
New: allocBigInt,
}
func allocBigInt() interface{} {
return &big.Int{}
}
func GetBigInt() *big.Int {
return bigIntPool.Get().(*big.Int)
}
func ReleaseBigInt(i *big.Int) {
bigIntPool.Put(i.SetInt64(0))
}
var keyToErrorMapPool = sync.Pool{
New: allocKeyToErrorMap,
}
func allocKeyToErrorMap() interface{} {
return make(map[string]error)
}
func GetKeyToErrorMap() map[string]error {
return keyToErrorMapPool.Get().(map[string]error)
}
func ReleaseKeyToErrorMap(m map[string]error) {
for key := range m {
delete(m, key)
}
}