Skip to content

Commit

Permalink
[netty#2525] Use VoidChannelPromise in MessageToMessageEncoder when p…
Browse files Browse the repository at this point in the history
…ossible

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
  • Loading branch information
Norman Maurer committed Jun 1, 2014
1 parent cde667f commit 35cc23e
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,21 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
} 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);
}
Expand Down

0 comments on commit 35cc23e

Please sign in to comment.