Skip to content

Commit

Permalink
Remove one atomic integer operation in SingleStatRecorder.
Browse files Browse the repository at this point in the history
This can be computed as sum after the fact. This also reduces the number of
times the same variable is hit.

PiperOrigin-RevId: 501884565
Change-Id: I03e642525b0f5ca5f98d7274f593fd4ab28d1f25
  • Loading branch information
meisterT authored and copybara-github committed Jan 13, 2023
1 parent ba7ff31 commit d429313
Showing 1 changed file with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.google.common.collect.Range;
import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
import com.google.devtools.build.lib.profiler.MetricData.HistogramElement;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAccumulator;
Expand All @@ -33,7 +32,6 @@ public class SingleStatRecorder implements StatRecorder {
private final int buckets;
private final Object description;
private final AtomicIntegerArray histogram;
private final AtomicInteger count = new AtomicInteger(0);
private final AtomicLong sum = new AtomicLong(0);
private final AtomicLong sumSquared = new AtomicLong(0);
private final LongAccumulator max = new LongAccumulator(Math::max, -1);
Expand All @@ -57,21 +55,23 @@ public MetricData snapshot() {
from = to;
}
result.add(new HistogramElement(Range.atLeast(from), histogram.get(histogram.length() - 1)));
int n = count.get();
int n = 0;
for (int i = 0; i < histogram.length(); i++) {
n += histogram.get(i);
}
double stddev;
if (n == 1) {
stddev = 0;
} else {
stddev = Math.sqrt((sumSquared.longValue() - sum.get() * sum.doubleValue() / n) / n);
}
return new MetricData(
description, result.build(), count.get(), sum.doubleValue() / n, stddev, max.intValue());
description, result.build(), n, sum.doubleValue() / n, stddev, max.intValue());
}

@Override
public void addStat(int duration, Object obj) {
int histogramBucket = Math.min(32 - Integer.numberOfLeadingZeros(duration), buckets - 1);
count.incrementAndGet();
sum.addAndGet(duration);
sumSquared.addAndGet(duration * duration);
max.accumulate(duration);
Expand Down

0 comments on commit d429313

Please sign in to comment.