Skip to content

Commit

Permalink
Ensure buffers passed to labels.Labels.Bytes(), BytesWithLabels() and…
Browse files Browse the repository at this point in the history
… BytesWithoutLabels() are reused if resized
  • Loading branch information
charleskorn committed Dec 4, 2024
1 parent a06fb84 commit 74afbb3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
6 changes: 4 additions & 2 deletions pkg/streamingpromql/operators/aggregations/aggregation.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ func (a *Aggregation) groupingWithoutLabelsSeriesToGroupFuncs() (seriesToGroupLa
// Why 1024 bytes? It's what labels.Labels.String() uses as a buffer size, so we use that as a sensible starting point too.
b := make([]byte, 0, 1024)
bytesFunc := func(l labels.Labels) []byte {
return l.BytesWithoutLabels(b, a.Grouping...) // NewAggregation will add __name__ to Grouping for 'without' aggregations, so no need to add it here.
b = l.BytesWithoutLabels(b, a.Grouping...) // NewAggregation will add __name__ to Grouping for 'without' aggregations, so no need to add it here.
return b
}

lb := labels.NewBuilder(labels.EmptyLabels())
Expand All @@ -231,7 +232,8 @@ func (a *Aggregation) groupingByLabelsSeriesToGroupFuncs() (seriesToGroupLabelsB
// Why 1024 bytes? It's what labels.Labels.String() uses as a buffer size, so we use that as a sensible starting point too.
b := make([]byte, 0, 1024)
bytesFunc := func(l labels.Labels) []byte {
return l.BytesWithLabels(b, a.Grouping...)
b = l.BytesWithLabels(b, a.Grouping...)
return b
}

lb := labels.NewBuilder(labels.EmptyLabels())
Expand Down
9 changes: 6 additions & 3 deletions pkg/streamingpromql/operators/binops/binary_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ func vectorMatchingGroupKeyFunc(vectorMatching parser.VectorMatching) func(label
slices.Sort(vectorMatching.MatchingLabels)

return func(l labels.Labels) []byte {
return l.BytesWithLabels(buf, vectorMatching.MatchingLabels...)
buf = l.BytesWithLabels(buf, vectorMatching.MatchingLabels...)
return buf
}
}

if len(vectorMatching.MatchingLabels) == 0 {
// Fast path for common case for expressions like "a + b" with no 'on' or 'without' labels.
return func(l labels.Labels) []byte {
return l.BytesWithoutLabels(buf, labels.MetricName)
buf = l.BytesWithoutLabels(buf, labels.MetricName)
return buf
}
}

Expand All @@ -46,7 +48,8 @@ func vectorMatchingGroupKeyFunc(vectorMatching parser.VectorMatching) func(label
slices.Sort(lbls)

return func(l labels.Labels) []byte {
return l.BytesWithoutLabels(buf, lbls...)
buf = l.BytesWithoutLabels(buf, lbls...)
return buf
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,8 @@ func (g *GroupedVectorVectorBinaryOperation) additionalLabelsKeyFunc() func(oneS
buf := make([]byte, 0, 1024)

return func(oneSideLabels labels.Labels) []byte {
return oneSideLabels.BytesWithLabels(buf, g.VectorMatching.Include...)
buf = oneSideLabels.BytesWithLabels(buf, g.VectorMatching.Include...)
return buf
}
}

Expand All @@ -398,13 +399,15 @@ func (g *GroupedVectorVectorBinaryOperation) manySideGroupKeyFunc() func(manySid

if !g.shouldRemoveMetricNameFromManySide() && len(g.VectorMatching.Include) == 0 {
return func(manySideLabels labels.Labels) []byte {
return manySideLabels.Bytes(buf) // FIXME: it'd be nice if we could avoid copying the bytes here
buf = manySideLabels.Bytes(buf) // FIXME: it'd be nice if we could avoid Bytes() copying the slice here
return buf
}
}

if len(g.VectorMatching.Include) == 0 {
return func(manySideLabels labels.Labels) []byte {
return manySideLabels.BytesWithoutLabels(buf, labels.MetricName)
buf = manySideLabels.BytesWithoutLabels(buf, labels.MetricName)
return buf
}
}

Expand All @@ -416,7 +419,8 @@ func (g *GroupedVectorVectorBinaryOperation) manySideGroupKeyFunc() func(manySid
}

return func(manySideLabels labels.Labels) []byte {
return manySideLabels.BytesWithoutLabels(buf, labelsToRemove...)
buf = manySideLabels.BytesWithoutLabels(buf, labelsToRemove...)
return buf
}
}

Expand Down

0 comments on commit 74afbb3

Please sign in to comment.