From 3d99ad872fe950069ed66434a7d379ddaac8c384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CChineseTony=E2=80=9C?= Date: Tue, 10 Mar 2026 22:07:46 +0800 Subject: [PATCH 1/2] reduce bytes copy --- .../protocol/RemotingSerializable.java | 23 +++++-------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingSerializable.java b/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingSerializable.java index c4e4da14684..235a53476b2 100644 --- a/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingSerializable.java +++ b/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingSerializable.java @@ -30,8 +30,7 @@ public static byte[] encode(final Object obj) { if (obj == null) { return null; } - final String json = toJson(obj, false); - return json.getBytes(CHARSET_UTF8); + return JSON.toJSONBytes(obj); } public static String toJson(final Object obj, boolean prettyFormat) { @@ -45,31 +44,22 @@ public static T decode(final byte[] data, Class classOfT) { if (data == null) { return null; } - return fromJson(data, classOfT); + return JSON.parseObject(data, classOfT); } public static List decodeList(final byte[] data, Class classOfT) { if (data == null) { return null; } - String json = new String(data, CHARSET_UTF8); - return JSON.parseArray(json, classOfT); + return JSON.parseArray(data, 0, data.length, CHARSET_UTF8, classOfT); } public static T fromJson(String json, Class classOfT) { return JSON.parseObject(json, classOfT); } - private static T fromJson(byte[] data, Class classOfT) { - return JSON.parseObject(data, classOfT); - } - public byte[] encode() { - final String json = this.toJson(); - if (json != null) { - return json.getBytes(CHARSET_UTF8); - } - return null; + return JSON.toJSONBytes(this); } /** @@ -79,8 +69,7 @@ public byte[] encode() { * @return serialized data. */ public byte[] encode(JSONWriter.Feature... features) { - final String json = JSON.toJSONString(this, features); - return json.getBytes(CHARSET_UTF8); + return JSON.toJSONBytes(this, features); } public String toJson() { @@ -90,4 +79,4 @@ public String toJson() { public String toJson(final boolean prettyFormat) { return toJson(this, prettyFormat); } -} +} \ No newline at end of file From dd06445d86ad6824a79960f2c7e6e7cda3225f65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CChineseTony=E2=80=9C?= Date: Thu, 12 Mar 2026 22:07:33 +0800 Subject: [PATCH 2/2] reduce bytes copy --- .../rocketmq/remoting/protocol/RemotingSerializable.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingSerializable.java b/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingSerializable.java index 235a53476b2..b1de45bf97c 100644 --- a/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingSerializable.java +++ b/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingSerializable.java @@ -30,7 +30,7 @@ public static byte[] encode(final Object obj) { if (obj == null) { return null; } - return JSON.toJSONBytes(obj); + return JSON.toJSONBytes(obj, CHARSET_UTF8); } public static String toJson(final Object obj, boolean prettyFormat) { @@ -59,7 +59,7 @@ public static T fromJson(String json, Class classOfT) { } public byte[] encode() { - return JSON.toJSONBytes(this); + return JSON.toJSONBytes(this, CHARSET_UTF8); } /** @@ -69,7 +69,7 @@ public byte[] encode() { * @return serialized data. */ public byte[] encode(JSONWriter.Feature... features) { - return JSON.toJSONBytes(this, features); + return JSON.toJSONBytes(this, CHARSET_UTF8, features); } public String toJson() {