From bed3502772099dc1e70fefc839e69ee6d8841ad6 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 29 Jan 2015 20:53:52 +0100 Subject: [PATCH] [#3378] Automatically increase number of possible handled events Motivation: At the moment the max number of events that can be handled per epoll wakup was set during construction. Modifications: - Automatically increase the max number of events to handle Result: Better performance when a lot of events need to be handled without adjusting the code. --- .../io/netty/channel/epoll/EpollEventLoop.java | 16 +++++++++++++--- .../netty/channel/epoll/EpollEventLoopGroup.java | 7 ++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollEventLoop.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollEventLoop.java index be32c4f163..f208336071 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollEventLoop.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollEventLoop.java @@ -51,7 +51,8 @@ final class EpollEventLoop extends SingleThreadEventLoop { private final int epollFd; private final int eventFd; private final IntObjectMap ids = new IntObjectHashMap(); - private final long[] events; + private final boolean allowGrowing; + private long[] events; private int id; @@ -61,7 +62,13 @@ final class EpollEventLoop extends SingleThreadEventLoop { EpollEventLoop(EventLoopGroup parent, ThreadFactory threadFactory, int maxEvents) { super(parent, threadFactory, false); - events = new long[maxEvents]; + if (maxEvents == 0) { + allowGrowing = true; + events = new long[128]; + } else { + allowGrowing = false; + events = new long[maxEvents]; + } boolean success = false; int epollFd = -1; int eventFd = -1; @@ -268,7 +275,10 @@ final class EpollEventLoop extends SingleThreadEventLoop { final long ioTime = System.nanoTime() - ioStartTime; runAllTasks(ioTime * (100 - ioRatio) / ioRatio); } - + if (allowGrowing && ready == events.length) { + // double the size of the array as we needed the whole space for the events + events = new long[events.length << 1]; + } if (isShuttingDown()) { closeAll(); if (confirmShutdown()) { diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollEventLoopGroup.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollEventLoopGroup.java index f702417c5c..39313ea02d 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollEventLoopGroup.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollEventLoopGroup.java @@ -44,14 +44,19 @@ public final class EpollEventLoopGroup extends MultithreadEventLoopGroup { /** * Create a new instance using the specified number of threads and the given {@link ThreadFactory}. */ + @SuppressWarnings("deprecation") public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory) { - this(nThreads, threadFactory, 128); + this(nThreads, threadFactory, 0); } /** * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the given * maximal amount of epoll events to handle per epollWait(...). + * + * @deprecated Use {@link #EpollEventLoopGroup(int)}, {@link #EpollEventLoopGroup(int)} or + * {@link #EpollEventLoopGroup(int, ThreadFactory)} */ + @Deprecated public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory, int maxEventsAtOnce) { super(nThreads, threadFactory, maxEventsAtOnce); }