@@ -80,7 +80,7 @@ def _accumulate_metrics(metrics, accumulate):
80
80
for s in metric .samples :
81
81
name , labels , value , timestamp , exemplar = s
82
82
if metric .type == 'gauge' :
83
- without_pid_key = (name , tuple (l for l in labels if l [0 ] != 'pid' ))
83
+ without_pid_key = (name , tuple ([ l for l in labels if l [0 ] != 'pid' ] ))
84
84
if metric ._multiprocess_mode == 'min' :
85
85
current = samples_setdefault (without_pid_key , value )
86
86
if value < current :
@@ -95,15 +95,18 @@ def _accumulate_metrics(metrics, accumulate):
95
95
samples [(name , labels )] = value
96
96
97
97
elif metric .type == 'histogram' :
98
- bucket = tuple (float (l [1 ]) for l in labels if l [0 ] == 'le' )
99
- if bucket :
100
- # _bucket
101
- without_le = tuple (l for l in labels if l [0 ] != 'le' )
102
- buckets [without_le ][bucket [0 ]] += value
103
- else :
98
+ # A for loop with early exit is faster than a genexpr
99
+ # or a listcomp that ends up building unnecessary things
100
+ for l in labels :
101
+ if l [0 ] == 'le' :
102
+ bucket_value = float (l [1 ])
103
+ # _bucket
104
+ without_le = tuple (l for l in labels if l [0 ] != 'le' )
105
+ buckets [without_le ][bucket_value ] += value
106
+ break
107
+ else : # did not find the `le` key
104
108
# _sum/_count
105
109
samples [(name , labels )] += value
106
-
107
110
else :
108
111
# Counter and Summary.
109
112
samples [(name , labels )] += value
0 commit comments