From e8b1f033fb54604720b8c8af76102568a30d327e 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 | 14 ++++++++++++-- 2 files changed, 25 insertions(+), 5 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 1516724ca9..cd68b333b1 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 @@ -52,7 +52,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; @@ -62,7 +63,13 @@ final class EpollEventLoop extends SingleThreadEventLoop { EpollEventLoop(EventLoopGroup parent, Executor executor, int maxEvents) { super(parent, executor, 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 e6b009db52..e2146a4cc9 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 @@ -62,8 +62,9 @@ public final class EpollEventLoopGroup extends MultithreadEventLoopGroup { * {@code executor} to lie very close together. * @param executor the {@link Executor} to use, or {@code null} if the default should be used. */ + @SuppressWarnings("deprecation") public EpollEventLoopGroup(int nEventLoops, Executor executor) { - this(nEventLoops, executor, 128); + this(nEventLoops, executor, 0); } /** @@ -75,8 +76,9 @@ public final class EpollEventLoopGroup extends MultithreadEventLoopGroup { * @param executorServiceFactory the {@link ExecutorServiceFactory} to use, or {@code null} if the * default should be used. */ + @SuppressWarnings("deprecation") public EpollEventLoopGroup(int nEventLoops, ExecutorServiceFactory executorServiceFactory) { - this(nEventLoops, executorServiceFactory, 128); + this(nEventLoops, executorServiceFactory, 0); } /** @@ -87,7 +89,11 @@ public final class EpollEventLoopGroup extends MultithreadEventLoopGroup { * {@code executor} to lie very close together. * @param executor the {@link Executor} to use, or {@code null} if the default should be used. * @param maxEventsAtOnce the maximum number of epoll events to handle per epollWait(...). + * + * @deprecated Use {@link #EpollEventLoopGroup(int)}, {@link #EpollEventLoopGroup(int)} or + * {@link #EpollEventLoopGroup(int, Executor)} */ + @Deprecated public EpollEventLoopGroup(int nEventLoops, Executor executor, int maxEventsAtOnce) { super(nEventLoops, executor, maxEventsAtOnce); } @@ -101,7 +107,11 @@ public final class EpollEventLoopGroup extends MultithreadEventLoopGroup { * @param executorServiceFactory the {@link ExecutorServiceFactory} to use, or {@code null} if the default * should be used. * @param maxEventsAtOnce the maximum number of epoll events to handle per epollWait(...). + * + * @deprecated Use {@link #EpollEventLoopGroup(int)}, {@link #EpollEventLoopGroup(int)} or + * {@link #EpollEventLoopGroup(int, ExecutorServiceFactory)} */ + @Deprecated public EpollEventLoopGroup(int nEventLoops, ExecutorServiceFactory executorServiceFactory, int maxEventsAtOnce) { super(nEventLoops, executorServiceFactory, maxEventsAtOnce); }