Skip to content

Commit 6f666c7

Browse files
john-doyethanvc
authored andcommitted
optimize memory usage
Signed-off-by: ethanvc <[email protected]>
1 parent 80d3f0b commit 6f666c7

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

prometheus/internal/metric.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,16 @@ func (s MetricSorter) Less(i, j int) bool {
8383
// MetricFamilies pruned and the remaining MetricFamilies sorted by name within
8484
// the slice, with the contained Metrics sorted within each MetricFamily.
8585
func NormalizeMetricFamilies(metricFamiliesByName map[string]*dto.MetricFamily) []*dto.MetricFamily {
86+
result := make([]*dto.MetricFamily, 0, len(metricFamiliesByName))
8687
for _, mf := range metricFamiliesByName {
87-
sort.Sort(MetricSorter(mf.Metric))
88-
}
89-
names := make([]string, 0, len(metricFamiliesByName))
90-
for name, mf := range metricFamiliesByName {
91-
if len(mf.Metric) > 0 {
92-
names = append(names, name)
88+
if len(mf.Metric) == 0 {
89+
continue
9390
}
91+
sort.Sort(MetricSorter(mf.Metric))
92+
result = append(result, mf)
9493
}
95-
sort.Strings(names)
96-
result := make([]*dto.MetricFamily, 0, len(names))
97-
for _, name := range names {
98-
result = append(result, metricFamiliesByName[name])
99-
}
94+
sort.Slice(result, func(i, j int) bool {
95+
return *result[i].Name < *result[j].Name
96+
})
10097
return result
10198
}

0 commit comments

Comments
 (0)