Skip to content

Commit 7157974

Browse files
committed
Speedup time_series agg by caching current tsid ordinal, parent bucket ordinal and buck ordinal.
This avoids needlessly adding the same parent bucket ordinal or TSIDs to `BytesKeyedBucketOrds`. Relates to elastic#74660
1 parent 1c9a39c commit 7157974

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

modules/aggregations/src/main/java/org/elasticsearch/aggregations/bucket/timeseries/TimeSeriesAggregator.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,29 @@ protected void doClose() {
9292
protected LeafBucketCollector getLeafCollector(AggregationExecutionContext aggCtx, LeafBucketCollector sub) throws IOException {
9393
return new LeafBucketCollectorBase(sub, null) {
9494

95+
// This helps significantly reducing time spent attempting to add bucket + tsid combos that already were added.
96+
long currentTsidOrd = -1;
97+
long currentBucket = -1;
98+
long currentBucketOrdinal;
99+
95100
@Override
96101
public void collect(int doc, long bucket) throws IOException {
102+
if (currentBucket == bucket && currentTsidOrd == aggCtx.getTsidOrd()) {
103+
collectExistingBucket(sub, doc, currentBucketOrdinal);
104+
return;
105+
}
106+
97107
long bucketOrdinal = bucketOrds.add(bucket, aggCtx.getTsid());
98108
if (bucketOrdinal < 0) { // already seen
99109
bucketOrdinal = -1 - bucketOrdinal;
100110
collectExistingBucket(sub, doc, bucketOrdinal);
101111
} else {
102112
collectBucket(sub, doc, bucketOrdinal);
103113
}
114+
115+
currentBucketOrdinal = bucketOrdinal;
116+
currentTsidOrd = aggCtx.getTsidOrd();
117+
currentBucket = bucket;
104118
}
105119
};
106120
}

0 commit comments

Comments
 (0)