Skip to content

Modern Docker Management, Designed for Everyone

perf: replace repeated update-record scans with indexed lookups (#4110)

Kyle Mendell109f22402d05
diff --git a/backend/internal/image/image.go b/backend/internal/image/image.go
index 4172e5291..913b5fa03 100644
--- a/backend/internal/image/image.go
+++ b/backend/internal/image/image.go
@@ -745,8 +745,9 @@ func (s *ImageService) GetUpdateInfoByImageRefs(ctx context.Context, imageRefs [
return nil, errors.WrapIf(err, "failed to fetch update records by image refs")
}
+ index := indexLatestImageUpdateRecordsInternal(updateRecords)
for _, lookup := range lookups {
- if record := selectLatestMatchingImageUpdateRecordInternal(lookup, updateRecords); record != nil {
+ if record := selectLatestMatchingImageUpdateRecordInternal(lookup, updateRecords, index); record != nil {
result[lookup.originalRef] = record.UpdateInfo()
}
}
@@ -848,26 +849,53 @@ func buildImageRefUpdateLookupsInternal(imageRefs []string) []imageRefUpdateLook
return lookups
}
+type imageUpdateRecordKeyInternal struct {
+ repository string
+ tag string
+}
+
+func newImageUpdateRecordKeyInternal(repository, tag string) imageUpdateRecordKeyInternal {
+ return imageUpdateRecordKeyInternal{repository: strings.TrimSpace(repository), tag: strings.ToLower(strings.TrimSpace(tag))}
+}
+
+// indexLatestImageUpdateRecordsInternal maps each repository and tag to the
+// position of its latest record. Ties keep the earliest position, matching
+// the order the records were fetched in.
+func indexLatestImageUpdateRecordsInternal(updateRecords []imageupdate.ImageUpdateRecord) map[imageUpdateRecordKeyInternal]int {
+ index := make(map[imageUpdateRecordKeyInternal]int, len(updateRecords))
+ for i := range updateRecords {
+ key := newImageUpdateRecordKeyInternal(updateRecords[i].Repository, updateRecords[i].Tag)
+ if existing, ok := index[key]; !ok || updateRecords[i].CheckTime.After(updateRecords[existing].CheckTime) {
+ index[key] = i
+ }
+ }
+ return index
+}
+
+// selectLatestMatchingImageUpdateRecordInternal picks the latest record across
+// the lookup's repository aliases; equal check times fall back to fetch order.
func selectLatestMatchingImageUpdateRecordInternal(
lookup imageRefUpdateLookup,
updateRecords []imageupdate.ImageUpdateRecord,
+ index map[imageUpdateRecordKeyInternal]int,
) *imageupdate.ImageUpdateRecord {
- var latest *imageupdate.ImageUpdateRecord
-
- for i := range updateRecords {
- record := &updateRecords[i]
- if !strings.EqualFold(strings.TrimSpace(record.Tag), lookup.tag) {
- continue
- }
- if _, exists := lookup.repositoryCandidates[strings.TrimSpace(record.Repository)]; !exists {
+ latest := -1
+ for repository := range lookup.repositoryCandidates {
+ i, ok := index[newImageUpdateRecordKeyInternal(repository, lookup.tag)]
+ if !ok {
continue
}
- if latest == nil || record.CheckTime.After(latest.CheckTime) {
- latest = record
+ switch {
+ case latest == -1,
+ updateRecords[i].CheckTime.After(updateRecords[latest].CheckTime),
+ updateRecords[i].CheckTime.Equal(updateRecords[latest].CheckTime) && i < latest:
+ latest = i
}
}
-
- return latest
+ if latest == -1 {
+ return nil
+ }
+ return &updateRecords[latest]
}
func convertLabels(labels map[string]string) map[string]any {
diff --git a/backend/internal/imageupdate/tag_updates.go b/backend/internal/imageupdate/tag_updates.go
index 9412310de..7460d57a1 100644
--- a/backend/internal/imageupdate/tag_updates.go
+++ b/backend/internal/imageupdate/tag_updates.go
@@ -324,12 +324,18 @@ func (s *ImageUpdateService) saveContainerTagResultInternal(ctx context.Context,
})
}
+// attachContainerUpdatesInternal binds each container's policy result to the
+// image result sharing its normalized reference. The image-only snapshot is
+// taken before the first container result is attached so it stays untouched.
func attachContainerUpdatesInternal(results map[string]*imageupdatetypes.Response, containerUpdates map[string]*imageupdatetypes.Response) {
+ containerIDsByRef := make(map[string][]string, len(containerUpdates))
+ for id, update := range containerUpdates {
+ normalized := refs.NormalizeImageUpdateRef(update.ImageRef)
+ containerIDsByRef[normalized] = append(containerIDsByRef[normalized], id)
+ }
for imageRef, result := range results {
- for id, update := range containerUpdates {
- if refs.NormalizeImageUpdateRef(update.ImageRef) != refs.NormalizeImageUpdateRef(imageRef) {
- continue
- }
+ for _, id := range containerIDsByRef[refs.NormalizeImageUpdateRef(imageRef)] {
+ update := containerUpdates[id]
if result.ImageUpdate == nil {
imageOnly := *result
imageOnly.ContainerUpdates = nil
diff --git a/backend/internal/project/project_details.go b/backend/internal/project/project_details.go
index 35a925149..b38da9a1e 100644
--- a/backend/internal/project/project_details.go
+++ b/backend/internal/project/project_details.go
@@ -571,11 +571,11 @@ func (s *ProjectService) enrichProjectsWithUpdateInfoInternal(
}
}
- records := s.getProjectServiceUpdateRecordsInternal(ctx, projectIDs)
+ recordsByProjectID := groupUpdateRecordsByProjectInternal(s.getProjectServiceUpdateRecordsInternal(ctx, projectIDs))
scoped := s.getProjectContainerUpdateInfoInternal(ctx, details)
for i := range details {
if services := servicesByProjectID[details[i].ID]; services != nil {
- details[i].UpdateInfo = BuildConfiguredUpdateInfo(details[i].ID, services, updateInfoByRef, records, configuredRuntimeServiceUpdateInfoInternal(services, details[i].RuntimeServices, scoped))
+ details[i].UpdateInfo = BuildConfiguredUpdateInfo(details[i].ID, services, updateInfoByRef, recordsByProjectID[details[i].ID], configuredRuntimeServiceUpdateInfoInternal(services, details[i].RuntimeServices, scoped))
continue
}
refs := imageRefsByProjectID[details[i].ID]
@@ -616,6 +616,14 @@ func (s *ProjectService) getProjectServiceUpdateRecordsInternal(ctx context.Cont
return records
}
+func groupUpdateRecordsByProjectInternal(records []imageupdate.ImageUpdateRecord) map[string][]imageupdate.ImageUpdateRecord {
+ grouped := make(map[string][]imageupdate.ImageUpdateRecord)
+ for _, record := range records {
+ grouped[record.ProjectID] = append(grouped[record.ProjectID], record)
+ }
+ return grouped
+}
+
// BuildConfiguredUpdateInfo matches checks to current services and aggregates their results.
func BuildConfiguredUpdateInfo(projectID string, services []composetypes.ServiceConfig, byRef map[string]*imagetypes.UpdateInfo, records []imageupdate.ImageUpdateRecord, runtimeUpdates ...map[string]*imagetypes.UpdateInfo) *project.UpdateInfo {
checks := make(map[string]*imageupdate.ImageUpdateRecord)