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/0021-Pre-compute-VarLong-sizes.patch
Dreeam c8ba5fedc8 Updated Upstream (Paper)
Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@9b1798d6 Simplify custom payload handling (#12347)
PaperMC/Paper@2552abf0 fix message mutation in PlayerSetSpawnEvent
PaperMC/Paper@ae99e24f fix deprecated bungee chat api methods
PaperMC/Paper@dca4aab8 add util methods to CraftChatMessage
PaperMC/Paper@87c9d9b0 be more lenient on url parsing for legacy format
PaperMC/Paper@4a9bd2e3 Correctly clear items in PlayerDeathEvent
PaperMC/Paper@a70f7745 fix unsaveable launched trident
PaperMC/Paper@41a094cf move block data/state impl
PaperMC/Paper@6b26b219 remove hardcoded durability from material
PaperMC/Paper@db8c646d Merge remote-tracking branch 'origin/main' into update/1.21.5
2025-03-31 09:53:16 -04:00

188 lines
7.3 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Martijn Muijsers <martijnmuijsers@live.nl>
Date: Mon, 21 Aug 2023 21:46:10 +0200
Subject: [PATCH] Pre-compute VarLong sizes
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
new file mode 100644
index 0000000000000000000000000000000000000000..bbea09ffc2180c3c62e15d7dff51f8c220425bfe
--- /dev/null
+++ b/src/test/java/net/minecraft/network/VarIntLongTest.java
@@ -0,0 +1,159 @@
+// Gale - Velocity - VarInt and VarLong optimizations
+
+package net.minecraft.network;
+
+import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
+import it.unimi.dsi.fastutil.ints.IntSet;
+import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
+import it.unimi.dsi.fastutil.longs.LongSet;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class VarIntLongTest {
+
+ private static String padStringWithLeadingZeros(String string, int length) {
+ if (string.length() >= length) {
+ return string;
+ }
+ return "0".repeat(length - string.length()) + string;
+ }
+
+ private static final IntSet integerCases;
+ static {
+ integerCases = new IntOpenHashSet();
+ {
+ integerCases.add(0);
+ integerCases.add(-1);
+ }
+ {
+ for (int i = 0; i < 32; i++) {
+ integerCases.add(1 << i);
+ }
+ }
+ {
+ for (int factor = 1; factor <= 7; factor++) {
+ int all = 0;
+ for (int i = 0; i <= 4; i++) {
+ int shifted = 1 << (i * factor);
+ all |= shifted;
+ integerCases.add(shifted);
+ integerCases.add(shifted - 1);
+ integerCases.add(~shifted);
+ integerCases.add(-shifted);
+ integerCases.add((~shifted) & (0x80000000));
+ integerCases.add(all);
+ integerCases.add(all - 1);
+ integerCases.add(~all);
+ integerCases.add(-all);
+ integerCases.add((~all) & (0x80000000));
+ }
+ }
+ }
+ {
+ var newCases = new IntOpenHashSet();
+ for (int shiftSize = 2; shiftSize <= 6; shiftSize++) {
+ for (int offset = 0; offset < shiftSize; offset++) {
+ int striped = 0;
+ for (int i = offset; i < 32; i += shiftSize) {
+ striped |= 1 << i;
+ }
+ final var finalStriped = striped;
+ integerCases.forEach(existing -> {
+ newCases.add(existing | finalStriped);
+ newCases.add(existing | (~finalStriped));
+ newCases.add(existing & finalStriped);
+ newCases.add(existing & (~finalStriped));
+ newCases.add(existing - finalStriped);
+ newCases.add(existing - (~finalStriped));
+ });
+ }
+ }
+ integerCases.addAll(newCases);
+ }
+ }
+
+ private static final LongSet longCases;
+ static {
+ longCases = new LongOpenHashSet();
+ {
+ longCases.add(0);
+ longCases.add(-1);
+ }
+ {
+ for (int i = 0; i < 64; i++) {
+ longCases.add(1L << i);
+ }
+ }
+ {
+ for (int factor = 1; factor <= 7; factor++) {
+ long all = 0;
+ for (int i = 0; i <= 9; i++) {
+ long shifted = 1L << (i * factor);
+ all |= shifted;
+ longCases.add(shifted);
+ longCases.add(shifted - 1);
+ longCases.add(~shifted);
+ longCases.add(-shifted);
+ longCases.add((~shifted) & (0x8000000000000000L));
+ longCases.add(all);
+ longCases.add(all - 1);
+ longCases.add(~all);
+ longCases.add(-all);
+ longCases.add((~all) & (0x8000000000000000L));
+ }
+ }
+ }
+ {
+ var newCases = new LongOpenHashSet();
+ for (int shiftSize = 2; shiftSize <= 6; shiftSize++) {
+ for (int offset = 0; offset < shiftSize; offset++) {
+ long striped = 0;
+ for (int i = offset; i < 64; i += shiftSize) {
+ striped |= 1L << i;
+ }
+ final var finalStriped = striped;
+ longCases.forEach(existing -> {
+ newCases.add(existing | finalStriped);
+ newCases.add(existing | (~finalStriped));
+ newCases.add(existing & finalStriped);
+ newCases.add(existing & (~finalStriped));
+ newCases.add(existing - finalStriped);
+ newCases.add(existing - (~finalStriped));
+ });
+ }
+ }
+ longCases.addAll(newCases);
+ }
+ }
+
+ // Gale start - Velocity - pre-compute VarInt and VarLong sizes
+ @Test
+ public void testGetVarIntSizeComparedToOld() {
+ integerCases.forEach(value -> {
+ // given
+ int originalSize = VarInt.getByteSizeOld(value);
+
+ // when
+ int size = VarInt.getByteSize(value);
+
+ // then
+ Assertions.assertEquals(originalSize, size, "Optimized size (" + size + ") is not equal to original size (" + originalSize + ") for test case value " + value + " (binary: " + padStringWithLeadingZeros(Integer.toBinaryString(value), 32) + ")");
+ });
+ }
+
+ @Test
+ public void testGetVarLongSizeComparedToOld() {
+ longCases.forEach(value -> {
+ // given
+ int originalSize = VarLong.getByteSizeOld(value);
+
+ // when
+ int size = VarLong.getByteSize(value);
+
+ // then
+ Assertions.assertEquals(originalSize, size, "Optimized size (" + size + ") is not equal to original size (" + originalSize + ") for test case value " + value + " (binary: " + padStringWithLeadingZeros(Long.toBinaryString(value), 64) + ")");
+ });
+ }
+ // Gale end - Velocity - pre-compute VarInt and VarLong sizes
+
+}