GeezerVison is showing github.com — Adaptive's buffers setBytes performance is held back by NIO Buffers allocations · Issue #15723 · netty/netty
Adaptive's buffers setBytes performance is held back by NIO Buffers allocations #15723DescriptionThis is still another performance issue for users which cannot use Unsafe buffers.
This is a flamegraph collected while running Techempower plaintext while Nettyread data out of Network using NIO and point to
|
return byteBuf.writeBytes(javaChannel(), allocHandle.attemptedBytesRead()); |
But something similar can still happen on epoll (our native impl) too
|
ByteBuffer buf = byteBuf.internalNioBuffer(writerIndex, byteBuf.writableBytes()); |
if ByteBuf::hasMemoryAddress return false.
I would focus on the NIO case here which is calling
|
public int setBytes(int index, FileChannel in, long position, int length) |
|
throws IOException { |
|
try { |
|
return in.read(internalNioBuffer(index, length).duplicate(), position); |
|
} catch (ClosedChannelException ignored) { |
|
return -1; |
|
} |
|
} |
and
|
public ByteBuffer nioBuffer(int index, int length) { |
|
checkIndex(index, length); |
|
return ((ByteBuffer) buffer.duplicate().position(index).limit(index + length)).slice(); |
|
} |
Which means that:
- adaptive allocates from the
rootParent::nioBuffer a buffer duplicate and a slice of it
- adaptive further create another
duplicate of this
I believe we could reduce the number of allocations there for the no Unsafe version. Activityfranz1981 commented on Sep 29, 2025 ContributorAuthor More actions franz1981 commented on Sep 29, 2025 ContributorAuthor More actions franz1981 commented on Sep 29, 2025 ContributorAuthor More actions I see that what we can do there is:
- if
tmpBuf is created just duplicate it
- if
tmpBuf is NOT created, create a duplicate of the original buffer instead, no need to slice it but just limiting it in the right way
This can be used for each method which need a duplicate really, since it will create it regardless.
If lucky, at least on reader side, tmpBuf won't be created at all and we will save one copy. chrisvest commented on Sep 29, 2025 Alternatively we can use slice(int index, int length) on Java 13 onwards Reacted by Francesco Nigro franz1981 commented on Sep 29, 2025 ContributorAuthor More actions
Alternatively we can use slice(int index, int length) on Java 13 onwards
Since we expect people to move at runtime past 17 (fingers crossed, is what I suggest to our customers :) ). I'm all in for this @chrisvest
Do you have cycles to send a PR on this? chrisvest commented on Sep 29, 2025 Yeah, I can have a PR up in a few. Reacted by Francesco Nigro normanmaurer commented on Mar 25, 2026 franz1981 commented on Mar 25, 2026 ContributorAuthor More actions I think @chrisvest fixed it. Today I am on vacation but d9a8b00 should be the guilty commit which fixed it normanmaurer commented on Mar 25, 2026 Ok let me close it now...
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|