meshcontroller: improve service instance search performance in Master#1503
Conversation
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>
There was a problem hiding this comment.
Pull request overview
Improves Master.checkServiceInstances performance by replacing an O(N*M) nested search with an O(N+M) map lookup keyed by (ServiceName, InstanceID).
Changes:
- Build a map of service instance statuses keyed by service name + instance ID
- Replace per-spec linear scan with a direct map lookup
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
| statusMap := make(map[instanceKey]*spec.ServiceInstanceStatus) | ||
| for _, s := range statuses { | ||
| statusMap[instanceKey{s.ServiceName, s.InstanceID}] = s |
There was a problem hiding this comment.
The composite key literals rely on positional field order, which is easy to accidentally swap later. Prefer keyed struct literals for clarity and safety (e.g., instanceKey{serviceName: ..., instanceID: ...}).
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
| status = s | ||
| } | ||
| } | ||
| status := statusMap[instanceKey{_spec.ServiceName, _spec.InstanceID}] |
There was a problem hiding this comment.
The composite key literals rely on positional field order, which is easy to accidentally swap later. Prefer keyed struct literals for clarity and safety (e.g., instanceKey{serviceName: ..., instanceID: ...}).
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1503 +/- ##
===========================================
- Coverage 79.48% 37.63% -41.85%
===========================================
Files 152 374 +222
Lines 17295 41858 +24563
===========================================
+ Hits 13747 15754 +2007
- Misses 2879 25235 +22356
- Partials 669 869 +200 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@copilot add a commit to fix your reviewing suggestion |
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.