Remove ByteBuf.readBytes(int) calls when possible

Motivation:

We use ByteBuf.readBytes(int) in various places where we could either remove it completely or use readSlice(int).retain().

Modifications:

- Remove ByteBuf.readBytes(int) when possible or replace by readSlice(int).retain().

Result:

Faster code.
This commit is contained in:
Norman Maurer 2016-04-07 10:16:26 +02:00
parent 1b4a5609d3
commit d081851156
6 changed files with 22 additions and 12 deletions

View File

@ -437,9 +437,10 @@ public final class MqttDecoder extends ReplayingDecoder<DecoderState> {
numberOfBytesConsumed += size; numberOfBytesConsumed += size;
return new Result<String>(null, numberOfBytesConsumed); return new Result<String>(null, numberOfBytesConsumed);
} }
ByteBuf buf = buffer.readBytes(size); String s = buffer.toString(buffer.readerIndex(), size, CharsetUtil.UTF_8);
buffer.skipBytes(size);
numberOfBytesConsumed += size; numberOfBytesConsumed += size;
return new Result<String>(buf.toString(CharsetUtil.UTF_8), numberOfBytesConsumed); return new Result<String>(s, numberOfBytesConsumed);
} }
private static Result<Integer> decodeMsbLsb(ByteBuf buffer) { private static Result<Integer> decodeMsbLsb(ByteBuf buffer) {

View File

@ -19,7 +19,6 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder; import io.netty.handler.codec.ReplayingDecoder;
import io.netty.handler.codec.socks.SocksAuthRequestDecoder.State; import io.netty.handler.codec.socks.SocksAuthRequestDecoder.State;
import io.netty.util.CharsetUtil;
import java.util.List; import java.util.List;
@ -51,12 +50,12 @@ public class SocksAuthRequestDecoder extends ReplayingDecoder<State> {
} }
case READ_USERNAME: { case READ_USERNAME: {
fieldLength = byteBuf.readByte(); fieldLength = byteBuf.readByte();
username = byteBuf.readBytes(fieldLength).toString(CharsetUtil.US_ASCII); username = SocksCommonUtils.readUsAscii(byteBuf, fieldLength);
checkpoint(State.READ_PASSWORD); checkpoint(State.READ_PASSWORD);
} }
case READ_PASSWORD: { case READ_PASSWORD: {
fieldLength = byteBuf.readByte(); fieldLength = byteBuf.readByte();
password = byteBuf.readBytes(fieldLength).toString(CharsetUtil.US_ASCII); password = SocksCommonUtils.readUsAscii(byteBuf, fieldLength);
msg = new SocksAuthRequest(username, password); msg = new SocksAuthRequest(username, password);
} }
} }

View File

@ -19,7 +19,6 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder; import io.netty.handler.codec.ReplayingDecoder;
import io.netty.handler.codec.socks.SocksCmdRequestDecoder.State; import io.netty.handler.codec.socks.SocksCmdRequestDecoder.State;
import io.netty.util.CharsetUtil;
import java.util.List; import java.util.List;
@ -69,13 +68,15 @@ public class SocksCmdRequestDecoder extends ReplayingDecoder<State> {
} }
case DOMAIN: { case DOMAIN: {
fieldLength = byteBuf.readByte(); fieldLength = byteBuf.readByte();
host = byteBuf.readBytes(fieldLength).toString(CharsetUtil.US_ASCII); host = SocksCommonUtils.readUsAscii(byteBuf, fieldLength);
port = byteBuf.readUnsignedShort(); port = byteBuf.readUnsignedShort();
msg = new SocksCmdRequest(cmdType, addressType, host, port); msg = new SocksCmdRequest(cmdType, addressType, host, port);
break; break;
} }
case IPv6: { case IPv6: {
host = SocksCommonUtils.ipv6toStr(byteBuf.readBytes(16).array()); byte[] bytes = new byte[16];
byteBuf.readBytes(bytes);
host = SocksCommonUtils.ipv6toStr(bytes);
port = byteBuf.readUnsignedShort(); port = byteBuf.readUnsignedShort();
msg = new SocksCmdRequest(cmdType, addressType, host, port); msg = new SocksCmdRequest(cmdType, addressType, host, port);
break; break;

View File

@ -19,7 +19,6 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder; import io.netty.handler.codec.ReplayingDecoder;
import io.netty.handler.codec.socks.SocksCmdResponseDecoder.State; import io.netty.handler.codec.socks.SocksCmdResponseDecoder.State;
import io.netty.util.CharsetUtil;
import java.util.List; import java.util.List;
@ -68,13 +67,15 @@ public class SocksCmdResponseDecoder extends ReplayingDecoder<State> {
} }
case DOMAIN: { case DOMAIN: {
fieldLength = byteBuf.readByte(); fieldLength = byteBuf.readByte();
host = byteBuf.readBytes(fieldLength).toString(CharsetUtil.US_ASCII); host = SocksCommonUtils.readUsAscii(byteBuf, fieldLength);
port = byteBuf.readUnsignedShort(); port = byteBuf.readUnsignedShort();
msg = new SocksCmdResponse(cmdStatus, addressType, host, port); msg = new SocksCmdResponse(cmdStatus, addressType, host, port);
break; break;
} }
case IPv6: { case IPv6: {
host = SocksCommonUtils.ipv6toStr(byteBuf.readBytes(16).array()); byte[] bytes = new byte[16];
byteBuf.readBytes(bytes);
host = SocksCommonUtils.ipv6toStr(bytes);
port = byteBuf.readUnsignedShort(); port = byteBuf.readUnsignedShort();
msg = new SocksCmdResponse(cmdStatus, addressType, host, port); msg = new SocksCmdResponse(cmdStatus, addressType, host, port);
break; break;

View File

@ -15,6 +15,8 @@
*/ */
package io.netty.handler.codec.socks; package io.netty.handler.codec.socks;
import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
import io.netty.util.internal.StringUtil; import io.netty.util.internal.StringUtil;
final class SocksCommonUtils { final class SocksCommonUtils {
@ -103,4 +105,10 @@ final class SocksCommonUtils {
private static void appendHextet(StringBuilder sb, byte[] src, int i) { private static void appendHextet(StringBuilder sb, byte[] src, int i) {
StringUtil.toHexString(sb, src, i << 1, 2); StringUtil.toHexString(sb, src, i << 1, 2);
} }
static String readUsAscii(ByteBuf buffer, int length) {
String s = buffer.toString(buffer.readerIndex(), length, CharsetUtil.US_ASCII);
buffer.skipBytes(length);
return s;
}
} }

View File

@ -60,7 +60,7 @@ public class ProtobufVarint32FrameDecoder extends ByteToMessageDecoder {
in.resetReaderIndex(); in.resetReaderIndex();
return; return;
} else { } else {
out.add(in.readBytes(length)); out.add(in.readSlice(length).retain());
return; return;
} }
} }