-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcache.go
47 lines (40 loc) · 959 Bytes
/
cache.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
package jsonpath
import (
"sort"
"sync"
)
var sortSliceSyncPool = &sync.Pool{
New: func() interface{} { return new(sort.StringSlice) },
}
var resultSyncPool = &sync.Pool{
New: func() interface{} { return new(bufferContainer) },
}
func getSortedKeys(srcMap map[string]interface{}) *sort.StringSlice {
length := len(srcMap)
sortKeys := sortSliceSyncPool.Get().(*sort.StringSlice)
if cap(*sortKeys) < length {
*sortKeys = make(sort.StringSlice, length)
}
*sortKeys = (*sortKeys)[:length]
index := 0
for key := range srcMap {
(*sortKeys)[index] = key
index++
}
if length > 1 {
sortKeys.Sort()
}
return sortKeys
}
func putSortSlice(sortKeys *sort.StringSlice) {
if sortKeys != nil {
sortSliceSyncPool.Put(sortKeys)
}
}
func getContainer() *bufferContainer {
return resultSyncPool.Get().(*bufferContainer)
}
func putContainer(container *bufferContainer) {
container.result = container.result[:0]
resultSyncPool.Put(container)
}