-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcache.go
281 lines (262 loc) · 6.52 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package nject
import (
"reflect"
"sync"
"unicode"
"unicode/utf8"
)
type (
in3 [3]interface{}
in10 [10]interface{}
in30 [30]interface{}
in90 [90]interface{}
)
type cacherFunc func(in []reflect.Value) []reflect.Value
var (
cachers = make(map[int32]cacherFunc)
singletons = make(map[int32]cacherFunc)
lockLock sync.RWMutex
)
// canSimpleTypeBeMapKey cannot handle structs or interfaces
func canSimpleTypeBeMapKey(t reflect.Type) bool {
switch t.Kind() {
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128, reflect.Array,
reflect.Ptr, reflect.String:
return true
case reflect.Chan, reflect.Func, reflect.Map,
reflect.Slice, reflect.UnsafePointer:
return false
case reflect.Interface, reflect.Struct, reflect.Invalid:
fallthrough
default:
// we shouldn't be here
return false
}
}
func canValueBeMapKey(v reflect.Value, recurseOkay bool) bool {
if !v.IsValid() {
// this is actually nil, but we can't do Interface on it so supporting
// typed nils is too hard
return false
}
// nolint:exhaustive
switch v.Type().Kind() {
case reflect.Interface:
if v.IsNil() {
return true
}
if !recurseOkay {
return false
}
// Is this right?
if !v.CanInterface() {
return false
}
iface := v.Interface()
value := reflect.ValueOf(iface)
return canValueBeMapKey(value, false)
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
if !canValueBeMapKey(v.Field(i), true) {
return false
}
}
return true
default:
return canSimpleTypeBeMapKey(v.Type())
}
}
// canBeMapKey examines an array of types. If any of those types cannot
// be a map key, then it returns false. If the ability for those types
// to be a map key cannot be determined just by looking at the types, then
// it returns true and also returns a function that given an array of
// values, determines if the values can be map keys.
func canBeMapKey(in []reflect.Type) (bool, func([]reflect.Value) bool) {
var checkers []func([]reflect.Value) bool
for i, t := range in {
i := i
// nolint:exhaustive
switch t.Kind() {
case reflect.Struct:
for j := 0; j < t.NumField(); j++ {
f := t.Field(j)
ok, check := canBeMapKey([]reflect.Type{f.Type})
if !ok {
return false, nil
}
if check != nil {
r, _ := utf8.DecodeRuneInString(f.Name)
if unicode.IsLower(r) {
// If the type doesn't make it clear that the field
// is acceptable as a map key (eg: it's an interface)
// then since the field isn't exported, we cannot get
// the value and thus we cannot use it as part of a
// map key
return false, nil
}
checkers = append(checkers, func(in []reflect.Value) bool {
return check([]reflect.Value{in[i].FieldByIndex(f.Index)})
})
}
}
case reflect.Interface:
// We cannot determine the map key compatibility of interface types. They
// may be okay, or they may not be okay. The actual type type will have to be
// checked once we have a value.
checkers = append(checkers, func(in []reflect.Value) bool {
return canValueBeMapKey(in[i], true)
})
default:
if !canSimpleTypeBeMapKey(t) {
return false, nil
}
}
}
switch len(checkers) {
case 0:
return true, nil
case 1:
return true, checkers[0]
default:
return true, func(in []reflect.Value) bool {
for _, f := range checkers {
if !f(in) {
return false
}
}
return true
}
}
}
func generateLookup(fm *provider, fv canCall, numInputs int) cacherFunc {
if fm.memoized {
return generateCache(fm.id, fv, numInputs, fm.mapKeyCheck)
}
if fm.singleton {
return generateSingleton(fm.id, fv)
}
return nil
}
func generateSingleton(id int32, fv canCall) cacherFunc {
lockLock.Lock()
defer lockLock.Unlock()
if singleton, ok := singletons[id]; ok {
return singleton
}
var once sync.Once
var out []reflect.Value
singleton := func(in []reflect.Value) []reflect.Value {
once.Do(func() {
out = fv.Call(in)
})
return out
}
singletons[id] = singleton
return singleton
}
func generateCache(id int32, fv canCall, l int, okayCheck func([]reflect.Value) bool) cacherFunc {
lockLock.Lock()
defer lockLock.Unlock()
if cacher, ok := cachers[id]; ok {
return cacher
}
cacher := defineCacher(id, fv, l, okayCheck)
cachers[id] = cacher
return cacher
}
func fillKeyFromInputs(key []interface{}, in []reflect.Value) {
for i, v := range in {
if !v.IsValid() {
key[i] = ""
continue
}
if v.Type().Kind() == reflect.Interface && v.IsNil() {
key[i] = ""
continue
}
key[i] = v.Interface()
}
for i := len(in); i < len(key); i++ {
key[i] = ""
}
}
func defineCacher(_ int32, fv canCall, l int, okayCheck func([]reflect.Value) bool) cacherFunc {
var lock sync.Mutex
switch {
case l <= 3:
cache := make(map[in3][]reflect.Value)
return func(in []reflect.Value) []reflect.Value {
if okayCheck != nil && !okayCheck(in) {
return fv.Call(in)
}
lock.Lock()
defer lock.Unlock()
var key in3
fillKeyFromInputs(key[:], in)
if out, found := cache[key]; found {
return out
}
out := fv.Call(in)
cache[key] = out
return out
}
case l <= 10:
cache := make(map[in10][]reflect.Value)
return func(in []reflect.Value) []reflect.Value {
if okayCheck != nil && !okayCheck(in) {
return fv.Call(in)
}
lock.Lock()
defer lock.Unlock()
var key in10
fillKeyFromInputs(key[:], in)
if out, found := cache[key]; found {
return out
}
out := fv.Call(in)
cache[key] = out
return out
}
case l <= 30:
cache := make(map[in30][]reflect.Value)
return func(in []reflect.Value) []reflect.Value {
if okayCheck != nil && !okayCheck(in) {
return fv.Call(in)
}
lock.Lock()
defer lock.Unlock()
var key in30
fillKeyFromInputs(key[:], in)
if out, found := cache[key]; found {
return out
}
out := fv.Call(in)
cache[key] = out
return out
}
case l <= 90:
cache := make(map[in90][]reflect.Value)
return func(in []reflect.Value) []reflect.Value {
if okayCheck != nil && !okayCheck(in) {
return fv.Call(in)
}
lock.Lock()
defer lock.Unlock()
var key in90
fillKeyFromInputs(key[:], in)
if out, found := cache[key]; found {
return out
}
out := fv.Call(in)
cache[key] = out
return out
}
default:
debugf("number of arguments exceeds maximum! %d", l)
return func(in []reflect.Value) []reflect.Value {
return fv.Call(in)
}
}
}