From 25f96b164498988439c38baf6ff4c57763464824 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Mon, 22 Jul 2013 11:28:02 +0200 Subject: [PATCH] [#1606] Reduce overhead during writes because of ChannelPromise validation --- .../channel/DefaultChannelHandlerContext.java | 24 +++++++++++++++---- .../netty/channel/DefaultChannelPromise.java | 9 +++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/transport/src/main/java/io/netty/channel/DefaultChannelHandlerContext.java b/transport/src/main/java/io/netty/channel/DefaultChannelHandlerContext.java index 38cc6b3e90..340cc33b8a 100644 --- a/transport/src/main/java/io/netty/channel/DefaultChannelHandlerContext.java +++ b/transport/src/main/java/io/netty/channel/DefaultChannelHandlerContext.java @@ -812,17 +812,28 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements return new FailedChannelFuture(channel(), executor(), cause); } - private void validatePromise(ChannelFuture promise, boolean allowUnsafe) { + private void validatePromise(ChannelPromise promise, boolean allowUnsafe) { if (promise == null) { throw new NullPointerException("promise"); } + + if (promise.isDone()) { + throw new IllegalArgumentException("promise already done: " + promise); + } + + // check if the promise is of type DefaultChannelPromise and if so check if its validated already. + DefaultChannelPromise p = null; + if (promise instanceof DefaultChannelPromise) { + p = (DefaultChannelPromise) promise; + if (p.isValidated()) { + return; + } + } + if (promise.channel() != channel()) { throw new IllegalArgumentException(String.format( "promise.channel does not match: %s (expected: %s)", promise.channel(), channel())); } - if (promise.isDone()) { - throw new IllegalArgumentException("promise already done: " + promise); - } if (!allowUnsafe && promise instanceof VoidChannelPromise) { throw new IllegalArgumentException( StringUtil.simpleClassName(VoidChannelPromise.class) + " not allowed for this operation"); @@ -831,6 +842,11 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements throw new IllegalArgumentException( StringUtil.simpleClassName(AbstractChannel.CloseFuture.class) + " not allowed in a pipeline"); } + + if (p != null) { + // mark as validated + p.validated(); + } } private DefaultChannelHandlerContext findContextInbound() { diff --git a/transport/src/main/java/io/netty/channel/DefaultChannelPromise.java b/transport/src/main/java/io/netty/channel/DefaultChannelPromise.java index 3d76347281..117f6af64f 100644 --- a/transport/src/main/java/io/netty/channel/DefaultChannelPromise.java +++ b/transport/src/main/java/io/netty/channel/DefaultChannelPromise.java @@ -29,6 +29,7 @@ public class DefaultChannelPromise extends DefaultPromise implements Chann private final Channel channel; private long checkpoint; + private boolean validated; /** * Creates a new instance. @@ -157,4 +158,12 @@ public class DefaultChannelPromise extends DefaultPromise implements Chann super.checkDeadLock(); } } + + final boolean isValidated() { + return validated; + } + + final void validated() { + validated = true; + } }