From 94e907fa39825faddfc985449de376e04000ea40 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Tue, 27 Oct 2015 20:36:49 +0100 Subject: [PATCH] Only call ReferenceCountUtil.touch(...) if ResourceLeakDetection was enabled when Channel was created. Motivation: We should only call ReferenceCountUtil.touch(...) if needed as otherwise we pay the overhead of instanceof and cast everytime. Modifications: Add boolean flag which indicates if touch(...) should be called. Result: Less overhead when leak detection is not enabled. --- .../netty/channel/AbstractChannelHandlerContext.java | 10 +++------- .../java/io/netty/channel/DefaultChannelPipeline.java | 6 ++++++ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/transport/src/main/java/io/netty/channel/AbstractChannelHandlerContext.java b/transport/src/main/java/io/netty/channel/AbstractChannelHandlerContext.java index f371c66502..e3f2ec965e 100644 --- a/transport/src/main/java/io/netty/channel/AbstractChannelHandlerContext.java +++ b/transport/src/main/java/io/netty/channel/AbstractChannelHandlerContext.java @@ -18,7 +18,6 @@ package io.netty.channel; import io.netty.buffer.ByteBufAllocator; import io.netty.util.Attribute; import io.netty.util.AttributeKey; -import io.netty.util.ReferenceCountUtil; import io.netty.util.ResourceLeakHint; import io.netty.util.concurrent.EventExecutor; import io.netty.util.internal.StringUtil; @@ -153,8 +152,7 @@ abstract class AbstractChannelHandlerContext implements ChannelHandlerContext, R @Override public ChannelHandlerContext fireChannelRead(Object msg) { AbstractChannelHandlerContext next = findContextInbound(); - ReferenceCountUtil.touch(msg, next); - next.invoker().invokeChannelRead(next, msg); + next.invoker().invokeChannelRead(next, pipeline.touch(msg, next)); return this; } @@ -261,8 +259,7 @@ abstract class AbstractChannelHandlerContext implements ChannelHandlerContext, R @Override public ChannelFuture write(Object msg, ChannelPromise promise) { AbstractChannelHandlerContext next = findContextOutbound(); - ReferenceCountUtil.touch(msg, next); - next.invoker().invokeWrite(next, msg, promise); + next.invoker().invokeWrite(next, pipeline.touch(msg, next), promise); return promise; } @@ -276,9 +273,8 @@ abstract class AbstractChannelHandlerContext implements ChannelHandlerContext, R @Override public ChannelFuture writeAndFlush(Object msg, ChannelPromise promise) { AbstractChannelHandlerContext next = findContextOutbound(); - ReferenceCountUtil.touch(msg, next); ChannelHandlerInvoker invoker = next.invoker(); - invoker.invokeWrite(next, msg, promise); + invoker.invokeWrite(next, pipeline.touch(msg, next) , promise); invoker.invokeFlush(next); return promise; } diff --git a/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java b/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java index 6ae2cae83e..9ef99f943e 100644 --- a/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java +++ b/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java @@ -17,6 +17,7 @@ package io.netty.channel; import io.netty.channel.Channel.Unsafe; import io.netty.util.ReferenceCountUtil; +import io.netty.util.ResourceLeakDetector; import io.netty.util.concurrent.EventExecutor; import io.netty.util.concurrent.EventExecutorGroup; import io.netty.util.internal.OneTimeTask; @@ -63,6 +64,7 @@ final class DefaultChannelPipeline implements ChannelPipeline { private final Map name2ctx = new HashMap(4); + private final boolean touch = ResourceLeakDetector.isEnabled(); /** * @see #findInvoker(EventExecutorGroup) @@ -82,6 +84,10 @@ final class DefaultChannelPipeline implements ChannelPipeline { tail.prev = head; } + Object touch(Object msg, AbstractChannelHandlerContext next) { + return touch ? ReferenceCountUtil.touch(msg, next) : msg; + } + @Override public Channel channel() { return channel;