You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by li...@apache.org on 2023/01/03 02:32:43 UTC

[rocketmq] branch develop updated: [ISSUE #5802]Optimize HashMap and HasSet invoke resize method (#5803)

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

lizhanhui 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 d070d467a [ISSUE #5802]Optimize HashMap and HasSet invoke resize method (#5803)
d070d467a is described below

commit d070d467a6e69f810c8f078a30ccfb44cf76afd5
Author: mxsm <lj...@gmail.com>
AuthorDate: Tue Jan 3 10:32:36 2023 +0800

    [ISSUE #5802]Optimize HashMap and HasSet invoke resize method (#5803)
---
 .../java/org/apache/rocketmq/common/message/MessageDecoder.java     | 4 ++--
 .../java/org/apache/rocketmq/remoting/protocol/RemotingCommand.java | 4 ++--
 .../org/apache/rocketmq/remoting/protocol/RocketMQSerializable.java | 6 +++---
 .../apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java    | 4 +++-
 4 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/common/src/main/java/org/apache/rocketmq/common/message/MessageDecoder.java b/common/src/main/java/org/apache/rocketmq/common/message/MessageDecoder.java
index 082e1df13..6de0b69fb 100644
--- a/common/src/main/java/org/apache/rocketmq/common/message/MessageDecoder.java
+++ b/common/src/main/java/org/apache/rocketmq/common/message/MessageDecoder.java
@@ -278,7 +278,7 @@ public class MessageDecoder {
     /**
      * Encode without store timestamp and store host, skip blank msg.
      *
-     * @param messageExt msg
+     * @param messageExt   msg
      * @param needCompress need compress or not
      * @return byte array
      * @throws IOException when compress failed
@@ -608,7 +608,7 @@ public class MessageDecoder {
     }
 
     public static Map<String, String> string2messageProperties(final String properties) {
-        Map<String, String> map = new HashMap<>();
+        Map<String, String> map = new HashMap<>(128);
         if (properties != null) {
             int len = properties.length();
             int index = 0;
diff --git a/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingCommand.java b/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingCommand.java
index 3f50f4a45..0d694145c 100644
--- a/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingCommand.java
+++ b/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingCommand.java
@@ -490,7 +490,7 @@ public class RemotingCommand {
         // header data
         result.put(headerData);
 
-        ((Buffer)result).flip();
+        ((Buffer) result).flip();
 
         return result;
     }
@@ -597,7 +597,7 @@ public class RemotingCommand {
 
     public void addExtField(String key, String value) {
         if (null == extFields) {
-            extFields = new HashMap<>();
+            extFields = new HashMap<>(256);
         }
         extFields.put(key, value);
     }
diff --git a/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RocketMQSerializable.java b/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RocketMQSerializable.java
index 4cf031e78..25ebbaafd 100644
--- a/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RocketMQSerializable.java
+++ b/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RocketMQSerializable.java
@@ -94,7 +94,6 @@ public class RocketMQSerializable {
         return out.writerIndex() - beginIndex;
     }
 
-
     public static byte[] rocketMQProtocolEncode(RemotingCommand cmd) {
         // String remark
         byte[] remarkBytes = null;
@@ -203,7 +202,8 @@ public class RocketMQSerializable {
         return length;
     }
 
-    public static RemotingCommand rocketMQProtocolDecode(final ByteBuf headerBuffer, int headerLen) throws RemotingCommandException {
+    public static RemotingCommand rocketMQProtocolDecode(final ByteBuf headerBuffer,
+        int headerLen) throws RemotingCommandException {
         RemotingCommand cmd = new RemotingCommand();
         // int code(~32767)
         cmd.setCode(headerBuffer.readShort());
@@ -231,7 +231,7 @@ public class RocketMQSerializable {
 
     public static HashMap<String, String> mapDeserialize(ByteBuf byteBuffer, int len) throws RemotingCommandException {
 
-        HashMap<String, String> map = new HashMap<>();
+        HashMap<String, String> map = new HashMap<>(128);
         int endIndex = byteBuffer.readerIndex() + len;
 
         while (byteBuffer.readerIndex() < endIndex) {
diff --git a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java
index f2b421ecd..ed694799c 100644
--- a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java
+++ b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java
@@ -351,7 +351,9 @@ public class AutoSwitchHAService extends DefaultHAService {
     }
 
     public synchronized Set<String> getSyncStateSet() {
-        return new HashSet<>(this.syncStateSet);
+        HashSet<String> set = new HashSet<>(this.syncStateSet.size());
+        set.addAll(this.syncStateSet);
+        return set;
     }
 
     public void truncateEpochFilePrefix(final long offset) {