diff --git a/pkg/cortexpb/compat.go b/pkg/cortexpb/compat.go index 83bdbff33d..6de2423d56 100644 --- a/pkg/cortexpb/compat.go +++ b/pkg/cortexpb/compat.go @@ -67,13 +67,13 @@ func FromLabelAdaptersToLabels(ls []LabelAdapter) labels.Labels { // Do NOT use unsafe to convert between data types because this function may // get in input labels whose data structure is reused. func FromLabelAdaptersToLabelsWithCopy(input []LabelAdapter) labels.Labels { - return CopyLabels(input) + return CopyLabels(FromLabelAdaptersToLabels(input)) } // Efficiently copies labels input slice. To be used in cases where input slice // can be reused, but long-term copy is needed. -func CopyLabels(input []LabelAdapter) labels.Labels { - builder := labels.NewBuilder(labels.EmptyLabels()) +func CopyLabels(input []labels.Label) labels.Labels { + result := make(labels.Labels, len(input)) size := 0 for _, l := range input { @@ -84,14 +84,12 @@ func CopyLabels(input []LabelAdapter) labels.Labels { // Copy all strings into the buffer, and use 'yoloString' to convert buffer // slices to strings. buf := make([]byte, size) - var name, value string - for _, l := range input { - name, buf = copyStringToBuffer(l.Name, buf) - value, buf = copyStringToBuffer(l.Value, buf) - builder.Set(name, value) + for i, l := range input { + result[i].Name, buf = copyStringToBuffer(l.Name, buf) + result[i].Value, buf = copyStringToBuffer(l.Value, buf) } - return builder.Labels() + return result } // Copies string to buffer (which must be big enough), and converts buffer slice containing