From 540405e796711ab60f31fe7a655ebf55600951a7 Mon Sep 17 00:00:00 2001 From: themode Date: Mon, 16 Nov 2020 11:41:03 +0100 Subject: [PATCH] Optimized PacketCompressor --- .../net/minestom/server/entity/Player.java | 21 +++++----- .../server/inventory/PlayerInventory.java | 2 +- .../listener/PlayerPositionListener.java | 3 +- .../network/netty/codec/PacketCompressor.java | 41 ++++++++----------- 4 files changed, 32 insertions(+), 35 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 7c7672898..8c500c441 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -420,11 +420,6 @@ public class Player extends LivingEntity implements CommandSender { entityPositionAndRotationPacket.pitch = position.getPitch(); entityPositionAndRotationPacket.onGround = onGround; - lastX = position.getX(); - lastY = position.getY(); - lastZ = position.getZ(); - lastYaw = position.getYaw(); - lastPitch = position.getPitch(); updatePacket = entityPositionAndRotationPacket; } else if (positionChanged) { EntityPositionPacket entityPositionPacket = new EntityPositionPacket(); @@ -433,9 +428,7 @@ public class Player extends LivingEntity implements CommandSender { entityPositionPacket.deltaY = (short) ((position.getY() * 32 - lastY * 32) * 128); entityPositionPacket.deltaZ = (short) ((position.getZ() * 32 - lastZ * 32) * 128); entityPositionPacket.onGround = onGround; - lastX = position.getX(); - lastY = position.getY(); - lastZ = position.getZ(); + updatePacket = entityPositionPacket; } else { // View changed @@ -445,8 +438,6 @@ public class Player extends LivingEntity implements CommandSender { entityRotationPacket.pitch = position.getPitch(); entityRotationPacket.onGround = onGround; - lastYaw = position.getYaw(); - lastPitch = position.getPitch(); updatePacket = entityRotationPacket; } @@ -473,6 +464,16 @@ public class Player extends LivingEntity implements CommandSender { } } + if (positionChanged) { + lastX = position.getX(); + lastY = position.getY(); + lastZ = position.getZ(); + } + if (viewChanged) { + lastYaw = position.getYaw(); + lastPitch = position.getPitch(); + } + } @Override diff --git a/src/main/java/net/minestom/server/inventory/PlayerInventory.java b/src/main/java/net/minestom/server/inventory/PlayerInventory.java index 683c6497c..a492909a3 100644 --- a/src/main/java/net/minestom/server/inventory/PlayerInventory.java +++ b/src/main/java/net/minestom/server/inventory/PlayerInventory.java @@ -335,7 +335,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler /** * Refreshes an inventory slot. * - * @param slot the packet slot + * @param slot the packet slot, * see {@link net.minestom.server.utils.inventory.PlayerInventoryUtils#convertToPacketSlot(int)} * @param itemStack the item stack in the slot */ diff --git a/src/main/java/net/minestom/server/listener/PlayerPositionListener.java b/src/main/java/net/minestom/server/listener/PlayerPositionListener.java index 204bcf0c3..20ca663bd 100644 --- a/src/main/java/net/minestom/server/listener/PlayerPositionListener.java +++ b/src/main/java/net/minestom/server/listener/PlayerPositionListener.java @@ -8,6 +8,7 @@ import net.minestom.server.network.packet.client.play.ClientPlayerPositionPacket import net.minestom.server.network.packet.client.play.ClientPlayerRotationPacket; import net.minestom.server.utils.Position; import net.minestom.server.utils.chunk.ChunkUtils; +import org.jetbrains.annotations.NotNull; public class PlayerPositionListener { @@ -47,7 +48,7 @@ public class PlayerPositionListener { processMovement(player, x, y, z, yaw, pitch, onGround); } - private static void processMovement(Player player, float x, float y, float z, + private static void processMovement(@NotNull Player player, float x, float y, float z, float yaw, float pitch, boolean onGround) { // Try to move in an unloaded chunk, prevent it diff --git a/src/main/java/net/minestom/server/network/netty/codec/PacketCompressor.java b/src/main/java/net/minestom/server/network/netty/codec/PacketCompressor.java index c5b13477b..deaf9e937 100644 --- a/src/main/java/net/minestom/server/network/netty/codec/PacketCompressor.java +++ b/src/main/java/net/minestom/server/network/netty/codec/PacketCompressor.java @@ -27,45 +27,38 @@ import java.util.List; import java.util.zip.Deflater; import java.util.zip.Inflater; -// TODO Optimize public class PacketCompressor extends ByteToMessageCodec { - private final byte[] buffer = new byte[8192]; - private final int threshold; - private final Inflater inflater; - private final Deflater deflater; + private byte[] buffer = new byte[8192]; + + private Deflater deflater = new Deflater(); + private Inflater inflater = new Inflater(); public PacketCompressor(int threshold) { - this.inflater = new Inflater(); - this.deflater = new Deflater(); - this.threshold = threshold; } @Override protected void encode(ChannelHandlerContext ctx, ByteBuf from, ByteBuf to) { - int i = from.readableBytes(); + final int packetLength = from.readableBytes(); - if (i < this.threshold) { + if (packetLength < this.threshold) { Utils.writeVarIntBuf(to, 0); to.writeBytes(from); } else { - byte[] abyte = new byte[i]; - from.readBytes(abyte); + Utils.writeVarIntBuf(to, packetLength); - Utils.writeVarIntBuf(to, abyte.length); - this.deflater.setInput(abyte, 0, i); - this.deflater.finish(); + deflater.setInput(from.nioBuffer()); + deflater.finish(); - while (!this.deflater.finished()) { - int j = this.deflater.deflate(this.buffer); - - to.writeBytes(this.buffer, 0, j); + while (!deflater.finished()) { + final int length = deflater.deflate(buffer); + to.writeBytes(buffer, 0, length); } - this.deflater.reset(); + deflater.reset(); } } @@ -85,16 +78,18 @@ public class PacketCompressor extends ByteToMessageCodec { throw new DecoderException("Badly compressed packet - size of " + i + " is larger than protocol maximum of 2097152"); } + // TODO optimize to do not initialize arrays each time + byte[] abyte = new byte[buf.readableBytes()]; buf.readBytes(abyte); - this.inflater.setInput(abyte); + inflater.setInput(abyte); byte[] abyte1 = new byte[i]; - this.inflater.inflate(abyte1); + inflater.inflate(abyte1); out.add(Unpooled.wrappedBuffer(abyte1)); - this.inflater.reset(); + inflater.reset(); } } }