|
34 | 34 | import java.util.HashMap; |
35 | 35 | import java.util.List; |
36 | 36 | import java.util.Map; |
| 37 | +import java.util.function.BiConsumer; |
| 38 | +import java.util.function.Function; |
37 | 39 | import java.util.logging.Level; |
38 | 40 | import java.util.logging.Logger; |
39 | 41 |
|
@@ -70,6 +72,8 @@ public final class AsynchronousMetricStorage<T extends PointData, U extends Exem |
70 | 72 | // Only populated if memoryMode == REUSABLE_DATA |
71 | 73 | private final ObjectPool<T> reusablePointsPool; |
72 | 74 | 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; |
73 | 77 |
|
74 | 78 | private final List<T> reusablePointsList = new ArrayList<>(); |
75 | 79 | // If aggregationTemporality == DELTA, this reference and lastPoints will be swapped at every |
@@ -99,6 +103,8 @@ private AsynchronousMetricStorage( |
99 | 103 | this.maxCardinality = maxCardinality - 1; |
100 | 104 | this.reusablePointsPool = new ObjectPool<>(aggregator::createReusablePoint); |
101 | 105 | this.reusableHandlesPool = new ObjectPool<>(aggregator::createHandle); |
| 106 | + this.handleBuilder = ignored -> reusableHandlesPool.borrowObject(); |
| 107 | + this.handleReleaser = (ignored, handle) -> reusableHandlesPool.returnObject(handle); |
102 | 108 |
|
103 | 109 | if (memoryMode == REUSABLE_DATA) { |
104 | 110 | this.lastPoints = new PooledHashMap<>(); |
@@ -138,16 +144,14 @@ AsynchronousMetricStorage<T, U> create( |
138 | 144 | /** Record callback measurement from {@link ObservableLongMeasurement}. */ |
139 | 145 | void record(Attributes attributes, long value) { |
140 | 146 | attributes = validateAndProcessAttributes(attributes); |
141 | | - AggregatorHandle<T, U> handle = |
142 | | - aggregatorHandles.computeIfAbsent(attributes, key -> reusableHandlesPool.borrowObject()); |
| 147 | + AggregatorHandle<T, U> handle = aggregatorHandles.computeIfAbsent(attributes, handleBuilder); |
143 | 148 | handle.recordLong(value, attributes, Context.current()); |
144 | 149 | } |
145 | 150 |
|
146 | 151 | /** Record callback measurement from {@link ObservableDoubleMeasurement}. */ |
147 | 152 | void record(Attributes attributes, double value) { |
148 | 153 | attributes = validateAndProcessAttributes(attributes); |
149 | | - AggregatorHandle<T, U> handle = |
150 | | - aggregatorHandles.computeIfAbsent(attributes, key -> reusableHandlesPool.borrowObject()); |
| 154 | + AggregatorHandle<T, U> handle = aggregatorHandles.computeIfAbsent(attributes, handleBuilder); |
151 | 155 | handle.recordDouble(value, attributes, Context.current()); |
152 | 156 | } |
153 | 157 |
|
@@ -198,7 +202,7 @@ public MetricData collect( |
198 | 202 | : collectWithCumulativeAggregationTemporality(); |
199 | 203 |
|
200 | 204 | // collectWith*AggregationTemporality() methods are responsible for resetting the handle |
201 | | - aggregatorHandles.forEach((attribute, handle) -> reusableHandlesPool.returnObject(handle)); |
| 205 | + aggregatorHandles.forEach(handleReleaser); |
202 | 206 | aggregatorHandles.clear(); |
203 | 207 |
|
204 | 208 | return aggregator.toMetricData( |
|
0 commit comments