mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-03 05:51:37 +01:00
Improve/simplify packet memory allocation
This commit is contained in:
parent
7edc508662
commit
56a34f73d9
@ -192,7 +192,7 @@ public class NettyPlayerConnection extends PlayerConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
synchronized (tickBuffer) {
|
synchronized (tickBuffer) {
|
||||||
PacketUtils.writeFramedPacket(tickBuffer, serverPacket, false);
|
PacketUtils.writeFramedPacket(tickBuffer, serverPacket);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
} else if (message instanceof ByteBuf) {
|
} else if (message instanceof ByteBuf) {
|
||||||
|
@ -194,7 +194,7 @@ public final class PacketUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void writeFramedPacket(@NotNull ByteBuf buffer,
|
public static void writeFramedPacket(@NotNull ByteBuf buffer,
|
||||||
@NotNull ServerPacket serverPacket, boolean directBuffer) {
|
@NotNull ServerPacket serverPacket) {
|
||||||
final int compressionThreshold = MinecraftServer.getCompressionThreshold();
|
final int compressionThreshold = MinecraftServer.getCompressionThreshold();
|
||||||
final boolean compression = compressionThreshold > 0;
|
final boolean compression = compressionThreshold > 0;
|
||||||
|
|
||||||
@ -214,17 +214,14 @@ public final class PacketUtils {
|
|||||||
if (packetSize >= compressionThreshold) {
|
if (packetSize >= compressionThreshold) {
|
||||||
// Packet large enough
|
// Packet large enough
|
||||||
|
|
||||||
// Compress id + payload
|
|
||||||
ByteBuf compressedBuf = directBuffer ? BufUtils.getBuffer(true) : Unpooled.buffer();
|
|
||||||
final Deflater deflater = DEFLATER.get();
|
final Deflater deflater = DEFLATER.get();
|
||||||
compress(deflater, buffer.slice(contentIndex, packetSize), compressedBuf);
|
// Compress id + payload
|
||||||
|
ByteBuf uncompressedCopy = buffer.copy(contentIndex, packetSize);
|
||||||
|
buffer.writerIndex(contentIndex);
|
||||||
|
compress(deflater, uncompressedCopy, buffer);
|
||||||
|
uncompressedCopy.release();
|
||||||
|
|
||||||
final int totalPacketLength = compressedBuf.readableBytes() + hardcodedVarIntSize;
|
final int totalPacketLength = buffer.writerIndex() - contentIndex + hardcodedVarIntSize;
|
||||||
|
|
||||||
// Replace uncompressed by compressed data
|
|
||||||
buffer.setBytes(contentIndex, compressedBuf);
|
|
||||||
buffer.writerIndex(contentIndex + compressedBuf.writerIndex());
|
|
||||||
compressedBuf.release();
|
|
||||||
|
|
||||||
// Update header values
|
// Update header values
|
||||||
Utils.overrideVarInt(buffer, packetLengthIndex, hardcodedVarIntSize, totalPacketLength);
|
Utils.overrideVarInt(buffer, packetLengthIndex, hardcodedVarIntSize, totalPacketLength);
|
||||||
@ -263,7 +260,7 @@ public final class PacketUtils {
|
|||||||
@NotNull
|
@NotNull
|
||||||
public static ByteBuf createFramedPacket(@NotNull ServerPacket serverPacket, boolean directBuffer) {
|
public static ByteBuf createFramedPacket(@NotNull ServerPacket serverPacket, boolean directBuffer) {
|
||||||
ByteBuf packetBuf = directBuffer ? BufUtils.getBuffer(true) : Unpooled.buffer();
|
ByteBuf packetBuf = directBuffer ? BufUtils.getBuffer(true) : Unpooled.buffer();
|
||||||
writeFramedPacket(packetBuf, serverPacket, directBuffer);
|
writeFramedPacket(packetBuf, serverPacket);
|
||||||
return packetBuf;
|
return packetBuf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user