From 0b779603496d3852c8806f3abd81a83e7515aa39 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Mon, 12 Oct 2015 20:30:11 -0700 Subject: [PATCH] agent: remove an N^2 check. See #1265 --- command/agent/local.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/command/agent/local.go b/command/agent/local.go index 62e818bf8..1bd86395a 100644 --- a/command/agent/local.go +++ b/command/agent/local.go @@ -389,16 +389,15 @@ func (l *localState) setSyncState() error { l.serviceStatus[id] = syncStatus{inSync: equal} } + // Index the remote health checks to improve efficiency + checkIndex := make(map[string]*structs.HealthCheck, len(checks)) + for _, check := range checks { + checkIndex[check.CheckID] = check + } + + // Sync any check which doesn't exist on the remote side for id, _ := range l.checks { - // Sync any check which doesn't exist on the remote side - found := false - for _, check := range checks { - if check.CheckID == id { - found = true - break - } - } - if !found { + if _, ok := checkIndex[id]; !ok { l.checkStatus[id] = syncStatus{inSync: false} } }