Extract List reflect optimization

This commit is contained in:
scott 2023-05-27 18:54:34 -04:00 committed by Jordan Liggitt
parent b8a3bd673d
commit efc601302a
No known key found for this signature in database

View File

@ -214,8 +214,9 @@ func extractList(obj runtime.Object, allocNew bool) ([]runtime.Object, error) {
list := make([]runtime.Object, items.Len()) list := make([]runtime.Object, items.Len())
for i := range list { for i := range list {
raw := items.Index(i) raw := items.Index(i)
switch item := raw.Interface().(type) { switch {
case runtime.RawExtension: case raw.Type() == rawExtensionObjectType:
item := raw.Interface().(runtime.RawExtension)
switch { switch {
case item.Object != nil: case item.Object != nil:
list[i] = item.Object list[i] = item.Object
@ -225,8 +226,8 @@ func extractList(obj runtime.Object, allocNew bool) ([]runtime.Object, error) {
default: default:
list[i] = nil list[i] = nil
} }
case runtime.Object: case raw.Type().Implements(objectType):
list[i] = item list[i] = raw.Interface().(runtime.Object)
default: default:
var found bool var found bool
if list[i], found = raw.Addr().Interface().(runtime.Object); !found { if list[i], found = raw.Addr().Interface().(runtime.Object); !found {
@ -237,8 +238,12 @@ func extractList(obj runtime.Object, allocNew bool) ([]runtime.Object, error) {
return list, nil return list, nil
} }
var (
// objectSliceType is the type of a slice of Objects // objectSliceType is the type of a slice of Objects
var objectSliceType = reflect.TypeOf([]runtime.Object{}) objectSliceType = reflect.TypeOf([]runtime.Object{})
objectType = reflect.TypeOf((*runtime.Object)(nil)).Elem()
rawExtensionObjectType = reflect.TypeOf(runtime.RawExtension{})
)
// LenList returns the length of this list or 0 if it is not a list. // LenList returns the length of this list or 0 if it is not a list.
func LenList(list runtime.Object) int { func LenList(list runtime.Object) int {
@ -273,7 +278,7 @@ func SetList(list runtime.Object, objects []runtime.Object) error {
slice := reflect.MakeSlice(items.Type(), len(objects), len(objects)) slice := reflect.MakeSlice(items.Type(), len(objects), len(objects))
for i := range objects { for i := range objects {
dest := slice.Index(i) dest := slice.Index(i)
if dest.Type() == reflect.TypeOf(runtime.RawExtension{}) { if dest.Type() == rawExtensionObjectType {
dest = dest.FieldByName("Object") dest = dest.FieldByName("Object")
} }