Skip to content

Commit

Permalink
perf: do not use reflect in selector
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienaury committed Sep 19, 2024
1 parent 05cde85 commit c5ab707
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 28 deletions.
6 changes: 0 additions & 6 deletions pkg/model/ordered_dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,29 +188,23 @@ func Untyped(inter interface{}) interface{} {
return cleanmap
case []interface{}:
tab := []Entry{}

for _, item := range typedInter {
tab = append(tab, Untyped(item))
}

return tab

case []Dictionary:
tab := []interface{}{}

for _, item := range typedInter {
tab = append(tab, Untyped(item))
}

return tab

case []Entry:
tab := []interface{}{}

for _, item := range typedInter {
tab = append(tab, Untyped(item))
}

return tab

default:
Expand Down
36 changes: 14 additions & 22 deletions pkg/model/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,15 @@ func (s selector) applySub(root Dictionary, current Dictionary, appliers ...Appl
return false
}

v := reflect.ValueOf(entry)
kind := v.Kind()

if s.sub != nil {
if entry == nil {
return false
}

switch kind {
case reflect.Slice:
for i := 0; i < v.Len(); i++ {
switch typedV := v.Index(i).Interface().(type) {
switch typedEntry := entry.(type) {
case []Entry:
for i := 0; i < len(typedEntry); i++ {
switch typedV := typedEntry[i].(type) {
case Dictionary:
s.sub.applySub(root, typedV, appliers...)
default:
Expand All @@ -121,8 +118,8 @@ func (s selector) applySub(root Dictionary, current Dictionary, appliers ...Appl
}
}
return true
case reflect.Struct:
return s.sub.applySub(root, entry.(Dictionary), appliers...)
case Dictionary:
return s.sub.applySub(root, typedEntry, appliers...)
default:
// this handle the case where an intermediate path is a scalar or a null, instead of a dictionary
// e.g. :
Expand All @@ -132,13 +129,9 @@ func (s selector) applySub(root Dictionary, current Dictionary, appliers ...Appl
return false
}
}
switch kind {
case reflect.Slice:
actualSlice := []Entry{}
for i := 0; i < v.Len(); i++ {
actualSlice = append(actualSlice, v.Index(i).Interface())
}
s.applySlice(root, current, actualSlice, appliers)
switch typedEntry := entry.(type) {
case []Entry:
s.applySlice(root, current, typedEntry, appliers)
default:
s.apply(root, current, entry, appliers)
}
Expand Down Expand Up @@ -186,13 +179,12 @@ func (s selector) applySubContext(root Dictionary, current Dictionary, appliers
}
return true
}
v := reflect.ValueOf(entry)
kind := v.Kind()

if s.sub != nil {
switch kind {
case reflect.Slice:
for i := 0; i < v.Len(); i++ {
s.sub.applySubContext(root, v.Index(i).Interface().(Dictionary), appliers...)
switch typedEntry := entry.(type) {
case []Entry:
for i := 0; i < len(typedEntry); i++ {
s.sub.applySubContext(root, typedEntry[i].(Dictionary), appliers...)
}
return true
default:
Expand Down

0 comments on commit c5ab707

Please sign in to comment.