-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathpool.go
51 lines (47 loc) · 983 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
package agogo
import (
"sync"
)
var iterPool = make(map[int]map[int]*sync.Pool)
func borrowIterator(m, n int) [][]float32 {
if d, ok := iterPool[m]; ok {
if d2, ok := d[n]; ok {
return d2.Get().([][]float32)
}
}
retVal := make([][]float32, m)
for i := range retVal {
retVal[i] = make([]float32, n)
}
return retVal
}
func ReturnIterator(m, n int, it [][]float32) {
if d, ok := iterPool[m]; ok {
if _, ok := d[n]; ok {
iterPool[m][n].Put(it)
} else {
iterPool[m][n] = &sync.Pool{
New: func() interface{} {
retVal := make([][]float32, m)
for i := range retVal {
retVal[i] = make([]float32, n)
}
return retVal
},
}
iterPool[m][n].Put(it)
}
} else {
iterPool[m] = make(map[int]*sync.Pool)
iterPool[m][n] = &sync.Pool{
New: func() interface{} {
retVal := make([][]float32, m)
for i := range retVal {
retVal[i] = make([]float32, n)
}
return retVal
},
}
iterPool[m][n].Put(it)
}
}