Skip to content

Commit ae8b2ac

Browse files
committed
reuse bucket when reduce terms buckets
Signed-off-by: bowenlan-amzn <bowenlan23@gmail.com>
1 parent 5041d43 commit ae8b2ac

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

server/src/main/java/org/opensearch/search/aggregations/bucket/terms/InternalTerms.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,13 +550,20 @@ protected B reduceBucket(List<B> buckets, ReduceContext context) {
550550
}
551551
}
552552

553-
InternalAggregations subAggs;
553+
InternalAggregations reducedSubAggs;
554554
if (aggregationsList.isEmpty()) {
555-
subAggs = InternalAggregations.EMPTY;
555+
reducedSubAggs = InternalAggregations.EMPTY;
556556
} else {
557-
subAggs = InternalAggregations.reduce(aggregationsList, context);
557+
reducedSubAggs = InternalAggregations.reduce(aggregationsList, context);
558558
}
559-
return createBucket(docCount, subAggs, docCountError, buckets.get(0));
559+
560+
// In-place mutation: reuse first bucket instead of allocating new one
561+
B firstBucket = buckets.get(0);
562+
Bucket<?> mutableBucket = (Bucket<?>) firstBucket;
563+
mutableBucket.docCount = docCount;
564+
mutableBucket.aggregations = reducedSubAggs;
565+
mutableBucket.docCountError = docCountError;
566+
return firstBucket;
560567
}
561568

562569
protected abstract void setDocCountError(long docCountError);

0 commit comments

Comments
 (0)