Skip to content

Commit 53149d4

Browse files
authored
GH-3764: Replace LinkedList with ArrayList in listener container for records
Fixes: #3764 Issue link: #3764 Acknowledging an index in a batch has quadratic time `N(N+1)/2` ~ `N^2` Batch consumers operate on a `LinkedList` of records. If the consumer uses `MANUAL_IMMEDIATE` ack mode, and the listener invokes `acknowledgement.acknowledge(index)` where index is relatively big (e.g. when processing batches of `100k`), performance takes hit because of the linear lookup `records.get(i)` in a loop Signed-off-by: Janek Lasocki-Biczysko <[email protected]> [[email protected]: improve commit message] **Auto-cherry-pick to `3.3.x`** Signed-off-by: Artem Bilan <[email protected]>
1 parent 7dfac08 commit 53149d4

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

spring-kafka/src/main/java/org/springframework/kafka/listener/KafkaMessageListenerContainer.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
* @author Sanghyeok An
173173
* @author Christian Fredriksson
174174
* @author Timofey Barabanov
175+
* @author Janek Lasocki-Biczysko
175176
*/
176177
public class KafkaMessageListenerContainer<K, V> // NOSONAR line count
177178
extends AbstractMessageListenerContainer<K, V> implements ConsumerPauseResumeEventPublisher {
@@ -2237,12 +2238,9 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
22372238
}
22382239

22392240
private List<ConsumerRecord<K, V>> createRecordList(final ConsumerRecords<K, V> records) {
2240-
Iterator<ConsumerRecord<K, V>> iterator = records.iterator();
2241-
List<ConsumerRecord<K, V>> list = new LinkedList<>();
2242-
while (iterator.hasNext()) {
2243-
list.add(iterator.next());
2244-
}
2245-
return list;
2241+
List<ConsumerRecord<K, V>> recordList = new ArrayList<>(records.count());
2242+
records.forEach(recordList::add);
2243+
return recordList;
22462244
}
22472245

22482246
/**

0 commit comments

Comments
 (0)