Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit f7b5fc0

Browse files
committed
Refactor Labels creation
Previous Labels creation involved creating a lot more smaller objects This change creates the final object earlier and reuses this memory. Turns out that this change by itself does not help all that much. But it does allow the Labels to use the insertCtx pool, which is implemented in the next commit.
1 parent a565b92 commit f7b5fc0

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

pkg/pgmodel/labels.go

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,29 @@ func EmptyLables() Labels {
3131
// LabelsFromSlice converts a labels.Labels to a Labels object
3232
func LabelsFromSlice(ls labels.Labels) (Labels, error) {
3333
length := len(ls)
34-
names := make([]string, 0, length)
35-
values := make([]string, 0, length)
34+
labels := Labels{
35+
names: make([]string, 0, length),
36+
values: make([]string, 0, length),
37+
}
3638

37-
metricName := ""
39+
labels.metricName = ""
3840
for _, l := range ls {
39-
names = append(names, l.Name)
40-
values = append(values, l.Value)
41+
labels.names = append(labels.names, l.Name)
42+
labels.values = append(labels.values, l.Value)
4143
if l.Name == MetricNameLabelName {
42-
metricName = l.Value
44+
labels.metricName = l.Value
4345
}
4446
}
4547

46-
return LabelsFromSlices(names, values, metricName)
48+
err := initLabels(&labels)
49+
return labels, err
4750
}
4851

49-
// LabelsFromSlices creates a Labels object from keys, values, and metric name
50-
func LabelsFromSlices(names []string, values []string, metricName string) (Labels, error) {
51-
l := Labels{names: names, values: values, metricName: metricName}
52+
// initLabels intializes labels
53+
func initLabels(l *Labels) error {
5254

53-
if !sort.IsSorted(&l) {
54-
sort.Sort(&l)
55+
if !sort.IsSorted(l) {
56+
sort.Sort(l)
5557
}
5658

5759
length := len(l.names)
@@ -67,7 +69,7 @@ func LabelsFromSlices(names []string, values []string, metricName string) (Label
6769
// total length anyway, we only use 16bits to store the legth of each substring
6870
// in our string encoding
6971
if expectedStrLen > math.MaxUint16 {
70-
return l, fmt.Errorf("series too long, combined series has length %d, max length %d", expectedStrLen, ^uint16(0))
72+
return fmt.Errorf("series too long, combined series has length %d, max length %d", expectedStrLen, ^uint16(0))
7173
}
7274

7375
// the string representation is
@@ -100,26 +102,28 @@ func LabelsFromSlices(names []string, values []string, metricName string) (Label
100102

101103
l.str = builder.String()
102104

103-
return l, nil
105+
return nil
104106
}
105107

106108
func labelProtosToLabels(labelPairs []prompb.Label) (Labels, string, error) {
107109
length := len(labelPairs)
108-
names := make([]string, 0, length)
109-
values := make([]string, 0, length)
110+
labels := Labels{
111+
names: make([]string, 0, length),
112+
values: make([]string, 0, length),
113+
}
110114

111-
metricName := ""
115+
labels.metricName = ""
112116
for _, l := range labelPairs {
113-
names = append(names, l.Name)
114-
values = append(values, l.Value)
117+
labels.names = append(labels.names, l.Name)
118+
labels.values = append(labels.values, l.Value)
115119
if l.Name == MetricNameLabelName {
116-
metricName = l.Value
120+
labels.metricName = l.Value
117121
}
118122
}
119123

120-
ls, err := LabelsFromSlices(names, values, metricName)
124+
err := initLabels(&labels)
121125

122-
return ls, metricName, err
126+
return labels, labels.metricName, err
123127
}
124128

125129
func (l Labels) isEmpty() bool {

0 commit comments

Comments
 (0)