Fix performance regression which was caused by calling the fireInboundBufferUpdated() a way to often

* Also use a ThreadLocal for the decoder buffer to safe space as it is cheap
This commit is contained in:
Norman Maurer 2013-04-03 17:07:52 +02:00
parent c3559ddbda
commit 9828267165
2 changed files with 111 additions and 112 deletions

View File

@ -45,18 +45,23 @@ public abstract class ByteToMessageDecoder
private volatile boolean singleDecode;
private boolean decodeWasNull;
private MessageBuf<Object> decoderOutput;
private static final ThreadLocal<MessageBuf<Object>> decoderOutput =
new ThreadLocal<MessageBuf<Object>>() {
@Override
protected MessageBuf<Object> initialValue() {
return Unpooled.messageBuffer();
}
};
@Override
public ByteBuf newInboundBuffer(ChannelHandlerContext ctx) throws Exception {
decoderOutput = Unpooled.messageBuffer();
return super.newInboundBuffer(ctx);
}
@Override
public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
super.freeInboundBuffer(ctx);
decoderOutput.release();
}
/**
@ -130,12 +135,12 @@ public abstract class ByteToMessageDecoder
protected void callDecode(ChannelHandlerContext ctx, ByteBuf in) {
boolean wasNull = false;
boolean decoded = false;
MessageBuf<Object> out = decoderOutput();
assert out.isEmpty();
try {
while (in.isReadable()) {
try {
int outSize = out.size();
@ -165,6 +170,8 @@ public abstract class ByteToMessageDecoder
} else {
throw new DecoderException(t);
}
}
}
} finally {
for (;;) {
Object msg = out.poll();
@ -175,13 +182,6 @@ public abstract class ByteToMessageDecoder
ChannelHandlerUtil.addToNextInboundBuffer(ctx, msg);
}
if (decoded) {
decoded = false;
ctx.fireInboundBufferUpdated();
}
}
}
if (decoded) {
decodeWasNull = false;
ctx.fireInboundBufferUpdated();
@ -191,6 +191,7 @@ public abstract class ByteToMessageDecoder
}
}
}
}
/**
* Decode the from one {@link ByteBuf} to an other. This method will be called till either the input
@ -217,7 +218,7 @@ public abstract class ByteToMessageDecoder
}
final MessageBuf<Object> decoderOutput() {
return decoderOutput;
return decoderOutput.get();
}
}

View File

@ -406,10 +406,13 @@ public abstract class ReplayingDecoder<S> extends ByteToMessageDecoder {
@Override
protected void callDecode(ChannelHandlerContext ctx, ByteBuf buf) {
boolean wasNull = false;
ByteBuf in = cumulation;
MessageBuf<Object> out = decoderOutput();
boolean decoded = false;
assert out.isEmpty();
try {
while (in.isReadable()) {
try {
int oldReaderIndex = checkpoint = in.readerIndex();
@ -454,8 +457,9 @@ public abstract class ReplayingDecoder<S> extends ByteToMessageDecoder {
} else {
throw new DecoderException(t);
}
}
}
} finally {
for (;;) {
Object msg = out.poll();
if (msg == null) {
@ -464,13 +468,6 @@ public abstract class ReplayingDecoder<S> extends ByteToMessageDecoder {
decoded = true;
ChannelHandlerUtil.addToNextInboundBuffer(ctx, msg);
}
if (decoded) {
decoded = false;
ctx.fireInboundBufferUpdated();
}
}
}
if (decoded) {
decodeWasNull = false;
ctx.fireInboundBufferUpdated();
@ -480,6 +477,7 @@ public abstract class ReplayingDecoder<S> extends ByteToMessageDecoder {
}
}
}
}
@Override
public void channelReadSuspended(ChannelHandlerContext ctx) throws Exception {