mirror of
https://github.com/Minestom/Minestom.git
synced 2024-09-29 06:57:28 +02:00
Improve performance when cached packets are disabled
This commit is contained in:
parent
d7ab273b60
commit
bb1b1e53d7
@ -3,8 +3,10 @@ package net.minestom.server.network.packet.server;
|
|||||||
import net.minestom.server.utils.PacketUtils;
|
import net.minestom.server.utils.PacketUtils;
|
||||||
import org.jetbrains.annotations.ApiStatus;
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.lang.ref.SoftReference;
|
import java.lang.ref.SoftReference;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
|
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
@ -51,7 +53,7 @@ public final class CachedPacket implements SendablePacket {
|
|||||||
* Retrieves the packet object without allocating a buffer.
|
* Retrieves the packet object without allocating a buffer.
|
||||||
* <p>
|
* <p>
|
||||||
* This method can be useful in case the payload is not important (e.g. for packet listening),
|
* 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() {
|
public @NotNull ServerPacket packet() {
|
||||||
FramedPacket cache;
|
FramedPacket cache;
|
||||||
@ -60,17 +62,19 @@ public final class CachedPacket implements SendablePacket {
|
|||||||
return packetSupplier.get();
|
return packetSupplier.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public @NotNull ByteBuffer toBuffer() {
|
||||||
* Retrieves the packet content. May be recomputed if the packet is invalidated or
|
FramedPacket cache = updatedCache();
|
||||||
* if there is memory demand (handled by a soft reference).
|
return cache != null ? cache.body() : PacketUtils.createFramedPacket(packet());
|
||||||
* <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 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;
|
SoftReference<FramedPacket> ref;
|
||||||
FramedPacket cache;
|
FramedPacket cache;
|
||||||
if (updated == 0 ||
|
if (updated == 0 ||
|
||||||
|
@ -356,7 +356,7 @@ public class PlayerSocketConnection extends PlayerConnection {
|
|||||||
} else if (packet instanceof FramedPacket framedPacket) {
|
} else if (packet instanceof FramedPacket framedPacket) {
|
||||||
writeFramedPacketSync(framedPacket);
|
writeFramedPacketSync(framedPacket);
|
||||||
} else if (packet instanceof CachedPacket cachedPacket) {
|
} else if (packet instanceof CachedPacket cachedPacket) {
|
||||||
writeFramedPacketSync(cachedPacket.retrieve());
|
writeBufferSync(cachedPacket.toBuffer());
|
||||||
} else {
|
} else {
|
||||||
throw new RuntimeException("Unknown packet type: " + packet.getClass().getName());
|
throw new RuntimeException("Unknown packet type: " + packet.getClass().getName());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user