Skip to content

Commit a12aceb

Browse files
committed
Refactor newTopics to remove nested loops
Replace O(n²) nested iteration with O(n) map-based lookups for filtering topics Use topicNameToMapKey to efficiently resolve duplicate topic names This improves application startup time and scalability when managing a large number of topics, while preserving the original filtering logic Resolves: #4025 Signed-off-by: Choi Wang Gyu <[email protected]>
1 parent a7cf43d commit a12aceb

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

spring-kafka/src/main/java/org/springframework/kafka/core/KafkaAdmin.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.Collection;
2323
import java.util.Collections;
2424
import java.util.HashMap;
25-
import java.util.Iterator;
2625
import java.util.LinkedList;
2726
import java.util.List;
2827
import java.util.Map;
@@ -81,6 +80,7 @@
8180
* @author Valentina Armenise
8281
* @author Anders Swanson
8382
* @author Omer Celik
83+
* @author Choi Wang Gyu
8484
*
8585
* @since 1.3
8686
*/
@@ -332,31 +332,25 @@ protected Collection<NewTopic> newTopics() {
332332
Map<String, NewTopic> topicsForRetry = newTopicsMap.entrySet().stream()
333333
.filter(entry -> entry.getValue() instanceof TopicForRetryable)
334334
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
335+
Map<String, String> topicNameToMapKey = new HashMap<>();
336+
for (Entry<String, NewTopic> entry : newTopicsMap.entrySet()) {
337+
topicNameToMapKey.put(entry.getValue().name(), entry.getKey());
338+
}
339+
335340
for (Entry<String, NewTopic> entry : topicsForRetry.entrySet()) {
336-
Iterator<Entry<String, NewTopic>> iterator = newTopicsMap.entrySet().iterator();
337-
boolean remove = false;
338-
while (iterator.hasNext()) {
339-
Entry<String, NewTopic> nt = iterator.next();
340-
// if we have a NewTopic and TopicForRetry with the same name, remove the latter
341-
if (nt.getValue().name().equals(entry.getValue().name())
342-
&& !(nt.getValue() instanceof TopicForRetryable)) {
343-
344-
remove = true;
345-
break;
341+
String retryTopicName = entry.getValue().name();
342+
String keyInNewTopicsMap = topicNameToMapKey.get(retryTopicName);
343+
if (keyInNewTopicsMap != null) {
344+
NewTopic existing = newTopicsMap.get(keyInNewTopicsMap);
345+
if (!(existing instanceof TopicForRetryable)) {
346+
newTopicsMap.remove(keyInNewTopicsMap);
346347
}
347348
}
348-
if (remove) {
349-
newTopicsMap.remove(entry.getKey());
350-
}
351349
}
352-
Iterator<Entry<String, NewTopic>> iterator = newTopicsMap.entrySet().iterator();
353-
while (iterator.hasNext()) {
354-
Entry<String, NewTopic> next = iterator.next();
355-
if (!this.createOrModifyTopic.test(next.getValue())) {
356-
iterator.remove();
357-
}
358-
}
359-
return new ArrayList<>(newTopicsMap.values());
350+
Map<String, NewTopic> filteredMap = newTopicsMap.entrySet().stream()
351+
.filter(entry -> this.createOrModifyTopic.test(entry.getValue()))
352+
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
353+
return new ArrayList<>(filteredMap.values());
360354
}
361355

362356
@Override

0 commit comments

Comments
 (0)