Skip to content

Commit

Permalink
[#2577] ChannelOutboundBuffer.addFlush() unnecessary loop through all…
Browse files Browse the repository at this point in the history
… entries on multiple calls

Motivation:

If ChannelOutboundBuffer.addFlush() is called multiple times and flushed != unflushed it will still loop through all entries that are not flushed yet even if it is not needed anymore as these were marked uncancellable before.

Modifications:

Check if new messages were added since addFlush() was called and only if this was the case loop through all entries and try to mark the uncancellable.

Result:

Less overhead when ChannelOuboundBuffer.addFlush() is called multiple times without new messages been added.
  • Loading branch information
Norman Maurer committed Jun 17, 2014
1 parent 4d60ea2 commit 375da78
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions transport/src/main/java/io/netty/channel/ChannelOutboundBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,24 @@ private void addCapacity() {
* Mark all messages in this {@link ChannelOutboundBuffer} as flushed.
*/
public final void addFlush() {
unflushed = tail;

final int mask = buffer.length - 1;
int i = flushed;
while (i != unflushed && buffer[i].msg != null) {
Entry entry = buffer[i];
if (!entry.promise.setUncancellable()) {
// Was cancelled so make sure we free up memory and notify about the freed bytes
int pending = entry.cancel();
decrementPendingOutboundBytes(pending);
// There is no need to process all entries if there was already a flush before and no new messages
// where added in the meantime.
//
// See https://github.com/netty/netty/issues/2577
if (unflushed != tail) {
unflushed = tail;

final int mask = buffer.length - 1;
int i = flushed;
while (i != unflushed && buffer[i].msg != null) {
Entry entry = buffer[i];
if (!entry.promise.setUncancellable()) {
// Was cancelled so make sure we free up memory and notify about the freed bytes
int pending = entry.cancel();
decrementPendingOutboundBytes(pending);
}
i = i + 1 & mask;
}
i = i + 1 & mask;
}
}

Expand Down

0 comments on commit 375da78

Please sign in to comment.