Skip to content

Commit

Permalink
Utilize ByteBuf#indexOf (#13974)
Browse files Browse the repository at this point in the history
Motivation:
`ByteBuf#indexOf` employees advanced algorithm to search a given byte.

Modification:
Utilize `ByteBuf#indexOf` instead `forEachByte`.

Result:
Better performance.
  • Loading branch information
jchrys committed Apr 15, 2024
1 parent 9f44b97 commit b9af2e6
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ static HAProxyMessage decodeHeader(ByteBuf header) {
Math.min(addressInfoLen, header.readableBytes()) + " bytes (expected: 216+ bytes)");
}
int startIdx = header.readerIndex();
int addressEnd = header.forEachByte(startIdx, 108, ByteProcessor.FIND_NUL);
int addressEnd = header.indexOf(startIdx, startIdx + 108, (byte) 0); // FIND_NUL
if (addressEnd == -1) {
addressLen = 108;
} else {
Expand All @@ -199,7 +199,7 @@ static HAProxyMessage decodeHeader(ByteBuf header) {

startIdx += 108;

addressEnd = header.forEachByte(startIdx, 108, ByteProcessor.FIND_NUL);
addressEnd = header.indexOf(startIdx, startIdx + 108, (byte) 0); // FIND_NUL
if (addressEnd == -1) {
addressLen = 108;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ private static ByteBuf readLine(ByteBuf in) {
if (!in.isReadable(RedisConstants.EOL_LENGTH)) {
return null;
}
final int lfIndex = in.forEachByte(ByteProcessor.FIND_LF);
final int lfIndex = in.indexOf(in.readerIndex(), in.writerIndex(), (byte) '\n');
if (lfIndex < 0) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ private void fail(final ChannelHandlerContext ctx, String length) {
*/
private int findEndOfLine(final ByteBuf buffer) {
int totalLength = buffer.readableBytes();
int i = buffer.forEachByte(buffer.readerIndex() + offset, totalLength - offset, ByteProcessor.FIND_LF);
int i = buffer.indexOf(buffer.readerIndex() + offset,
buffer.readerIndex() + totalLength, (byte) '\n');
if (i >= 0) {
offset = 0;
if (i > 0 && buffer.getByte(i - 1) == '\r') {
Expand Down

0 comments on commit b9af2e6

Please sign in to comment.