Skip to content

Commit a30d4de

Browse files
committed
Cap sub-region's size of returned nio buffer
1 parent 9dbe53e commit a30d4de

File tree

1 file changed

+5
-20
lines changed

1 file changed

+5
-20
lines changed

common/network-common/src/main/java/org/apache/spark/network/protocol/MessageWithHeader.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -137,30 +137,15 @@ protected void deallocate() {
137137
}
138138

139139
private int copyByteBuf(ByteBuf buf, WritableByteChannel target) throws IOException {
140-
ByteBuffer buffer = buf.nioBuffer();
141-
int written = (buffer.remaining() <= NIO_BUFFER_LIMIT) ?
142-
target.write(buffer) : writeNioBuffer(target, buffer);
140+
// SPARK-24578: cap the sub-region's size of returned nio buffer to improve the performance
141+
// for the case that the passed-in buffer has too many components.
142+
int length = Math.min(buf.readableBytes(), NIO_BUFFER_LIMIT);
143+
ByteBuffer buffer = buf.nioBuffer(buf.readerIndex(), length);
144+
int written = target.write(buffer);
143145
buf.skipBytes(written);
144146
return written;
145147
}
146148

147-
private int writeNioBuffer(
148-
WritableByteChannel writeCh,
149-
ByteBuffer buf) throws IOException {
150-
int originalLimit = buf.limit();
151-
int ret = 0;
152-
153-
try {
154-
int ioSize = Math.min(buf.remaining(), NIO_BUFFER_LIMIT);
155-
buf.limit(buf.position() + ioSize);
156-
ret = writeCh.write(buf);
157-
} finally {
158-
buf.limit(originalLimit);
159-
}
160-
161-
return ret;
162-
}
163-
164149
@Override
165150
public MessageWithHeader touch(Object o) {
166151
super.touch(o);

0 commit comments

Comments
 (0)