Skip to content

Commit cbe0784

Browse files
committed
no useless allocs
1 parent 29f93c5 commit cbe0784

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/aggregator/DoubleLastValueAggregator.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import io.opentelemetry.sdk.resources.Resource;
2323
import java.util.Collection;
2424
import java.util.List;
25-
import java.util.Objects;
26-
import java.util.concurrent.atomic.AtomicReference;
2725
import java.util.function.Supplier;
2826
import javax.annotation.Nullable;
2927
import javax.annotation.concurrent.ThreadSafe;
@@ -114,8 +112,8 @@ public MetricData toMetricData(
114112
}
115113

116114
static final class Handle extends AggregatorHandle<DoublePointData, DoubleExemplarData> {
117-
@Nullable private static final Double DEFAULT_VALUE = null;
118-
private final AtomicReference<Double> current = new AtomicReference<>(DEFAULT_VALUE);
115+
private volatile boolean set = false;
116+
private volatile double current = 0;
119117

120118
// Only used when memoryMode is REUSABLE_DATA
121119
@Nullable private final MutableDoublePointData reusablePoint;
@@ -136,20 +134,31 @@ protected DoublePointData doAggregateThenMaybeReset(
136134
Attributes attributes,
137135
List<DoubleExemplarData> exemplars,
138136
boolean reset) {
139-
Double value = reset ? this.current.getAndSet(DEFAULT_VALUE) : this.current.get();
137+
double currentLocal = current;
138+
if (!set) {
139+
throw new NullPointerException();
140+
}
141+
if (reset) {
142+
set = false;
143+
}
144+
145+
DoublePointData output;
140146
if (reusablePoint != null) {
141-
reusablePoint.set(
142-
startEpochNanos, epochNanos, attributes, Objects.requireNonNull(value), exemplars);
143-
return reusablePoint;
147+
reusablePoint.set(startEpochNanos, epochNanos, attributes, currentLocal, exemplars);
148+
output = reusablePoint;
144149
} else {
145-
return ImmutableDoublePointData.create(
146-
startEpochNanos, epochNanos, attributes, Objects.requireNonNull(value), exemplars);
150+
output =
151+
ImmutableDoublePointData.create(
152+
startEpochNanos, epochNanos, attributes, currentLocal, exemplars);
147153
}
154+
155+
return output;
148156
}
149157

150158
@Override
151159
protected void doRecordDouble(double value) {
152-
current.set(value);
160+
current = value;
161+
set = true;
153162
}
154163
}
155164
}

0 commit comments

Comments
 (0)