Skip to content

Commit 2783351

Browse files
authored
Merge pull request #29 from haya14busa/avoid-reflect
avoid using reflect and reduce allocs
2 parents 866744c + 245dd08 commit 2783351

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

go/vimlfunc.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,20 @@ func viml_string(obj interface{}) string {
147147
}
148148

149149
func viml_has_key(obj interface{}, key interface{}) bool {
150+
// Avoid using reflect as much as possible by listing type used as obj and
151+
// use type switch.
152+
switch o := obj.(type) {
153+
case map[string]*Cmd:
154+
_, ok := o[key.(string)]
155+
return ok
156+
case map[string]interface{}:
157+
_, ok := o[key.(string)]
158+
return ok
159+
case map[int][]interface{}:
160+
_, ok := o[key.(int)]
161+
return ok
162+
}
163+
// fallback to reflect. Shoul be unreachable.
150164
m := reflect.ValueOf(obj)
151165
v := m.MapIndex(reflect.ValueOf(key))
152166
return v.Kind() != reflect.Invalid

0 commit comments

Comments
 (0)