yew1eb commented on code in PR #3769:
URL: https://github.com/apache/celeborn/pull/3769#discussion_r3719616052
##########
common/src/main/java/org/apache/celeborn/common/network/protocol/Encoders.java:
##########
@@ -20,20 +20,20 @@
import java.nio.charset.StandardCharsets;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
/** Provides a canonical set of Encoders for simple types. */
public class Encoders {
/** Strings are encoded with their length followed by UTF-8 bytes. */
public static class Strings {
public static int encodedLength(String s) {
- return 4 + s.getBytes(StandardCharsets.UTF_8).length;
+ return 4 + ByteBufUtil.utf8Bytes(s);
}
public static void encode(ByteBuf buf, String s) {
- byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
- buf.writeInt(bytes.length);
- buf.writeBytes(bytes);
+ buf.writeInt(ByteBufUtil.utf8Bytes(s));
+ ByteBufUtil.writeUtf8(buf, s);
Review Comment:
Thanks for the suggestions!
For `encode`, adopted the backfill approach in 0cd388ac — write a zero
length slot, `writeUtf8`, then `setInt` the real byte length, so the string is
traversed only once. `encodedLength` still uses `utf8Bytes(s)` since message
framing needs the exact total length up front.
For `decode`, tracked as a separate issue CELEBORN-2393 to keep this PR
focused on the encode path.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]