Skip to content

Commit ee6f991

Browse files
Speedup sum aggregator (elastic#120241) (elastic#120380)
Using an object to track the two double fields is extremely inefficient here since the object can't be removed by escape analysis.
1 parent 142e2bd commit ee6f991

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

server/src/main/java/org/elasticsearch/search/aggregations/metrics/SumAggregator.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,33 @@ public void collect(int doc, long bucket) throws IOException {
6767

6868
@Override
6969
protected LeafBucketCollector getLeafCollector(NumericDoubleValues values, final LeafBucketCollector sub) {
70-
final CompensatedSum kahanSummation = new CompensatedSum(0, 0);
7170
return new LeafBucketCollectorBase(sub, values) {
7271
@Override
7372
public void collect(int doc, long bucket) throws IOException {
7473
if (values.advanceExact(doc)) {
7574
maybeGrow(bucket);
75+
var sums = SumAggregator.this.sums;
7676
// Compute the sum of double values with Kahan summation algorithm which is more
7777
// accurate than naive summation.
78-
kahanSummation.reset(sums.get(bucket), compensations.get(bucket));
79-
kahanSummation.add(values.doubleValue());
80-
compensations.set(bucket, kahanSummation.delta());
81-
sums.set(bucket, kahanSummation.value());
78+
double value = sums.get(bucket);
79+
// If the value is Inf or NaN, just add it to the running tally to "convert" to
80+
// Inf/NaN. This keeps the behavior bwc from before kahan summing
81+
double v = values.doubleValue();
82+
if (Double.isFinite(v) == false) {
83+
value = v + value;
84+
}
85+
86+
var compensations = SumAggregator.this.compensations;
87+
double delta = compensations.get(bucket);
88+
if (Double.isFinite(value)) {
89+
double correctedSum = v + delta;
90+
double updatedValue = value + correctedSum;
91+
delta = correctedSum - (updatedValue - value);
92+
value = updatedValue;
93+
compensations.set(bucket, delta);
94+
}
95+
96+
sums.set(bucket, value);
8297
}
8398
}
8499
};

0 commit comments

Comments
 (0)