Skip to content

Commit 6d161e3

Browse files
authored
Lower the memory footprint when creating DelayedBucket (#112519)
Trim list to size when creating delayed buckets.
1 parent bb10704 commit 6d161e3

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

docs/changelog/112519.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 112519
2+
summary: Lower the memory footprint when creating `DelayedBucket`
3+
area: Aggregations
4+
type: enhancement
5+
issues: []

server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/AbstractInternalTerms.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,14 @@ protected boolean lessThan(IteratorAndCurrent<B> a, IteratorAndCurrent<B> b) {
171171
pq.add(new IteratorAndCurrent<>(buckets.iterator()));
172172
}
173173
// list of buckets coming from different shards that have the same key
174-
List<B> sameTermBuckets = new ArrayList<>();
174+
ArrayList<B> sameTermBuckets = new ArrayList<>();
175175
B lastBucket = null;
176176
while (pq.size() > 0) {
177177
final IteratorAndCurrent<B> top = pq.top();
178178
assert lastBucket == null || cmp.compare(top.current(), lastBucket) >= 0;
179179
if (lastBucket != null && cmp.compare(top.current(), lastBucket) != 0) {
180180
// the key changed so bundle up the last key's worth of buckets
181+
sameTermBuckets.trimToSize();
181182
sink.accept(new DelayedBucket<>(AbstractInternalTerms.this::reduceBucket, reduceContext, sameTermBuckets));
182183
sameTermBuckets = new ArrayList<>();
183184
}
@@ -198,18 +199,20 @@ protected boolean lessThan(IteratorAndCurrent<B> a, IteratorAndCurrent<B> b) {
198199
}
199200

200201
if (sameTermBuckets.isEmpty() == false) {
202+
sameTermBuckets.trimToSize();
201203
sink.accept(new DelayedBucket<>(AbstractInternalTerms.this::reduceBucket, reduceContext, sameTermBuckets));
202204
}
203205
}
204206

205207
private void reduceLegacy(List<List<B>> bucketsList, AggregationReduceContext reduceContext, Consumer<DelayedBucket<B>> sink) {
206-
final Map<Object, List<B>> bucketMap = new HashMap<>();
208+
final Map<Object, ArrayList<B>> bucketMap = new HashMap<>();
207209
for (List<B> buckets : bucketsList) {
208210
for (B bucket : buckets) {
209211
bucketMap.computeIfAbsent(bucket.getKey(), k -> new ArrayList<>()).add(bucket);
210212
}
211213
}
212-
for (List<B> sameTermBuckets : bucketMap.values()) {
214+
for (ArrayList<B> sameTermBuckets : bucketMap.values()) {
215+
sameTermBuckets.trimToSize();
213216
sink.accept(new DelayedBucket<>(AbstractInternalTerms.this::reduceBucket, reduceContext, sameTermBuckets));
214217
}
215218
}

0 commit comments

Comments
 (0)