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())
for i := range list {
raw := items.Index(i)
switch item := raw.Interface().(type) {
case runtime.RawExtension:
switch {
case raw.Type() == rawExtensionObjectType:
item := raw.Interface().(runtime.RawExtension)
switch {
case item.Object != nil:
list[i] = item.Object
@ -225,8 +226,8 @@ func extractList(obj runtime.Object, allocNew bool) ([]runtime.Object, error) {
default:
list[i] = nil
}
case runtime.Object:
list[i] = item
case raw.Type().Implements(objectType):
list[i] = raw.Interface().(runtime.Object)
default:
var found bool
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
}
// objectSliceType is the type of a slice of Objects
var objectSliceType = reflect.TypeOf([]runtime.Object{})
var (
// objectSliceType is the type of a slice of Objects
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.
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))
for i := range objects {
dest := slice.Index(i)
if dest.Type() == reflect.TypeOf(runtime.RawExtension{}) {
if dest.Type() == rawExtensionObjectType {
dest = dest.FieldByName("Object")
}