From 695fbc91405412ce19eb5728acb2e8962dc8390c Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Mon, 21 Jul 2014 06:43:49 +0200 Subject: [PATCH] [#2684] EpollSocketChannel gathering writes should take fast-path if possible Motivation: In EpollSocketchannel.writeBytesMultiple(...) we loop over all buffers to see if we need to adjust the readerIndex for incomplete writes. We can skip this if we know that everything was written (a.k.a complete write). Modification: Use fast-path if all bytes are written and so no need to loop over buffers Result: Fast write path for the average use. --- .../channel/epoll/EpollSocketChannel.java | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollSocketChannel.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollSocketChannel.java index 33b5e08f81..6dbe9bb015 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollSocketChannel.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollSocketChannel.java @@ -162,7 +162,13 @@ public final class EpollSocketChannel extends AbstractEpollChannel implements So expectedWrittenBytes -= localWrittenBytes; writtenBytes += localWrittenBytes; - while (offset < end && localWrittenBytes > 0) { + if (expectedWrittenBytes == 0) { + // Written everything, just break out here (fast-path) + done = true; + break loop; + } + + do { AddressEntry address = addresses[offset]; int readerIndex = address.readerIndex; int bytes = address.writerIndex - readerIndex; @@ -175,12 +181,7 @@ public final class EpollSocketChannel extends AbstractEpollChannel implements So addressCnt--; localWrittenBytes -= bytes; } - } - - if (expectedWrittenBytes == 0) { - done = true; - break; - } + } while (offset < end && localWrittenBytes > 0); } } @@ -207,8 +208,12 @@ public final class EpollSocketChannel extends AbstractEpollChannel implements So } expectedWrittenBytes -= localWrittenBytes; writtenBytes += localWrittenBytes; - - while (offset < end && localWrittenBytes > 0) { + if (expectedWrittenBytes == 0) { + // Written everything, just break out here (fast-path) + done = true; + break loop; + } + do { ByteBuffer buffer = nioBuffers[offset]; int pos = buffer.position(); int bytes = buffer.limit() - pos; @@ -221,12 +226,7 @@ public final class EpollSocketChannel extends AbstractEpollChannel implements So nioBufferCnt--; localWrittenBytes -= bytes; } - } - - if (expectedWrittenBytes == 0) { - done = true; - break; - } + } while (offset < end && localWrittenBytes > 0); } } updateOutboundBuffer(in, writtenBytes, msgCount, done);