Repository navigation
Commit 6bcd096
committed
perf: avoid allocating a mapping lambda on every labelValues() call
labelValues() went straight to data.computeIfAbsent(key, l -> ...). The mapping
function captures 'this', so a new lambda instance was allocated on every call -
including the common case where the data point already exists, since the lambda
argument is constructed before computeIfAbsent runs.
Add a data.get(key) fast path that returns the existing data point without
constructing the lambda. In a JMH benchmark of a histogram-heavy workload this
cut record-path allocation by ~18% (exactly the 16-byte captured lambda per
observation).
On the miss path, validate the label values on the raw array before
computeIfAbsent, so the null check runs outside the ConcurrentHashMap bin lock
and without List indirection. Creation stays inside computeIfAbsent: newDataPoint()
has side effects (a native histogram may schedule a reset task), so at-most-once
creation must be preserved.
Behavior is unchanged (verified by the core tests, including StatefulMetricTest).
Signed-off-by: David Mollitor <david.mollitor@databricks.com>1 parent f71915f commit 6bcd096
1 file changed
Lines changed: 14 additions & 14 deletions
File tree
- prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
133 | 133 | | |
134 | 134 | | |
135 | 135 | | |
| |||
0 commit comments