Optimize native handling further.

We now try to work within the boundaries given by the native. In the
case of Java natives, we work with byte arrays. With natives, always use
direct buffers.

However, the numbers do favor the natives, since they work with direct
byte buffers, without any copying. For the most part, this commit is
intended to improve the lives of Velocity users on Windows.
This commit is contained in:
Andrew Steinborn
2018-12-30 06:16:04 -05:00
parent 095a478440
commit 8588e7f1fe
10 changed files with 121 additions and 57 deletions

View File

@@ -1,9 +1,10 @@
package com.velocitypowered.proxy.protocol.netty;
import static com.velocitypowered.natives.util.MoreByteBufUtils.ensureCompatible;
import static com.velocitypowered.natives.util.MoreByteBufUtils.preferredBuffer;
import static com.velocitypowered.proxy.protocol.util.NettyPreconditions.checkFrame;
import com.velocitypowered.natives.compression.VelocityCompressor;
import com.velocitypowered.natives.util.MoreByteBufUtils;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
@@ -35,9 +36,9 @@ public class MinecraftCompressDecoder extends MessageToMessageDecoder<ByteBuf> {
checkFrame(expectedUncompressedSize >= threshold,
"Uncompressed size %s is greater than threshold %s",
expectedUncompressedSize, threshold);
ByteBuf compatibleIn = MoreByteBufUtils.ensureCompatible(ctx.alloc(), compressor, in);
ByteBuf uncompressed = ctx.alloc().directBuffer(Math.min(expectedUncompressedSize,
MAXIMUM_INITIAL_BUFFER_SIZE));
ByteBuf compatibleIn = ensureCompatible(ctx.alloc(), compressor, in);
int initialCapacity = Math.min(expectedUncompressedSize, MAXIMUM_INITIAL_BUFFER_SIZE);
ByteBuf uncompressed = preferredBuffer(ctx.alloc(), compressor, initialCapacity);
try {
compressor.inflate(compatibleIn, uncompressed);
checkFrame(expectedUncompressedSize == uncompressed.readableBytes(),

View File

@@ -38,11 +38,9 @@ public class MinecraftCompressEncoder extends MessageToByteEncoder<ByteBuf> {
@Override
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect)
throws Exception {
if (msg.readableBytes() <= threshold) {
return ctx.alloc().directBuffer(msg.readableBytes() + 1);
}
// A reasonable assumption about compression savings
return ctx.alloc().directBuffer(msg.readableBytes() / 3);
int initialBufferSize = msg.readableBytes() <= threshold ? msg.readableBytes() + 1 :
msg.readableBytes() / 3;
return MoreByteBufUtils.preferredBuffer(ctx.alloc(), compressor, initialBufferSize);
}
@Override