From f3eb3f6e34ee08c67e60aba018360d2a7523b2f9 Mon Sep 17 00:00:00 2001 From: TheMode Date: Tue, 30 Mar 2021 22:38:45 +0200 Subject: [PATCH] Remove packet caching to CacheablePacket#getCache Signed-off-by: TheMode --- .../network/player/NettyPlayerConnection.java | 34 +++---------------- .../server/utils/cache/CacheablePacket.java | 31 +++++++++++++++++ 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java index 306a4dbfe..099a2b04c 100644 --- a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java +++ b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java @@ -20,8 +20,6 @@ import net.minestom.server.network.packet.server.login.SetCompressionPacket; import net.minestom.server.utils.BufUtils; import net.minestom.server.utils.PacketUtils; import net.minestom.server.utils.cache.CacheablePacket; -import net.minestom.server.utils.cache.TemporaryCache; -import net.minestom.server.utils.cache.TimedBuffer; import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -136,35 +134,13 @@ public class NettyPlayerConnection extends PlayerConnection { if (getPlayer() != null) { // Flush happen during #update() if (serverPacket instanceof CacheablePacket && MinecraftServer.hasPacketCaching()) { - final CacheablePacket cacheablePacket = (CacheablePacket) serverPacket; - final UUID identifier = cacheablePacket.getIdentifier(); - - if (identifier == null) { - // This packet explicitly asks to do not retrieve the cache - write(serverPacket, skipTranslating); + // Check if the packet is cached + final FramedPacket cachedPacket = CacheablePacket.getCache(serverPacket); + if (cachedPacket != null) { + write(cachedPacket); } else { - final long timestamp = cacheablePacket.getTimestamp(); - // Try to retrieve the cached buffer - TemporaryCache temporaryCache = cacheablePacket.getCache(); - TimedBuffer timedBuffer = temporaryCache.retrieve(identifier); - - // Update the buffer if non-existent or outdated - final boolean shouldUpdate = timedBuffer == null || - timestamp > timedBuffer.getTimestamp(); - - if (shouldUpdate) { - final ByteBuf buffer = PacketUtils.createFramedPacket(serverPacket, true); - TimedBuffer oldBuffer = timedBuffer; - timedBuffer = new TimedBuffer(buffer, timestamp); - temporaryCache.cache(identifier, timedBuffer); - if (oldBuffer != null) { - oldBuffer.getBuffer().release(); - } - } - - write(new FramedPacket(timedBuffer.getBuffer())); + write(serverPacket, skipTranslating); } - } else { write(serverPacket, skipTranslating); } diff --git a/src/main/java/net/minestom/server/utils/cache/CacheablePacket.java b/src/main/java/net/minestom/server/utils/cache/CacheablePacket.java index dcb83fe31..b8507164e 100644 --- a/src/main/java/net/minestom/server/utils/cache/CacheablePacket.java +++ b/src/main/java/net/minestom/server/utils/cache/CacheablePacket.java @@ -1,6 +1,9 @@ package net.minestom.server.utils.cache; +import io.netty.buffer.ByteBuf; +import net.minestom.server.network.netty.packet.FramedPacket; import net.minestom.server.network.packet.server.ServerPacket; +import net.minestom.server.utils.PacketUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -41,4 +44,32 @@ public interface CacheablePacket { */ long getTimestamp(); + @Nullable + static FramedPacket getCache(@NotNull ServerPacket serverPacket) { + final CacheablePacket cacheablePacket = (CacheablePacket) serverPacket; + final UUID identifier = cacheablePacket.getIdentifier(); + if (identifier == null) { + // This packet explicitly asks to do not retrieve the cache + return null; + } else { + final long timestamp = cacheablePacket.getTimestamp(); + // Try to retrieve the cached buffer + TemporaryCache temporaryCache = cacheablePacket.getCache(); + TimedBuffer timedBuffer = temporaryCache.retrieve(identifier); + + // Update the buffer if non-existent or outdated + final boolean shouldUpdate = timedBuffer == null || + timestamp > timedBuffer.getTimestamp(); + + if (shouldUpdate) { + // Buffer freed by guava cache #removalListener + final ByteBuf buffer = PacketUtils.createFramedPacket(serverPacket, true); + timedBuffer = new TimedBuffer(buffer, timestamp); + temporaryCache.cache(identifier, timedBuffer); + } + + return new FramedPacket(timedBuffer.getBuffer()); + } + } + }