This is an automated email from the ASF dual-hosted git repository.
RongtongJin 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 801d6c0a2f [ISSUE #10467] Reduce per-message allocation in
createUniqID and valueOfMagicCode (#10469)
801d6c0a2f is described below
commit 801d6c0a2fef953c475cdc2e0916ef92109bf766
Author: Jiahua Wang <[email protected]>
AuthorDate: Thu Jun 11 20:26:12 2026 +0800
[ISSUE #10467] Reduce per-message allocation in createUniqID and
valueOfMagicCode (#10469)
- MessageClientIDSetter.createUniqID(): reuse a ThreadLocal<char[]>
instead of allocating a new char[LEN*2] on every send.
- MessageVersion.valueOfMagicCode(): replace Enum.values() array copy +
O(n) loop with direct if-else on the two known magic codes.
Co-authored-by: wangjiahua.wjh <[email protected]>
Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
---
.../common/message/MessageClientIDSetter.java | 12 ++++---
.../rocketmq/common/message/MessageVersion.java | 13 +++----
.../common/message/MessageVersionTest.java | 40 ++++++++++++++++++++++
3 files changed, 54 insertions(+), 11 deletions(-)
diff --git
a/common/src/main/java/org/apache/rocketmq/common/message/MessageClientIDSetter.java
b/common/src/main/java/org/apache/rocketmq/common/message/MessageClientIDSetter.java
index 4ae5ef59d6..4953428d1d 100644
---
a/common/src/main/java/org/apache/rocketmq/common/message/MessageClientIDSetter.java
+++
b/common/src/main/java/org/apache/rocketmq/common/message/MessageClientIDSetter.java
@@ -111,9 +111,11 @@ public class MessageClientIDSetter {
return value & 0x0000FFFF;
}
+ private static final ThreadLocal<char[]> UNIQ_ID_BUF =
ThreadLocal.withInitial(() -> new char[LEN * 2]);
+
public static String createUniqID() {
- char[] sb = new char[LEN * 2];
- System.arraycopy(FIX_STRING, 0, sb, 0, FIX_STRING.length);
+ char[] buf = UNIQ_ID_BUF.get();
+ System.arraycopy(FIX_STRING, 0, buf, 0, FIX_STRING.length);
long current = System.currentTimeMillis();
if (current >= nextStartTime) {
setStartTime(current);
@@ -124,10 +126,10 @@ public class MessageClientIDSetter {
diff = 0;
}
int pos = FIX_STRING.length;
- UtilAll.writeInt(sb, pos, diff);
+ UtilAll.writeInt(buf, pos, diff);
pos += 8;
- UtilAll.writeShort(sb, pos, COUNTER.getAndIncrement());
- return new String(sb);
+ UtilAll.writeShort(buf, pos, COUNTER.getAndIncrement());
+ return new String(buf);
}
public static void setUniqID(final Message msg) {
diff --git
a/common/src/main/java/org/apache/rocketmq/common/message/MessageVersion.java
b/common/src/main/java/org/apache/rocketmq/common/message/MessageVersion.java
index bb1c2e8d64..68580f0110 100644
---
a/common/src/main/java/org/apache/rocketmq/common/message/MessageVersion.java
+++
b/common/src/main/java/org/apache/rocketmq/common/message/MessageVersion.java
@@ -71,13 +71,14 @@ public enum MessageVersion {
}
public static MessageVersion valueOfMagicCode(int magicCode) {
- for (MessageVersion version : MessageVersion.values()) {
- if (version.getMagicCode() == magicCode) {
- return version;
- }
+ switch (magicCode) {
+ case MessageDecoder.MESSAGE_MAGIC_CODE:
+ return MESSAGE_VERSION_V1;
+ case MessageDecoder.MESSAGE_MAGIC_CODE_V2:
+ return MESSAGE_VERSION_V2;
+ default:
+ throw new IllegalArgumentException("Invalid magicCode " +
magicCode);
}
-
- throw new IllegalArgumentException("Invalid magicCode " + magicCode);
}
public int getMagicCode() {
diff --git
a/common/src/test/java/org/apache/rocketmq/common/message/MessageVersionTest.java
b/common/src/test/java/org/apache/rocketmq/common/message/MessageVersionTest.java
new file mode 100644
index 0000000000..9cc5f3b9b0
--- /dev/null
+++
b/common/src/test/java/org/apache/rocketmq/common/message/MessageVersionTest.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.rocketmq.common.message;
+
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+public class MessageVersionTest {
+
+ @Test
+ public void testValueOfMagicCode() {
+ for (MessageVersion version : MessageVersion.values()) {
+ assertThat(MessageVersion.valueOfMagicCode(version.getMagicCode()))
+ .isEqualTo(version);
+ }
+ }
+
+ @Test
+ public void testValueOfMagicCodeInvalid() {
+ assertThatThrownBy(() -> MessageVersion.valueOfMagicCode(0xDEADBEEF))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("Invalid magicCode");
+ }
+}