Skip to content

Commit

Permalink
Refactor some optimizations
Browse files Browse the repository at this point in the history
Some of the logic in the `checkListsMatch` function does not require the
copies of the inputs to be created or sorted. This does those checks
first, potentially skipping the unnecessary work.

Signed-off-by: Justin Kulikauskas <jkulikau@redhat.com>
  • Loading branch information
JustinKuli authored and openshift-ci[bot] committed Oct 12, 2023
1 parent eafaef3 commit d6807db
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions controllers/configurationpolicy_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,24 +300,25 @@ func checkListFieldsWithSort(mergedObj []map[string]interface{}, oldObj []map[st

// checkListsMatch is a generic list check that uses an arbitrary sort to ensure it is comparing the right values
func checkListsMatch(oldVal []interface{}, mergedVal []interface{}) (m bool) {
oVal := append([]interface{}{}, oldVal...)
mVal := append([]interface{}{}, mergedVal...)
if (oldVal == nil && mergedVal != nil) || (oldVal != nil && mergedVal == nil) {
return false
}

if (oVal == nil && mVal != nil) || (oVal != nil && mVal == nil) {
if len(mergedVal) != len(oldVal) {
return false
}

// Make copies of the lists, so we can sort them without mutating this function's inputs
oVal := append([]interface{}{}, oldVal...)
mVal := append([]interface{}{}, mergedVal...)

sort.Slice(oVal, func(i, j int) bool {
return fmt.Sprintf("%v", oVal[i]) < fmt.Sprintf("%v", oVal[j])
})
sort.Slice(mVal, func(x, y int) bool {
return fmt.Sprintf("%v", mVal[x]) < fmt.Sprintf("%v", mVal[y])
})

if len(mVal) != len(oVal) {
return false
}

for idx, oNestedVal := range oVal {
switch oNestedVal := oNestedVal.(type) {
case map[string]interface{}:
Expand Down

0 comments on commit d6807db

Please sign in to comment.