Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #7757] Use CompositeByteBuf to prevent memory copy. #7694

Merged
merged 6 commits into from
Jan 17, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
package org.apache.rocketmq.remoting.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.FileRegion;
import io.netty.handler.codec.MessageToByteEncoder;
Expand Down Expand Up @@ -51,9 +54,11 @@ protected void encode(ChannelHandlerContext ctx, FileRegion msg, final ByteBuf o
WritableByteChannel writableByteChannel = new WritableByteChannel() {
@Override
public int write(ByteBuffer src) {
int prev = out.writerIndex();
out.writeBytes(src);
return out.writerIndex() - prev;
// To prevent mem_copy.
CompositeByteBuf b = (CompositeByteBuf) out;
// Have to increase writerIndex manually.
b.addComponent(true, Unpooled.wrappedBuffer(src));
return out.capacity();
}

@Override
Expand All @@ -76,4 +81,10 @@ public void close() throws IOException {
msg.transferTo(writableByteChannel, transferred);
}
}
}

@Override
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, FileRegion msg, boolean preferDirect) throws Exception {
ByteBufAllocator allocator = ctx.alloc();
return preferDirect ? allocator.compositeDirectBuffer() : allocator.compositeHeapBuffer();
}
}
Loading