[#2362] AbstractChannel.AbstractUnsafe.write(...) is slow

Motivation:
At the moment we do a Channel.isActive() check in every AbstractChannel.AbstractUnsafe.write(...) call which gives quite some overhead as shown in the profiler when you write fast enough. We can eliminate the check and do something more smart here.

Modifications:
Remove the isActive() check and just check if the ChannelOutboundBuffer was set to null before, which means the Channel was closed. The rest will be handled in flush0() anyway.

Result:
Less overhead when doing many write calls
This commit is contained in:
Norman Maurer 2014-04-04 09:45:03 +02:00
parent 9d695e03db
commit 5cd939f634

View File

@ -638,18 +638,18 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
@Override @Override
public void write(Object msg, ChannelPromise promise) { public void write(Object msg, ChannelPromise promise) {
if (!isActive()) { ChannelOutboundBuffer outboundBuffer = this.outboundBuffer;
// Mark the write request as failure if the channel is inactive. if (outboundBuffer == null) {
if (isOpen()) { // If the outboundBuffer is null we know the channel was closed and so
safeSetFailure(promise, NOT_YET_CONNECTED_EXCEPTION); // need to fail the future right away. If it is not null the handling of the rest
} else { // will be done in flush0()
safeSetFailure(promise, CLOSED_CHANNEL_EXCEPTION); // See https://github.com/netty/netty/issues/2362
} safeSetFailure(promise, CLOSED_CHANNEL_EXCEPTION);
// release message now to prevent resource-leak // release message now to prevent resource-leak
ReferenceCountUtil.release(msg); ReferenceCountUtil.release(msg);
} else { return;
outboundBuffer.addMessage(msg, promise);
} }
outboundBuffer.addMessage(msg, promise);
} }
@Override @Override