Use LocalCache for deflaters

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-09-25 20:29:47 +02:00
parent 9fe3f28497
commit f07ab21d3e
2 changed files with 6 additions and 2 deletions

View File

@ -40,7 +40,7 @@ import java.util.zip.Deflater;
*/
public final class PacketUtils {
private static final PacketListenerManager PACKET_LISTENER_MANAGER = MinecraftServer.getPacketListenerManager();
private static final ThreadLocal<Deflater> LOCAL_DEFLATER = ThreadLocal.withInitial(Deflater::new);
private static final LocalCache<Deflater> LOCAL_DEFLATER = LocalCache.of(Deflater::new);
/// Local buffers
private static final LocalCache<ByteBuffer> PACKET_BUFFER = LocalCache.ofBuffer(Server.MAX_PACKET_SIZE);

View File

@ -27,8 +27,12 @@ public final class LocalCache<T> {
this.fallback = ThreadLocal.withInitial(supplier);
}
public static <T> LocalCache<T> of(@NotNull Supplier<T> supplier) {
return new LocalCache<>(supplier);
}
public static LocalCache<ByteBuffer> ofBuffer(int size) {
return new LocalCache<>(() -> ByteBuffer.allocateDirect(size));
return of(() -> ByteBuffer.allocateDirect(size));
}
public T get() {