From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Martijn Muijsers Date: Tue, 22 Aug 2023 21:38:37 +0200 Subject: [PATCH] Optimize VarInt#write and VarLong#write License: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html) Gale - https://galemc.org This patch is based on the following commit: "Reapply "Optimize varint writing"" By: Andrew Steinborn As part of: Velocity (https://github.com/PaperMC/Velocity) Licensed under: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html) * Velocity description * Inspired by the approach described at the bottom of https://richardstartin.github.io/posts/dont-use-protobuf-for-telemetry Given that we do a lot of varint writing as well, this should provide a small performance boost for larger/complex packets whilst not regressing hard on smaller packets. This includes a test to ensure that the behavior is as expected and fixes the initialization loop so that the correct results will be given. Much thanks to @octylFractal for acting as my duck while trying to figure this out. diff --git a/src/test/java/net/minecraft/network/VarIntLongTest.java b/src/test/java/net/minecraft/network/VarIntLongTest.java index 04959045a14ccdce3f836e43e270bcf3349b28bb..96853a1db7998e68cb0404eb1c339e807cfd339e 100644 --- a/src/test/java/net/minecraft/network/VarIntLongTest.java +++ b/src/test/java/net/minecraft/network/VarIntLongTest.java @@ -2,6 +2,7 @@ package net.minecraft.network; +import io.netty.buffer.Unpooled; import it.unimi.dsi.fastutil.ints.IntOpenHashSet; import it.unimi.dsi.fastutil.ints.IntSet; import it.unimi.dsi.fastutil.longs.LongOpenHashSet; @@ -158,4 +159,46 @@ public class VarIntLongTest { } // Gale end - Velocity - pre-compute VarInt and VarLong sizes + // Gale - Velocity - optimized VarInt#write and VarLong#write + @Test + public void testWriteVarIntComparedToOld() { + integerCases.forEach(value -> { + // given + int capacity = 5; + FriendlyByteBuf buf1 = new FriendlyByteBuf(Unpooled.buffer(capacity)); + FriendlyByteBuf buf2 = new FriendlyByteBuf(Unpooled.buffer(capacity)); + VarInt.writeOld(buf1, value); + + // when + buf2.writeVarInt(value); + + // then + Assertions.assertEquals(buf1.writerIndex(), buf2.writerIndex(), "Writer index of optimized buffer (" + buf2.writerIndex() + ") is not equal to writer index of original buffer (" + buf1.writerIndex() + ") for test case value " + value + " (binary: " + padStringWithLeadingZeros(Integer.toBinaryString(value), 32) + ")"); + for (int i = 0; i < capacity; i++) { + Assertions.assertEquals(buf1.getByte(i), buf2.getByte(i), "Buffer byte (at index " + i + ") in optimized buffer (" + buf2.getByte(i) + " (binary: " + padStringWithLeadingZeros(Integer.toBinaryString(Byte.toUnsignedInt(buf2.getByte(i))), 8) + ")) is not equal to the same byte in original buffer (" + buf1.getByte(i) + " (binary: " + padStringWithLeadingZeros(Integer.toBinaryString(Byte.toUnsignedInt(buf1.getByte(i))), 8) + ")) for test case value " + value + " (binary: " + padStringWithLeadingZeros(Integer.toBinaryString(value), 32) + ")"); + } + }); + } + + @Test + public void testWriteVarLongComparedToOld() { + longCases.forEach(value -> { + // given + int capacity = 10; + FriendlyByteBuf buf1 = new FriendlyByteBuf(Unpooled.buffer(capacity)); + FriendlyByteBuf buf2 = new FriendlyByteBuf(Unpooled.buffer(capacity)); + VarLong.writeOld(buf1, value); + + // when + buf2.writeVarLong(value); + + // then + Assertions.assertEquals(buf1.writerIndex(), buf2.writerIndex(), "Writer index of optimized buffer (" + buf2.writerIndex() + ") is not equal to writer index of original buffer (" + buf1.writerIndex() + ") for test case value " + value + " (binary: " + padStringWithLeadingZeros(Long.toBinaryString(value), 64) + ")"); + for (int i = 0; i < capacity; i++) { + Assertions.assertEquals(buf1.getByte(i), buf2.getByte(i), "Buffer byte (at index " + i + ") in optimized buffer (" + buf2.getByte(i) + " (binary: " + padStringWithLeadingZeros(Integer.toBinaryString(Byte.toUnsignedInt(buf2.getByte(i))), 8) + ")) is not equal to the same byte in original buffer (" + buf1.getByte(i) + " (binary: " + padStringWithLeadingZeros(Integer.toBinaryString(Byte.toUnsignedInt(buf1.getByte(i))), 8) + ")) for test case value " + value + " (binary: " + padStringWithLeadingZeros(Long.toBinaryString(value), 64) + ")"); + } + }); + } + // Gale end - Velocity - optimized VarInt#write and VarLong#write + }