From db5285950b0186ee96945eed81af8c72d4cb20af Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Fri, 4 Apr 2014 09:45:03 +0200 Subject: [PATCH] [#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 --- .../java/io/netty/channel/AbstractChannel.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/transport/src/main/java/io/netty/channel/AbstractChannel.java b/transport/src/main/java/io/netty/channel/AbstractChannel.java index 976014891d..b8b2db8413 100644 --- a/transport/src/main/java/io/netty/channel/AbstractChannel.java +++ b/transport/src/main/java/io/netty/channel/AbstractChannel.java @@ -602,18 +602,18 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha @Override public void write(Object msg, ChannelPromise promise) { - if (!isActive()) { - // Mark the write request as failure if the channel is inactive. - if (isOpen()) { - safeSetFailure(promise, NOT_YET_CONNECTED_EXCEPTION); - } else { - safeSetFailure(promise, CLOSED_CHANNEL_EXCEPTION); - } + ChannelOutboundBuffer outboundBuffer = this.outboundBuffer; + if (outboundBuffer == null) { + // If the outboundBuffer is null we know the channel was closed and so + // need to fail the future right away. If it is not null the handling of the rest + // will be done in flush0() + // See https://github.com/netty/netty/issues/2362 + safeSetFailure(promise, CLOSED_CHANNEL_EXCEPTION); // release message now to prevent resource-leak ReferenceCountUtil.release(msg); - } else { - outboundBuffer.addMessage(msg, promise); + return; } + outboundBuffer.addMessage(msg, promise); } @Override