yew1eb commented on code in PR #3769:
URL: https://github.com/apache/celeborn/pull/3769#discussion_r3672682762
##########
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:
Confirmed -- `writeUtf8` reserves `utf8MaxBytes` (3 bytes/char), so an
exactly-sized buffer grows (36 -> 128 for a 30-char ASCII string in a quick
check). Wire bytes are unaffected; the updated asserts check `writerIndex(0`,
which is the invariant `encode()` actually guarantees. Pre-sizing header
buffers could avoid the `grow-and-copy`, but I'd keep this PR focused and
revisit if benchmarks show it matters.
--
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]