Skip to content

Commit 80d6d73

Browse files
LokiWagergoogle-labs-jules[bot]Copilot
authored
meshcontroller: improve service instance search performance in Master (#1503)
* meshcontroller: improve service instance search performance in Master Replaced the O(N*M) nested loop in checkServiceInstances with an O(N+M) map-based lookup. This significantly improves performance when dealing with a large number of service instances. The map uses a composite key consisting of ServiceName and InstanceID to uniquely identify each instance status. Co-authored-by: LokiWager <32408858+LokiWager@users.noreply.github.com> * Update pkg/object/meshcontroller/master/master.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent b21c3ee commit 80d6d73

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

pkg/object/meshcontroller/master/master.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,21 @@ func (m *Master) checkServiceInstances() {
139139
statuses := m.service.ListAllServiceInstanceStatuses()
140140
specs := m.service.ListAllServiceInstanceSpecs()
141141

142+
type instanceKey struct {
143+
serviceName string
144+
instanceID string
145+
}
146+
statusMap := make(map[instanceKey]*spec.ServiceInstanceStatus, len(statuses))
147+
for _, s := range statuses {
148+
statusMap[instanceKey{s.ServiceName, s.InstanceID}] = s
149+
}
150+
142151
for _, _spec := range specs {
143152
if !m.isMeshRegistryName(_spec.RegistryName) {
144153
continue
145154
}
146155

147-
// TODO: improve search performance
148-
var status *spec.ServiceInstanceStatus
149-
for _, s := range statuses {
150-
if s.ServiceName == _spec.ServiceName && s.InstanceID == _spec.InstanceID {
151-
status = s
152-
}
153-
}
156+
status := statusMap[instanceKey{_spec.ServiceName, _spec.InstanceID}]
154157

155158
if status == nil {
156159
format := "status of %s/%s not found, need to delete"

0 commit comments

Comments
 (0)