-
Notifications
You must be signed in to change notification settings - Fork 248
/
pool.go
56 lines (48 loc) · 843 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
package iavl
import (
"math"
"sync"
)
type NodePool struct {
syncPool *sync.Pool
free chan int
nodes []Node
poolId uint64
}
func NewNodePool() *NodePool {
np := &NodePool{
syncPool: &sync.Pool{
New: func() interface{} {
return &Node{}
},
},
free: make(chan int, 1000),
}
return np
}
func (np *NodePool) Get() *Node {
if np.poolId == math.MaxUint64 {
np.poolId = 1
} else {
np.poolId++
}
n := np.syncPool.Get().(*Node)
n.poolId = np.poolId
return n
}
func (np *NodePool) Put(node *Node) {
node.leftNodeKey = emptyNodeKey
node.rightNodeKey = emptyNodeKey
node.rightNode = nil
node.leftNode = nil
node.nodeKey = emptyNodeKey
node.hash = nil
node.key = nil
node.value = nil
node.subtreeHeight = 0
node.size = 0
node.dirty = false
node.evict = false
node.poolId = 0
np.syncPool.Put(node)
}