Skip to content

Commit

Permalink
Speedup time_series agg by caching current tsid ordinal, parent bucke…
Browse files Browse the repository at this point in the history
…t ordinal and buck ordinal.

This avoids needlessly adding the same parent bucket ordinal or TSIDs to `BytesKeyedBucketOrds`.

Relates to elastic#74660
  • Loading branch information
martijnvg committed Nov 22, 2022
1 parent 1c9a39c commit 7157974
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,29 @@ protected void doClose() {
protected LeafBucketCollector getLeafCollector(AggregationExecutionContext aggCtx, LeafBucketCollector sub) throws IOException {
return new LeafBucketCollectorBase(sub, null) {

// This helps significantly reducing time spent attempting to add bucket + tsid combos that already were added.
long currentTsidOrd = -1;
long currentBucket = -1;
long currentBucketOrdinal;

@Override
public void collect(int doc, long bucket) throws IOException {
if (currentBucket == bucket && currentTsidOrd == aggCtx.getTsidOrd()) {
collectExistingBucket(sub, doc, currentBucketOrdinal);
return;
}

long bucketOrdinal = bucketOrds.add(bucket, aggCtx.getTsid());
if (bucketOrdinal < 0) { // already seen
bucketOrdinal = -1 - bucketOrdinal;
collectExistingBucket(sub, doc, bucketOrdinal);
} else {
collectBucket(sub, doc, bucketOrdinal);
}

currentBucketOrdinal = bucketOrdinal;
currentTsidOrd = aggCtx.getTsidOrd();
currentBucket = bucket;
}
};
}
Expand Down

0 comments on commit 7157974

Please sign in to comment.