Skip to content

Commit

Permalink
Reduce constrainLabels allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
khasanovbi committed May 13, 2023
1 parent 297fea3 commit 53e710e
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions prometheus/vec.go
Expand Up @@ -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
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 53e710e

Please sign in to comment.