From ea28081f3535202188e6c4cc44d35299e730f78a Mon Sep 17 00:00:00 2001 From: homily707 Date: Thu, 21 Jul 2022 00:39:02 +0800 Subject: [PATCH] refactor: replace for loop with map in topologicalSortChangesInBatch Signed-off-by: homily707 --- internal/pkg/pluginengine/change_helper.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/internal/pkg/pluginengine/change_helper.go b/internal/pkg/pluginengine/change_helper.go index bb8e6e0b5..527bbd69c 100644 --- a/internal/pkg/pluginengine/change_helper.go +++ b/internal/pkg/pluginengine/change_helper.go @@ -198,6 +198,8 @@ func topologicalSortChangesInBatch(changes []*Change) ([][]*Change, error) { // get tools from changes tools := getToolsFromChanges(changes) + // map key to changes + changesKeyMap := getChangesKeyMap(changes) batchesOfChanges := make([][]*Change, 0) @@ -216,10 +218,9 @@ func topologicalSortChangesInBatch(changes []*Change) ([][]*Change, error) { changesOneBatch := make([]*Change, 0) // for each tool in the batch, find the change that matches it for _, tool := range batch { - for _, change := range changes { - if change.Tool.Key() == tool.Key() { - changesOneBatch = append(changesOneBatch, change) - } + // only add the change that has the tool match with it + if change, ok := changesKeyMap[tool.Key()]; ok { + changesOneBatch = append(changesOneBatch, change) } } @@ -247,3 +248,11 @@ func getToolsFromChanges(changes []*Change) []configloader.Tool { return tools } + +func getChangesKeyMap(changes []*Change) map[string]*Change { + changesKeyMap := make(map[string]*Change) + for _, change := range changes { + changesKeyMap[change.Tool.Key()] = change + } + return changesKeyMap +}