Reputation: 185
I'm working on a project which needs to broadcast small messages (<100byte) to thousands of client channels. What is the most efficient way to minimize data copy for every channel by both Netty and JDK?
If use N instances of DuplicatedByteBuf wrapping a shared DirectByteBuf(pooled or unpooled), could the Direct ByteBuffer object in netty's DirectByteBuf be used by JDK instead of copying a new ByteBuffer for every SocketChannel?
Upvotes: 1
Views: 418
Reputation: 12817
You have few options :
Option 1. Messages share same data (your 100 bytes) but you have overhead for every retainedDuplicate() call as it creates new buffer instance (even it shares 100 bytes) :
ByteBuf msg = createMessage(); //pooled, unpooled
for (Channel target : targets) {
target.writeAndFlush(msg.retainedDuplicate());
}
this is option is not very good for your case as your messages are small.
Options 2. You always use same message but you have to handle message read/write indexes by your own.
ByteBuf msg = createMessage(); //pooled, unpooled
if (targets.size() > 1) {
msg.retain(targetsNum - 1).markReaderIndex();
}
for (Channel target : targets) {
target.writeAndFlush(msg);
if (msg.refCnt() > 0) {
msg.resetReaderIndex();
}
}
This option is better for your case. As you create message only once and after message is passed to 1 receiver you will reuse it again for next receiver.
Upvotes: 1
Reputation: 71
The buffer will not be copied when it's written to outbound channels.
As far as I know the 'write' will invoke HeapByteBuffer.duplicate, which is a shallow copy.
Upvotes: 0