Skip to content

Commit

Permalink
Short-circuit ByteBuf::release (#13782)
Browse files Browse the repository at this point in the history
Motivation:

ReferenceCountUtil::safeRelease can both hit interface virtual calls and
requires checking for an interface type (ReferenceCounted)

Modifications:

Perform a class check to save both.

Result:

Faster buffers release
  • Loading branch information
franz1981 committed Jan 19, 2024
1 parent d9ca50d commit e2859f4
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.netty.channel;

import io.netty.buffer.AbstractReferenceCountedByteBuf;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty.buffer.Unpooled;
Expand Down Expand Up @@ -275,9 +276,20 @@ public boolean remove() {

removeEntry(e);

// only release message, notify and decrement if it was not canceled before.
if (!e.cancelled) {
// only release message, notify and decrement if it was not canceled before.
ReferenceCountUtil.safeRelease(msg);
// this save both checking against the ReferenceCounted interface
// and makes better use of virtual calls vs interface ones
if (msg instanceof AbstractReferenceCountedByteBuf) {
try {
// release now as it is flushed.
((AbstractReferenceCountedByteBuf) msg).release();
} catch (Throwable t) {
logger.warn("Failed to release a ByteBuf: {}", msg, t);
}
} else {
ReferenceCountUtil.safeRelease(msg);
}
safeSuccess(promise);
decrementPendingOutboundBytes(size, false, true);
}
Expand Down

0 comments on commit e2859f4

Please sign in to comment.