Improve/simplify packet memory allocation

This commit is contained in:
themode 2021-03-28 15:58:52 +02:00
parent 7edc508662
commit 56a34f73d9
2 changed files with 9 additions and 12 deletions

View File

@ -192,7 +192,7 @@ public class NettyPlayerConnection extends PlayerConnection {
}
synchronized (tickBuffer) {
PacketUtils.writeFramedPacket(tickBuffer, serverPacket, false);
PacketUtils.writeFramedPacket(tickBuffer, serverPacket);
}
return;
} else if (message instanceof ByteBuf) {

View File

@ -194,7 +194,7 @@ public final class PacketUtils {
}
public static void writeFramedPacket(@NotNull ByteBuf buffer,
@NotNull ServerPacket serverPacket, boolean directBuffer) {
@NotNull ServerPacket serverPacket) {
final int compressionThreshold = MinecraftServer.getCompressionThreshold();
final boolean compression = compressionThreshold > 0;
@ -214,17 +214,14 @@ public final class PacketUtils {
if (packetSize >= compressionThreshold) {
// Packet large enough
// Compress id + payload
ByteBuf compressedBuf = directBuffer ? BufUtils.getBuffer(true) : Unpooled.buffer();
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;
// Replace uncompressed by compressed data
buffer.setBytes(contentIndex, compressedBuf);
buffer.writerIndex(contentIndex + compressedBuf.writerIndex());
compressedBuf.release();
final int totalPacketLength = buffer.writerIndex() - contentIndex + hardcodedVarIntSize;
// Update header values
Utils.overrideVarInt(buffer, packetLengthIndex, hardcodedVarIntSize, totalPacketLength);
@ -263,7 +260,7 @@ public final class PacketUtils {
@NotNull
public static ByteBuf createFramedPacket(@NotNull ServerPacket serverPacket, boolean directBuffer) {
ByteBuf packetBuf = directBuffer ? BufUtils.getBuffer(true) : Unpooled.buffer();
writeFramedPacket(packetBuf, serverPacket, directBuffer);
writeFramedPacket(packetBuf, serverPacket);
return packetBuf;
}