From dab9c9a3829bba2ff04b3b3f7c7cf1edb02c53a7 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Tue, 2 Jul 2019 02:19:52 -0400 Subject: [PATCH] Use Netty thread factory to take advantage of FastThreadLocal This should reduce CPU usage slightly. --- .../natives/encryption/JavaVelocityCipher.java | 9 +++++++-- .../com/velocitypowered/proxy/network/TransportType.java | 6 ++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/native/src/main/java/com/velocitypowered/natives/encryption/JavaVelocityCipher.java b/native/src/main/java/com/velocitypowered/natives/encryption/JavaVelocityCipher.java index c7a14815e..c8c15686a 100644 --- a/native/src/main/java/com/velocitypowered/natives/encryption/JavaVelocityCipher.java +++ b/native/src/main/java/com/velocitypowered/natives/encryption/JavaVelocityCipher.java @@ -4,6 +4,7 @@ import com.google.common.base.Preconditions; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; +import io.netty.util.concurrent.FastThreadLocal; import java.security.GeneralSecurityException; import javax.crypto.Cipher; import javax.crypto.SecretKey; @@ -24,8 +25,12 @@ public class JavaVelocityCipher implements VelocityCipher { } }; private static final int INITIAL_BUFFER_SIZE = 1024 * 8; - private static final ThreadLocal inBufLocal = ThreadLocal.withInitial( - () -> new byte[INITIAL_BUFFER_SIZE]); + private static final FastThreadLocal inBufLocal = new FastThreadLocal() { + @Override + protected byte[] initialValue() { + return new byte[INITIAL_BUFFER_SIZE]; + } + }; private final Cipher cipher; private boolean disposed = false; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/TransportType.java b/proxy/src/main/java/com/velocitypowered/proxy/network/TransportType.java index beb42d92f..88bfc8f31 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/TransportType.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/TransportType.java @@ -19,6 +19,7 @@ import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioDatagramChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.util.concurrent.DefaultThreadFactory; import java.util.concurrent.ThreadFactory; import java.util.function.BiFunction; @@ -60,10 +61,7 @@ enum TransportType { } private static ThreadFactory createThreadFactory(final String name, final Type type) { - return new ThreadFactoryBuilder() - .setNameFormat("Netty " + name + ' ' + type.toString() + " #%d") - .setDaemon(true) - .build(); + return new DefaultThreadFactory("Netty " + name + ' ' + type.toString(), false); } public static TransportType bestType() {