Skip to content

Commit f831572

Browse files
committed
save some alloc
1 parent a5d8b67 commit f831572

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/AsynchronousMetricStorage.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import java.util.HashMap;
3535
import java.util.List;
3636
import java.util.Map;
37+
import java.util.function.BiConsumer;
38+
import java.util.function.Function;
3739
import java.util.logging.Level;
3840
import java.util.logging.Logger;
3941

@@ -70,6 +72,8 @@ public final class AsynchronousMetricStorage<T extends PointData, U extends Exem
7072
// Only populated if memoryMode == REUSABLE_DATA
7173
private final ObjectPool<T> reusablePointsPool;
7274
private final ObjectPool<AggregatorHandle<T, U>> reusableHandlesPool;
75+
private final Function<Attributes, AggregatorHandle<T, U>> handleBuilder;
76+
private final BiConsumer<Attributes, AggregatorHandle<T, U>> handleReleaser;
7377

7478
private final List<T> reusablePointsList = new ArrayList<>();
7579
// If aggregationTemporality == DELTA, this reference and lastPoints will be swapped at every
@@ -99,6 +103,8 @@ private AsynchronousMetricStorage(
99103
this.maxCardinality = maxCardinality - 1;
100104
this.reusablePointsPool = new ObjectPool<>(aggregator::createReusablePoint);
101105
this.reusableHandlesPool = new ObjectPool<>(aggregator::createHandle);
106+
this.handleBuilder = ignored -> reusableHandlesPool.borrowObject();
107+
this.handleReleaser = (ignored, handle) -> reusableHandlesPool.returnObject(handle);
102108

103109
if (memoryMode == REUSABLE_DATA) {
104110
this.lastPoints = new PooledHashMap<>();
@@ -138,16 +144,14 @@ AsynchronousMetricStorage<T, U> create(
138144
/** Record callback measurement from {@link ObservableLongMeasurement}. */
139145
void record(Attributes attributes, long value) {
140146
attributes = validateAndProcessAttributes(attributes);
141-
AggregatorHandle<T, U> handle =
142-
aggregatorHandles.computeIfAbsent(attributes, key -> reusableHandlesPool.borrowObject());
147+
AggregatorHandle<T, U> handle = aggregatorHandles.computeIfAbsent(attributes, handleBuilder);
143148
handle.recordLong(value, attributes, Context.current());
144149
}
145150

146151
/** Record callback measurement from {@link ObservableDoubleMeasurement}. */
147152
void record(Attributes attributes, double value) {
148153
attributes = validateAndProcessAttributes(attributes);
149-
AggregatorHandle<T, U> handle =
150-
aggregatorHandles.computeIfAbsent(attributes, key -> reusableHandlesPool.borrowObject());
154+
AggregatorHandle<T, U> handle = aggregatorHandles.computeIfAbsent(attributes, handleBuilder);
151155
handle.recordDouble(value, attributes, Context.current());
152156
}
153157

@@ -198,7 +202,7 @@ public MetricData collect(
198202
: collectWithCumulativeAggregationTemporality();
199203

200204
// collectWith*AggregationTemporality() methods are responsible for resetting the handle
201-
aggregatorHandles.forEach((attribute, handle) -> reusableHandlesPool.returnObject(handle));
205+
aggregatorHandles.forEach(handleReleaser);
202206
aggregatorHandles.clear();
203207

204208
return aggregator.toMetricData(

0 commit comments

Comments
 (0)