Avoid slice allocation when compressing packets

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-11-03 03:06:19 +01:00
parent d919e1b60d
commit b18df47feb

View File

@ -199,15 +199,12 @@ public final class PacketUtils {
final int packetSize = buffer.position() - contentStart; final int packetSize = buffer.position() - contentStart;
final boolean compressed = packetSize >= MinecraftServer.getCompressionThreshold(); final boolean compressed = packetSize >= MinecraftServer.getCompressionThreshold();
if (compressed) { if (compressed) {
// Packet large enough, compress // Packet large enough, compress it
buffer.position(contentStart); final ByteBuffer input = localBuffer().put(0, buffer, contentStart, packetSize);
final ByteBuffer uncompressedContent = buffer.slice().limit(packetSize);
final ByteBuffer uncompressedCopy = localBuffer().put(uncompressedContent).flip();
Deflater deflater = LOCAL_DEFLATER.get(); Deflater deflater = LOCAL_DEFLATER.get();
deflater.setInput(uncompressedCopy); deflater.setInput(input.limit(packetSize));
deflater.finish(); deflater.finish();
deflater.deflate(buffer); deflater.deflate(buffer.position(contentStart));
deflater.reset(); deflater.reset();
} }
// Packet header (Packet + Data Length) // Packet header (Packet + Data Length)