This is an automated email from the ASF dual-hosted git repository.

lollipop pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git


The following commit(s) were added to refs/heads/develop by this push:
     new 5a3de926b Optimize updateSubscription check exist loop (#7190)
5a3de926b is described below

commit 5a3de926b816db5a121c1d788430072a3bc942ae
Author: Zhouxiang Zhan <[email protected]>
AuthorDate: Wed Aug 16 20:52:53 2023 +0800

    Optimize updateSubscription check exist loop (#7190)
---
 .../rocketmq/broker/client/ConsumerGroupInfo.java       | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)

diff --git 
a/broker/src/main/java/org/apache/rocketmq/broker/client/ConsumerGroupInfo.java 
b/broker/src/main/java/org/apache/rocketmq/broker/client/ConsumerGroupInfo.java
index 867b9c720..1ea58c125 100644
--- 
a/broker/src/main/java/org/apache/rocketmq/broker/client/ConsumerGroupInfo.java
+++ 
b/broker/src/main/java/org/apache/rocketmq/broker/client/ConsumerGroupInfo.java
@@ -18,6 +18,7 @@ package org.apache.rocketmq.broker.client;
 
 import io.netty.channel.Channel;
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map.Entry;
@@ -172,7 +173,7 @@ public class ConsumerGroupInfo {
      */
     public boolean updateSubscription(final Set<SubscriptionData> subList) {
         boolean updated = false;
-
+        Set<String> topicSet = new HashSet<>();
         for (SubscriptionData sub : subList) {
             SubscriptionData old = this.subscriptionTable.get(sub.getTopic());
             if (old == null) {
@@ -194,22 +195,16 @@ public class ConsumerGroupInfo {
 
                 this.subscriptionTable.put(sub.getTopic(), sub);
             }
+            // Add all new topics to the HashSet
+            topicSet.add(sub.getTopic());
         }
 
         Iterator<Entry<String, SubscriptionData>> it = 
this.subscriptionTable.entrySet().iterator();
         while (it.hasNext()) {
             Entry<String, SubscriptionData> next = it.next();
             String oldTopic = next.getKey();
-
-            boolean exist = false;
-            for (SubscriptionData sub : subList) {
-                if (sub.getTopic().equals(oldTopic)) {
-                    exist = true;
-                    break;
-                }
-            }
-
-            if (!exist) {
+            // Check HashSet with O(1) time complexity
+            if (!topicSet.contains(oldTopic)) {
                 log.warn("subscription changed, group: {} remove topic {} {}",
                     this.groupName,
                     oldTopic,

Reply via email to