@@ -171,13 +171,14 @@ protected boolean lessThan(IteratorAndCurrent<B> a, IteratorAndCurrent<B> b) {
171
171
pq .add (new IteratorAndCurrent <>(buckets .iterator ()));
172
172
}
173
173
// list of buckets coming from different shards that have the same key
174
- List <B > sameTermBuckets = new ArrayList <>();
174
+ ArrayList <B > sameTermBuckets = new ArrayList <>();
175
175
B lastBucket = null ;
176
176
while (pq .size () > 0 ) {
177
177
final IteratorAndCurrent <B > top = pq .top ();
178
178
assert lastBucket == null || cmp .compare (top .current (), lastBucket ) >= 0 ;
179
179
if (lastBucket != null && cmp .compare (top .current (), lastBucket ) != 0 ) {
180
180
// the key changed so bundle up the last key's worth of buckets
181
+ sameTermBuckets .trimToSize ();
181
182
sink .accept (new DelayedBucket <>(AbstractInternalTerms .this ::reduceBucket , reduceContext , sameTermBuckets ));
182
183
sameTermBuckets = new ArrayList <>();
183
184
}
@@ -198,18 +199,20 @@ protected boolean lessThan(IteratorAndCurrent<B> a, IteratorAndCurrent<B> b) {
198
199
}
199
200
200
201
if (sameTermBuckets .isEmpty () == false ) {
202
+ sameTermBuckets .trimToSize ();
201
203
sink .accept (new DelayedBucket <>(AbstractInternalTerms .this ::reduceBucket , reduceContext , sameTermBuckets ));
202
204
}
203
205
}
204
206
205
207
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 <>();
207
209
for (List <B > buckets : bucketsList ) {
208
210
for (B bucket : buckets ) {
209
211
bucketMap .computeIfAbsent (bucket .getKey (), k -> new ArrayList <>()).add (bucket );
210
212
}
211
213
}
212
- for (List <B > sameTermBuckets : bucketMap .values ()) {
214
+ for (ArrayList <B > sameTermBuckets : bucketMap .values ()) {
215
+ sameTermBuckets .trimToSize ();
213
216
sink .accept (new DelayedBucket <>(AbstractInternalTerms .this ::reduceBucket , reduceContext , sameTermBuckets ));
214
217
}
215
218
}
0 commit comments