@@ -80,7 +80,7 @@ def _accumulate_metrics(metrics, accumulate):
8080 for s in metric .samples :
8181 name , labels , value , timestamp , exemplar = s
8282 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' ] ))
8484 if metric ._multiprocess_mode == 'min' :
8585 current = samples_setdefault (without_pid_key , value )
8686 if value < current :
@@ -95,15 +95,18 @@ def _accumulate_metrics(metrics, accumulate):
9595 samples [(name , labels )] = value
9696
9797 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
104108 # _sum/_count
105109 samples [(name , labels )] += value
106-
107110 else :
108111 # Counter and Summary.
109112 samples [(name , labels )] += value
0 commit comments