[#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
This commit is contained in:
parent
303dfd5138
commit
0ea796ad55
@ -108,9 +108,21 @@ public abstract class MessageToMessageEncoder<I> extends ChannelOutboundHandlerA
|
|||||||
} finally {
|
} finally {
|
||||||
if (out != null) {
|
if (out != null) {
|
||||||
final int sizeMinusOne = out.size() - 1;
|
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 ++) {
|
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);
|
ctx.write(out.get(sizeMinusOne), promise);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user