Optimize reflect checks further, save item type information

This commit is contained in:
Jordan Liggitt 2023-05-27 19:10:13 -04:00
parent 12e3d9fcc4
commit bf116e8594
No known key found for this signature in database

View File

@ -223,10 +223,16 @@ func extractList(obj runtime.Object, allocNew bool) ([]runtime.Object, error) {
return nil, err return nil, err
} }
list := make([]runtime.Object, items.Len()) list := make([]runtime.Object, items.Len())
if len(list) == 0 {
return list, nil
}
elemType := items.Type().Elem()
isRawExtension := elemType == rawExtensionObjectType
implementsObject := elemType.Implements(objectType)
for i := range list { for i := range list {
raw := items.Index(i) raw := items.Index(i)
switch { switch {
case raw.Type() == rawExtensionObjectType: case isRawExtension:
item := raw.Interface().(runtime.RawExtension) item := raw.Interface().(runtime.RawExtension)
switch { switch {
case item.Object != nil: case item.Object != nil:
@ -237,7 +243,7 @@ func extractList(obj runtime.Object, allocNew bool) ([]runtime.Object, error) {
default: default:
list[i] = nil list[i] = nil
} }
case raw.Type().Implements(objectType): case implementsObject:
list[i] = raw.Interface().(runtime.Object) list[i] = raw.Interface().(runtime.Object)
case allocNew: case allocNew:
// shallow copy to avoid retaining a reference to the original list item // shallow copy to avoid retaining a reference to the original list item