Skip to content

Commit 87ce354

Browse files
authored
Avoid allocation in TimeWindowMax#record (#3206)
The capturing lambda that was used resulted in allocation on each invocation of the record method, which is on the hot path for metrics recording. To reduce the overhead of metrics, avoid the capturing lambda that results in allocation. Resolves gh-3193
1 parent 893e839 commit 87ce354

File tree

1 file changed

+3
-5
lines changed
  • micrometer-core/src/main/java/io/micrometer/core/instrument/distribution

1 file changed

+3
-5
lines changed

micrometer-core/src/main/java/io/micrometer/core/instrument/distribution/TimeWindowMax.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
2323
import java.util.concurrent.atomic.AtomicLong;
2424
import java.util.function.DoubleSupplier;
25-
import java.util.function.LongSupplier;
2625

2726
/**
2827
* An implementation of a decaying maximum for a distribution based on a configurable ring
@@ -71,12 +70,11 @@ public TimeWindowMax(Clock clock, long rotateFrequencyMillis, int bufferLength)
7170
* @param timeUnit The unit of time of the incoming sample.
7271
*/
7372
public void record(double sample, TimeUnit timeUnit) {
74-
record(() -> (long) TimeUtils.convert(sample, timeUnit, TimeUnit.NANOSECONDS));
73+
record((long) TimeUtils.convert(sample, timeUnit, TimeUnit.NANOSECONDS));
7574
}
7675

77-
private void record(LongSupplier sampleSupplier) {
76+
private void record(long sample) {
7877
rotate();
79-
long sample = sampleSupplier.getAsLong();
8078
for (AtomicLong max : ringBuffer) {
8179
updateMax(max, sample);
8280
}
@@ -109,7 +107,7 @@ public double poll() {
109107
* @param sample The value to record.
110108
*/
111109
public void record(double sample) {
112-
record(() -> Double.doubleToLongBits(sample));
110+
record(Double.doubleToLongBits(sample));
113111
}
114112

115113
private void updateMax(AtomicLong max, long sample) {

0 commit comments

Comments
 (0)