From 53e710e75bf5c7b810bcd93a67f01753f56710a0 Mon Sep 17 00:00:00 2001 From: Bulat Khasanov Date: Sat, 13 May 2023 20:22:52 +0300 Subject: [PATCH] Reduce constrainLabels allocations --- prometheus/vec.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/prometheus/vec.go b/prometheus/vec.go index 386fb2d23..c85636e61 100644 --- a/prometheus/vec.go +++ b/prometheus/vec.go @@ -92,7 +92,7 @@ func (m *MetricVec) DeleteLabelValues(lvs ...string) bool { // This method is used for the same purpose as DeleteLabelValues(...string). See // there for pros and cons of the two methods. func (m *MetricVec) Delete(labels Labels) bool { - labels = constrainLabels(m.desc, labels) + constrainLabels(m.desc, labels) h, err := m.hashLabels(labels) if err != nil { return false @@ -108,7 +108,7 @@ func (m *MetricVec) Delete(labels Labels) bool { // Note that curried labels will never be matched if deleting from the curried vector. // 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) + constrainLabels(m.desc, labels) return m.metricMap.deleteByLabels(labels, m.curry) } @@ -228,7 +228,7 @@ func (m *MetricVec) GetMetricWithLabelValues(lvs ...string) (Metric, error) { // around MetricVec, implementing a vector for a specific Metric implementation, // for example GaugeVec. func (m *MetricVec) GetMetricWith(labels Labels) (Metric, error) { - labels = constrainLabels(m.desc, labels) + constrainLabels(m.desc, labels) h, err := m.hashLabels(labels) if err != nil { return nil, err @@ -646,16 +646,12 @@ func inlineLabelValues(lvs []string, curry []curriedLabelValue) []string { return labelValues } -func constrainLabels(desc *Desc, labels Labels) Labels { - constrainedValues := make(Labels, len(labels)) +func constrainLabels(desc *Desc, labels Labels) { for l, v := range labels { if i, ok := indexOf(l, desc.variableLabels.labelNames()); ok { - constrainedValues[l] = desc.variableLabels[i].Constrain(v) - continue + labels[l] = desc.variableLabels[i].Constrain(v) } - constrainedValues[l] = v } - return constrainedValues } func constrainLabelValues(desc *Desc, lvs []string, curry []curriedLabelValue) []string {