Improve performance when cached packets are disabled

This commit is contained in:
themode 2021-12-22 07:27:35 +01:00 committed by TheMode
parent d7ab273b60
commit bb1b1e53d7
2 changed files with 17 additions and 13 deletions

View File

@ -3,8 +3,10 @@ package net.minestom.server.network.packet.server;
import net.minestom.server.utils.PacketUtils;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.ref.SoftReference;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.function.Supplier;
@ -51,7 +53,7 @@ public final class CachedPacket implements SendablePacket {
* Retrieves the packet object without allocating a buffer.
* <p>
* This method can be useful in case the payload is not important (e.g. for packet listening),
* but {@link #retrieve()} should be privileged otherwise.
* but {@link #toBuffer()} and {@link #toFramedPacket()} should be privileged otherwise.
*/
public @NotNull ServerPacket packet() {
FramedPacket cache;
@ -60,17 +62,19 @@ public final class CachedPacket implements SendablePacket {
return packetSupplier.get();
}
/**
* Retrieves the packet content. May be recomputed if the packet is invalidated or
* if there is memory demand (handled by a soft reference).
* <p>
* {@link FramedPacket#body()} will contain a buffer allocated by this method.
*/
public @NotNull FramedPacket retrieve() {
if (!PacketUtils.CACHED_PACKET) {
// TODO: Using a local buffer may be possible
return PacketUtils.allocateTrimmedPacket(packet());
}
public @NotNull ByteBuffer toBuffer() {
FramedPacket cache = updatedCache();
return cache != null ? cache.body() : PacketUtils.createFramedPacket(packet());
}
public @NotNull FramedPacket toFramedPacket() {
FramedPacket cache = updatedCache();
return cache != null ? cache : PacketUtils.allocateTrimmedPacket(packet());
}
private @Nullable FramedPacket updatedCache() {
if (!PacketUtils.CACHED_PACKET)
return null;
SoftReference<FramedPacket> ref;
FramedPacket cache;
if (updated == 0 ||

View File

@ -356,7 +356,7 @@ public class PlayerSocketConnection extends PlayerConnection {
} else if (packet instanceof FramedPacket framedPacket) {
writeFramedPacketSync(framedPacket);
} else if (packet instanceof CachedPacket cachedPacket) {
writeFramedPacketSync(cachedPacket.retrieve());
writeBufferSync(cachedPacket.toBuffer());
} else {
throw new RuntimeException("Unknown packet type: " + packet.getClass().getName());
}