Suppress system calls when a user attempts to write an empty buffer

This commit is contained in:
Trustin Lee 2010-06-16 03:28:30 +00:00
parent f753dc2b83
commit 752e60a723
2 changed files with 24 additions and 20 deletions

View File

@ -101,24 +101,26 @@ class OioDatagramWorker implements Runnable {
try {
ChannelBuffer buf = (ChannelBuffer) message;
int length = buf.readableBytes();
ByteBuffer nioBuf = buf.toByteBuffer();
DatagramPacket packet;
if (nioBuf.hasArray()) {
// Avoid copy if the buffer is backed by an array.
packet = new DatagramPacket(
nioBuf.array(), nioBuf.arrayOffset(), length);
} else {
// Otherwise it will be expensive.
byte[] arrayBuf = new byte[length];
buf.getBytes(0, arrayBuf);
packet = new DatagramPacket(arrayBuf, length);
}
if (length > 0) {
ByteBuffer nioBuf = buf.toByteBuffer();
DatagramPacket packet;
if (nioBuf.hasArray()) {
// Avoid copy if the buffer is backed by an array.
packet = new DatagramPacket(
nioBuf.array(), nioBuf.arrayOffset(), length);
} else {
// Otherwise it will be expensive.
byte[] arrayBuf = new byte[length];
buf.getBytes(0, arrayBuf);
packet = new DatagramPacket(arrayBuf, length);
}
if (remoteAddress != null) {
packet.setSocketAddress(remoteAddress);
if (remoteAddress != null) {
packet.setSocketAddress(remoteAddress);
}
channel.socket.send(packet);
fireWriteComplete(channel, length);
}
channel.socket.send(packet);
fireWriteComplete(channel, length);
future.setSuccess();
} catch (Throwable t) {
future.setFailure(t);

View File

@ -114,11 +114,13 @@ class OioWorker implements Runnable {
try {
ChannelBuffer a = (ChannelBuffer) message;
int bytes = a.readableBytes();
synchronized (out) {
a.getBytes(a.readerIndex(), out, bytes);
int length = a.readableBytes();
if (length > 0) {
synchronized (out) {
a.getBytes(a.readerIndex(), out, length);
}
fireWriteComplete(channel, length);
}
fireWriteComplete(channel, bytes);
future.setSuccess();
} catch (Throwable t) {
// Convert 'SocketException: Socket closed' to