This is an automated email from the ASF dual-hosted git repository.
yuzhou 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 34648ed60f [ISSUE #10161] reduce bytes copy in json encode(#10162)
34648ed60f is described below
commit 34648ed60f7407310238b9d2c927fc792da3494a
Author: ChineseTony <[email protected]>
AuthorDate: Tue Mar 17 10:33:25 2026 +0800
[ISSUE #10161] reduce bytes copy in json encode(#10162)
* reduce bytes copy
* reduce bytes copy
---
.../remoting/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 c4e4da1468..b1de45bf97 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 abstract class RemotingSerializable {
if (obj == null) {
return null;
}
- final String json = toJson(obj, false);
- return json.getBytes(CHARSET_UTF8);
+ return JSON.toJSONBytes(obj, CHARSET_UTF8);
}
public static String toJson(final Object obj, boolean prettyFormat) {
@@ -45,31 +44,22 @@ public abstract class RemotingSerializable {
if (data == null) {
return null;
}
- return fromJson(data, classOfT);
+ return JSON.parseObject(data, classOfT);
}
public static <T> List<T> decodeList(final byte[] data, Class<T> 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> T fromJson(String json, Class<T> classOfT) {
return JSON.parseObject(json, classOfT);
}
- private static <T> T fromJson(byte[] data, Class<T> 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, CHARSET_UTF8);
}
/**
@@ -79,8 +69,7 @@ public abstract class RemotingSerializable {
* @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, CHARSET_UTF8, features);
}
public String toJson() {
@@ -90,4 +79,4 @@ public abstract class RemotingSerializable {
public String toJson(final boolean prettyFormat) {
return toJson(this, prettyFormat);
}
-}
+}
\ No newline at end of file