Skip to content

Commit

Permalink
Perf improvement: process '-has' flag at the end (#37)
Browse files Browse the repository at this point in the history
* Perf improvement: process '-has' flag at the end

The '--has' flag is costly because it forces variables to be merged.
When looping over a lot of catalog items, it can slow down the command a
lot.

Move its processing at the last position when filtering catalog items, so it's
executed less often when used in combination of other flags.

* Fix tests
  • Loading branch information
fridim authored May 6, 2024
1 parent 6be6787 commit f752710
Showing 1 changed file with 53 additions and 27 deletions.
80 changes: 53 additions & 27 deletions cli/agnosticv.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,33 +424,6 @@ func findCatalogItems(workdir string, hasFlags []string, relatedFlags []string,
return nil
}

if len(hasFlags) > 0 {
logDebug.Println("hasFlags", hasFlags)
// Here we need yaml.v3 in order to use jmespath
merged, _, err := mergeVars(p, mergeStrategies)
if err != nil {
// Print the error and move to next file
logErr.Println(err)
return nil
}

for _, hasFlag := range hasFlags {
r, err := jmespath.Search(hasFlag, merged)
if err != nil {
logErr.Printf("ERROR: JMESPath '%q' not correct, %v", hasFlag, err)
return err
}

logDebug.Printf("merged=%#v\n", merged)
logDebug.Printf("r=%#v\n", r)

// If JMESPath expression does not match, skip file
if r == nil || r == false {
return nil
}
}
}

if len(relatedFlags) > 0 || len(orRelatedFlags) > 0 {
mergeList, err := getMergeList(pAbs)
related := extendMergeListWithRelated(pAbs, mergeList)
Expand All @@ -473,6 +446,32 @@ func findCatalogItems(workdir string, hasFlags []string, relatedFlags []string,
}

if containsPath(related, orRelatedAbs) {
if len(hasFlags) > 0 {
logDebug.Println("hasFlags", hasFlags)
// Here we need yaml.v3 in order to use jmespath
merged, _, err := mergeVars(p, mergeStrategies)
if err != nil {
// Print the error and move to next file
logErr.Println(err)
return nil
}

for _, hasFlag := range hasFlags {
r, err := jmespath.Search(hasFlag, merged)
if err != nil {
logErr.Printf("ERROR: JMESPath '%q' not correct, %v", hasFlag, err)
return err
}

logDebug.Printf("merged=%#v\n", merged)
logDebug.Printf("r=%#v\n", r)

// If JMESPath expression does not match, skip file
if r == nil || r == false {
return nil
}
}
}
// Add catalog item to result
result = append(result, p)
return nil
Expand All @@ -499,6 +498,33 @@ func findCatalogItems(workdir string, hasFlags []string, relatedFlags []string,
}
}

if len(hasFlags) > 0 {
logDebug.Println("hasFlags", hasFlags)
// Here we need yaml.v3 in order to use jmespath
merged, _, err := mergeVars(p, mergeStrategies)
if err != nil {
// Print the error and move to next file
logErr.Println(err)
return nil
}

for _, hasFlag := range hasFlags {
r, err := jmespath.Search(hasFlag, merged)
if err != nil {
logErr.Printf("ERROR: JMESPath '%q' not correct, %v", hasFlag, err)
return err
}

logDebug.Printf("merged=%#v\n", merged)
logDebug.Printf("r=%#v\n", r)

// If JMESPath expression does not match, skip file
if r == nil || r == false {
return nil
}
}
}

result = append(result, p)
return nil
})
Expand Down

0 comments on commit f752710

Please sign in to comment.