From 0191b74840adc9969355a9e242cf7996cb592f57 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 3 Aug 2018 05:21:35 -0400 Subject: [PATCH] Remove another memory copy with compression. I considered using composite byte buffers but they would have added GC overhead and would've been incompatible with any native code we added unless special care was taken. --- .../protocol/netty/MinecraftCompressEncoder.java | 16 ++++------------ .../netty/MinecraftVarintFrameDecoder.java | 2 +- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressEncoder.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressEncoder.java index 775ea3e02..f62834b93 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressEncoder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressEncoder.java @@ -17,22 +17,14 @@ public class MinecraftCompressEncoder extends MessageToByteEncoder { @Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { - if (msg.readableBytes() <= threshold) { + int uncompressed = msg.readableBytes(); + if (uncompressed <= threshold) { // Under the threshold, there is nothing to do. ProtocolUtils.writeVarInt(out, 0); out.writeBytes(msg); - return; - } - - // in other words, see if a plain 8KiB buffer fits us well - ByteBuf compressedBuffer = ctx.alloc().buffer(8192); - try { - int uncompressed = msg.readableBytes(); - compressor.deflate(msg, compressedBuffer); + } else { ProtocolUtils.writeVarInt(out, uncompressed); - out.writeBytes(compressedBuffer); - } finally { - compressedBuffer.release(); + compressor.deflate(msg, out); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftVarintFrameDecoder.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftVarintFrameDecoder.java index 0afa0637d..081253594 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftVarintFrameDecoder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftVarintFrameDecoder.java @@ -10,7 +10,7 @@ import java.util.List; public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { - if (in.readableBytes() < 1) { + if (!in.isReadable()) { return; }