Skip to content

Commit e2859f4

Browse files
authored
Short-circuit ByteBuf::release (#13782)
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
1 parent d9ca50d commit e2859f4

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

transport/src/main/java/io/netty/channel/ChannelOutboundBuffer.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.netty.channel;
1717

18+
import io.netty.buffer.AbstractReferenceCountedByteBuf;
1819
import io.netty.buffer.ByteBuf;
1920
import io.netty.buffer.ByteBufHolder;
2021
import io.netty.buffer.Unpooled;
@@ -275,9 +276,20 @@ public boolean remove() {
275276

276277
removeEntry(e);
277278

279+
// only release message, notify and decrement if it was not canceled before.
278280
if (!e.cancelled) {
279-
// only release message, notify and decrement if it was not canceled before.
280-
ReferenceCountUtil.safeRelease(msg);
281+
// this save both checking against the ReferenceCounted interface
282+
// and makes better use of virtual calls vs interface ones
283+
if (msg instanceof AbstractReferenceCountedByteBuf) {
284+
try {
285+
// release now as it is flushed.
286+
((AbstractReferenceCountedByteBuf) msg).release();
287+
} catch (Throwable t) {
288+
logger.warn("Failed to release a ByteBuf: {}", msg, t);
289+
}
290+
} else {
291+
ReferenceCountUtil.safeRelease(msg);
292+
}
281293
safeSuccess(promise);
282294
decrementPendingOutboundBytes(size, false, true);
283295
}

0 commit comments

Comments
 (0)