Minor optimizations for MinecraftCompressorAndLengthEncoder and friends
No need to bounce around changing the writer index, we can just set the value directly. Also pull out the handshake checks into a separate function, to improve inlining.
This commit is contained in:
@@ -150,7 +150,7 @@ public enum ProtocolUtils {
|
||||
BinaryTagTypes.COMPOUND, BinaryTagTypes.INT_ARRAY, BinaryTagTypes.LONG_ARRAY};
|
||||
private static final QuietDecoderException BAD_VARINT_CACHED =
|
||||
new QuietDecoderException("Bad VarInt decoded");
|
||||
private static final int[] VAR_INT_LENGTHS = new int[65];
|
||||
private static final int[] VAR_INT_LENGTHS = new int[33];
|
||||
|
||||
static {
|
||||
for (int i = 0; i <= 32; ++i) {
|
||||
@@ -250,16 +250,15 @@ public enum ProtocolUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the specified {@code value} as a 21-bit Minecraft VarInt to the specified {@code buf}.
|
||||
* Directly encodes a 21-bit Minecraft VarInt, ready to be written with {@link ByteBuf#writeMedium(int)}.
|
||||
* The upper 11 bits will be discarded.
|
||||
*
|
||||
* @param buf the buffer to read from
|
||||
* @param value the integer to write
|
||||
* @param value the value to encode
|
||||
* @return the encoded value
|
||||
*/
|
||||
public static void write21BitVarInt(ByteBuf buf, int value) {
|
||||
public static int encode21BitVarInt(int value) {
|
||||
// See https://steinborn.me/posts/performance/how-fast-can-you-write-a-varint/
|
||||
int w = (value & 0x7F | 0x80) << 16 | ((value >>> 7) & 0x7F | 0x80) << 8 | (value >>> 14);
|
||||
buf.writeMedium(w);
|
||||
return (value & 0x7F | 0x80) << 16 | ((value >>> 7) & 0x7F | 0x80) << 8 | (value >>> 14);
|
||||
}
|
||||
|
||||
public static String readString(ByteBuf buf) {
|
||||
|
||||
@@ -46,7 +46,7 @@ public class MinecraftCompressorAndLengthEncoder extends MessageToByteEncoder<By
|
||||
if (uncompressed < threshold) {
|
||||
// Under the threshold, there is nothing to do.
|
||||
ProtocolUtils.writeVarInt(out, uncompressed + 1);
|
||||
ProtocolUtils.writeVarInt(out, 0);
|
||||
out.writeByte(0);
|
||||
out.writeBytes(msg);
|
||||
} else {
|
||||
handleCompressed(ctx, msg, out);
|
||||
@@ -57,7 +57,7 @@ public class MinecraftCompressorAndLengthEncoder extends MessageToByteEncoder<By
|
||||
throws DataFormatException {
|
||||
int uncompressed = msg.readableBytes();
|
||||
|
||||
ProtocolUtils.write21BitVarInt(out, 0); // Dummy packet length
|
||||
out.writeMedium(0); // Reserve the packet length
|
||||
ProtocolUtils.writeVarInt(out, uncompressed);
|
||||
ByteBuf compatibleIn = MoreByteBufUtils.ensureCompatible(ctx.alloc(), compressor, msg);
|
||||
|
||||
@@ -72,11 +72,8 @@ public class MinecraftCompressorAndLengthEncoder extends MessageToByteEncoder<By
|
||||
throw new DataFormatException("The server sent a very large (over 2MiB compressed) packet.");
|
||||
}
|
||||
|
||||
int writerIndex = out.writerIndex();
|
||||
int packetLength = out.readableBytes() - 3;
|
||||
out.writerIndex(0);
|
||||
ProtocolUtils.write21BitVarInt(out, packetLength); // Rewrite packet length
|
||||
out.writerIndex(writerIndex);
|
||||
out.setMedium(0, ProtocolUtils.encode21BitVarInt(packetLength)); // Rewrite packet length
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -83,9 +83,8 @@ public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder {
|
||||
|
||||
// try to read the length of the packet
|
||||
in.markReaderIndex();
|
||||
int preIndex = in.readerIndex();
|
||||
int length = readRawVarInt21(in);
|
||||
if (preIndex == in.readerIndex()) {
|
||||
if (packetStart == in.readerIndex()) {
|
||||
return;
|
||||
}
|
||||
if (length < 0) {
|
||||
@@ -94,6 +93,23 @@ public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder {
|
||||
|
||||
if (length > 0) {
|
||||
if (state == StateRegistry.HANDSHAKE && direction == ProtocolUtils.Direction.SERVERBOUND) {
|
||||
if (validateServerboundHandshakePacket(in, length)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// note that zero-length packets are ignored
|
||||
if (length > 0) {
|
||||
if (in.readableBytes() < length) {
|
||||
in.resetReaderIndex();
|
||||
} else {
|
||||
out.add(in.readRetainedSlice(length));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean validateServerboundHandshakePacket(ByteBuf in, int length) throws Exception {
|
||||
StateRegistry.PacketRegistry.ProtocolRegistry registry =
|
||||
state.getProtocolRegistry(direction, ProtocolVersion.MINIMUM_VERSION);
|
||||
|
||||
@@ -102,7 +118,7 @@ public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder {
|
||||
// Index hasn't changed, we've read nothing
|
||||
if (index == in.readerIndex()) {
|
||||
in.resetReaderIndex();
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
final int payloadLength = length - ProtocolUtils.varIntBytes(packetId);
|
||||
|
||||
@@ -124,19 +140,8 @@ public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder {
|
||||
throw handleUnderflow(packet, expectedMaxLen, in.readableBytes());
|
||||
}
|
||||
|
||||
|
||||
in.readerIndex(index);
|
||||
}
|
||||
}
|
||||
|
||||
// note that zero-length packets are ignored
|
||||
if (length > 0) {
|
||||
if (in.readableBytes() < length) {
|
||||
in.resetReaderIndex();
|
||||
} else {
|
||||
out.add(in.readRetainedSlice(length));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
package com.velocitypowered.proxy.protocol;
|
||||
|
||||
import static com.velocitypowered.proxy.protocol.ProtocolUtils.encode21BitVarInt;
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@@ -83,7 +84,7 @@ public class ProtocolUtilsTest {
|
||||
|
||||
private void writeReadTest3Bytes(ByteBuf buf, int test) {
|
||||
buf.clear();
|
||||
ProtocolUtils.write21BitVarInt(buf, test);
|
||||
buf.writeMedium(encode21BitVarInt(test));
|
||||
assertEquals(test, ProtocolUtils.readVarInt(buf));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user