9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-19 15:09:25 +00:00
Files
Leaf/leaf-server/paper-patches/features/0025-Optimize-VarInt-write-and-VarLong-write.patch
2025-04-07 11:35:31 -04:00

82 lines
4.7 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Martijn Muijsers <martijnmuijsers@live.nl>
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 <git@steinborn.me>
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 bbea09ffc2180c3c62e15d7dff51f8c220425bfe..0c11670d8d7307307a7cd12be5d0dca40155829d 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;
@@ -156,4 +157,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
+
}