@@ -20,6 +20,24 @@ import (
2020 "github.com/prometheus/common/model"
2121)
2222
23+ var labelsPool = & sync.Pool {
24+ New : func () interface {} {
25+ return make (Labels )
26+ },
27+ }
28+
29+ func getLabelsFromPool () Labels {
30+ return labelsPool .Get ().(Labels )
31+ }
32+
33+ func putLabelsToPool (labels Labels ) {
34+ for k := range labels {
35+ delete (labels , k )
36+ }
37+
38+ labelsPool .Put (labels )
39+ }
40+
2341// MetricVec is a Collector to bundle metrics of the same name that differ in
2442// their label values. MetricVec is not used directly but as a building block
2543// for implementations of vectors of a given metric type, like GaugeVec,
@@ -93,6 +111,8 @@ func (m *MetricVec) DeleteLabelValues(lvs ...string) bool {
93111// there for pros and cons of the two methods.
94112func (m * MetricVec ) Delete (labels Labels ) bool {
95113 labels = constrainLabels (m .desc , labels )
114+ defer putLabelsToPool (labels )
115+
96116 h , err := m .hashLabels (labels )
97117 if err != nil {
98118 return false
@@ -109,6 +129,8 @@ func (m *MetricVec) Delete(labels Labels) bool {
109129// To match curried labels with DeletePartialMatch, it must be called on the base vector.
110130func (m * MetricVec ) DeletePartialMatch (labels Labels ) int {
111131 labels = constrainLabels (m .desc , labels )
132+ defer putLabelsToPool (labels )
133+
112134 return m .metricMap .deleteByLabels (labels , m .curry )
113135}
114136
@@ -229,6 +251,8 @@ func (m *MetricVec) GetMetricWithLabelValues(lvs ...string) (Metric, error) {
229251// for example GaugeVec.
230252func (m * MetricVec ) GetMetricWith (labels Labels ) (Metric , error ) {
231253 labels = constrainLabels (m .desc , labels )
254+ defer putLabelsToPool (labels )
255+
232256 h , err := m .hashLabels (labels )
233257 if err != nil {
234258 return nil , err
@@ -647,15 +671,16 @@ func inlineLabelValues(lvs []string, curry []curriedLabelValue) []string {
647671}
648672
649673func constrainLabels (desc * Desc , labels Labels ) Labels {
650- constrainedValues := make ( Labels , len ( labels ) )
674+ constrainedLabels := getLabelsFromPool ( )
651675 for l , v := range labels {
652676 if i , ok := indexOf (l , desc .variableLabels .labelNames ()); ok {
653- constrainedValues [l ] = desc .variableLabels [i ].Constrain (v )
654- continue
677+ v = desc .variableLabels [i ].Constrain (v )
655678 }
656- constrainedValues [l ] = v
679+
680+ constrainedLabels [l ] = v
657681 }
658- return constrainedValues
682+
683+ return constrainedLabels
659684}
660685
661686func constrainLabelValues (desc * Desc , lvs []string , curry []curriedLabelValue ) []string {
0 commit comments