From b0ddfb9b65344109c55e75ced8a33af4aaea7050 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Sun, 1 Jun 2014 19:24:30 +0200 Subject: [PATCH] [#2525] Use VoidChannelPromise in MessageToMessageEncoder when possible Motivation: At the moment MessageToMessageEncoder uses ctx.write(msg) when have more then one message was produced. This may produce more GC pressure then necessary as when the original ChannelPromise is a VoidChannelPromise we can safely also use one when write messages. Modifications: Use VoidChannelPromise when the original ChannelPromise was of this type Result: Less object creation and GC pressure --- .../handler/codec/MessageToMessageEncoder.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/codec/src/main/java/io/netty/handler/codec/MessageToMessageEncoder.java b/codec/src/main/java/io/netty/handler/codec/MessageToMessageEncoder.java index d0e98efa44..1a2cc034c2 100644 --- a/codec/src/main/java/io/netty/handler/codec/MessageToMessageEncoder.java +++ b/codec/src/main/java/io/netty/handler/codec/MessageToMessageEncoder.java @@ -108,9 +108,21 @@ public abstract class MessageToMessageEncoder extends ChannelOutboundHandlerA } finally { if (out != null) { final int sizeMinusOne = out.size() - 1; - if (sizeMinusOne >= 0) { + if (sizeMinusOne == 0) { + ctx.write(out.get(0), promise); + } else if (sizeMinusOne > 0) { + // Check if we can use a voidPromise for our extra writes to reduce GC-Pressure + // See https://github.com/netty/netty/issues/2525 + ChannelPromise voidPromise = ctx.voidPromise(); + boolean isVoidPromise = promise == voidPromise; for (int i = 0; i < sizeMinusOne; i ++) { - ctx.write(out.get(i)); + ChannelPromise p; + if (isVoidPromise) { + p = voidPromise; + } else { + p = ctx.newPromise(); + } + ctx.write(out.get(i), p); } ctx.write(out.get(sizeMinusOne), promise); }