From e531cdb373c654748f3c819b80a5286d9d431d21 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Mon, 3 May 2021 18:07:25 -0400 Subject: [PATCH] Revert "Optimize varint writing" This reverts commit 3db2fe8d63162c716b8ac57ae0898027ecd3a7bb. --- .../proxy/protocol/ProtocolUtils.java | 26 +++++-------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java index f7424b05b..50f6c48d0 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java @@ -62,17 +62,6 @@ public enum ProtocolUtils { private static final int DEFAULT_MAX_STRING_SIZE = 65536; // 64KiB private static final QuietDecoderException BAD_VARINT_CACHED = new QuietDecoderException("Bad varint decoded"); - private static final int[] VAR_INT_LENGTHS = new int[33]; - - static { - // Inspired by https://richardstartin.github.io/posts/dont-use-protobuf-for-telemetry - // - // This has been slightly modified in that we reduce the length to 32-bit only, since Velocity - // doesn't look at any part of the Minecraft protocol that requires us to look at VarLongs. - for (int i = 0; i <= 32; ++i) { - VAR_INT_LENGTHS[i] = (31 - i) / 7; - } - } /** * Reads a Minecraft-style VarInt from the specified {@code buf}. @@ -108,22 +97,21 @@ public enum ProtocolUtils { return Integer.MIN_VALUE; } - public static int varintBytes(int value) { - return VAR_INT_LENGTHS[Integer.numberOfLeadingZeros(value)]; - } - /** * Writes a Minecraft-style VarInt to the specified {@code buf}. * @param buf the buffer to read from * @param value the integer to write */ public static void writeVarInt(ByteBuf buf, int value) { - int length = varintBytes(value); - for (int i = 0; i < length; ++i) { - buf.writeByte(((byte) ((value & 0x7F) | 0x80))); + while (true) { + if ((value & 0xFFFFFF80) == 0) { + buf.writeByte(value); + return; + } + + buf.writeByte(value & 0x7F | 0x80); value >>>= 7; } - buf.writeByte((byte) value); } public static String readString(ByteBuf buf) {