GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Avoid allocation in HttpObjectEncoder.addEncodedLengthHex method (#16… · netty/netty@ee12997

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http/src/main/java/io/netty/handler/codec/http
    • HttpObjectEncoder.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectEncoder.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectEncoder.java
+14-3Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,13 +561,24 @@ private static void encodedChunkedFileRegionContent(ChannelHandlerContext ctx, F
561561
}
562562

563563
private static void addEncodedLengthHex(ChannelHandlerContext ctx, long contentLength, List<Object> out) {
564-
String lengthHex = Long.toHexString(contentLength);
565-
ByteBuf buf = ctx.alloc().buffer(lengthHex.length() + 2);
566-
buf.writeCharSequence(lengthHex, CharsetUtil.US_ASCII);
564+
// logic is from Long.toHexString() but we want to avoid creating a String
565+
int hexLen = contentLength == 0 ? 1 : (Long.SIZE - Long.numberOfLeadingZeros(contentLength) + 3) >>> 2;
566+
ByteBuf buf = ctx.alloc().buffer(hexLen + 2); // +2 for CRLF
567+
writeHexAscii(buf, contentLength, hexLen);
567568
ByteBufUtil.writeShortBE(buf, CRLF_SHORT);
568569
out.add(buf);
569570
}
570571

572+
private static final byte[] HEX = {
573+
'0', '1', '2', '3', '4', '5', '6', '7',
574+
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
575+
};
576+
private static void writeHexAscii(ByteBuf out, long contentLength, int hexLen) {
577+
for (int shift = (hexLen - 1) << 2; shift >= 0; shift -= 4) {
578+
out.writeByte(HEX[(int) ((contentLength >>> shift) & 0xF)]);
579+
}
580+
}
581+
571582
/**
572583
* Allows to sanitize headers of the message before encoding these.
573584
*/

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.