From 165912365a8a375c32f2747ca0836fd604f4ad17 Mon Sep 17 00:00:00 2001 From: Dmitriy Dumanskiy Date: Wed, 16 Jan 2019 12:00:25 +0200 Subject: [PATCH] Clenaup: simplify EpollEventLoop.closeAll() (#8719) Motivation: Avoid unnecessary iteration and `ArrayList` allocation. Modification: ``` for (AbstractEpollChannel channel: channels.values()) { array.add(channel); } ``` replaced with `array.addAll(channels.values())` and ``` Collection array = new ArrayList(channels.size()); array.addAll(channels.values()) ``` replaced with: `AbstractEpollChannel[] localChannels = channels.values().toArray(new AbstractEpollChannel[0]);` Result: Simpler code in `EpollEventLoop.closeAll();` --- .../java/io/netty/channel/epoll/EpollEventLoop.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 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 33adf86279..3420d67bc8 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 @@ -32,8 +32,6 @@ import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; import java.util.Queue; import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; @@ -379,13 +377,9 @@ class EpollEventLoop extends SingleThreadEventLoop { } // Using the intermediate collection to prevent ConcurrentModificationException. // In the `close()` method, the channel is deleted from `channels` map. - Collection array = new ArrayList(channels.size()); + AbstractEpollChannel[] localChannels = channels.values().toArray(new AbstractEpollChannel[0]); - for (AbstractEpollChannel channel: channels.values()) { - array.add(channel); - } - - for (AbstractEpollChannel ch: array) { + for (AbstractEpollChannel ch : localChannels) { ch.unsafe().close(ch.unsafe().voidPromise()); } }