Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce constrainLabels allocations #1272

Merged
merged 2 commits into from Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 30 additions & 5 deletions prometheus/vec.go
Expand Up @@ -20,6 +20,24 @@ import (
"github.com/prometheus/common/model"
)

var labelsPool = &sync.Pool{
New: func() interface{} {
return make(Labels)
},
}

func getLabelsFromPool() Labels {
return labelsPool.Get().(Labels)
}

func putLabelsToPool(labels Labels) {
for k := range labels {
delete(labels, k)
}

labelsPool.Put(labels)
}

// MetricVec is a Collector to bundle metrics of the same name that differ in
// their label values. MetricVec is not used directly but as a building block
// for implementations of vectors of a given metric type, like GaugeVec,
Expand Down Expand Up @@ -93,6 +111,8 @@ func (m *MetricVec) DeleteLabelValues(lvs ...string) bool {
// there for pros and cons of the two methods.
func (m *MetricVec) Delete(labels Labels) bool {
labels = constrainLabels(m.desc, labels)
defer putLabelsToPool(labels)

h, err := m.hashLabels(labels)
if err != nil {
return false
Expand All @@ -109,6 +129,8 @@ func (m *MetricVec) Delete(labels Labels) bool {
// To match curried labels with DeletePartialMatch, it must be called on the base vector.
func (m *MetricVec) DeletePartialMatch(labels Labels) int {
labels = constrainLabels(m.desc, labels)
defer putLabelsToPool(labels)

return m.metricMap.deleteByLabels(labels, m.curry)
}

Expand Down Expand Up @@ -229,6 +251,8 @@ func (m *MetricVec) GetMetricWithLabelValues(lvs ...string) (Metric, error) {
// for example GaugeVec.
func (m *MetricVec) GetMetricWith(labels Labels) (Metric, error) {
labels = constrainLabels(m.desc, labels)
defer putLabelsToPool(labels)

h, err := m.hashLabels(labels)
if err != nil {
return nil, err
Expand Down Expand Up @@ -647,15 +671,16 @@ func inlineLabelValues(lvs []string, curry []curriedLabelValue) []string {
}

func constrainLabels(desc *Desc, labels Labels) Labels {
constrainedValues := make(Labels, len(labels))
constrainedLabels := getLabelsFromPool()
for l, v := range labels {
if i, ok := indexOf(l, desc.variableLabels.labelNames()); ok {
constrainedValues[l] = desc.variableLabels[i].Constrain(v)
continue
v = desc.variableLabels[i].Constrain(v)
}
constrainedValues[l] = v

constrainedLabels[l] = v
}
return constrainedValues

return constrainedLabels
}

func constrainLabelValues(desc *Desc, lvs []string, curry []curriedLabelValue) []string {
Copy link
Member

@bwplotka bwplotka May 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do similar here for consistency?

Expand Down
28 changes: 28 additions & 0 deletions prometheus/vec_test.go
Expand Up @@ -904,6 +904,13 @@ func testConstrainedCurryVec(t *testing.T, vec *CounterVec, constraint func(stri
})
}

func BenchmarkMetricVecWithBasic(b *testing.B) {
benchmarkMetricVecWith(b, Labels{
"l1": "onevalue",
"l2": "twovalue",
})
}

func BenchmarkMetricVecWithLabelValuesBasic(b *testing.B) {
benchmarkMetricVecWithLabelValues(b, map[string][]string{
"l1": {"onevalue"},
Expand Down Expand Up @@ -948,6 +955,27 @@ func benchmarkMetricVecWithLabelValuesCardinality(b *testing.B, nkeys, nvalues i
benchmarkMetricVecWithLabelValues(b, labels)
}

func benchmarkMetricVecWith(b *testing.B, labels map[string]string) {
var keys []string
for k := range labels {
keys = append(keys, k)
}

vec := NewGaugeVec(
GaugeOpts{
Name: "test",
Help: "helpless",
},
keys,
)

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
vec.With(labels)
}
}

func benchmarkMetricVecWithLabelValues(b *testing.B, labels map[string][]string) {
var keys []string
for k := range labels { // Map order dependent, who cares though.
Expand Down