From 876e3b3853d6e8fbaaea34671ff2f78a698a3700 Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Sun, 26 May 2024 15:40:25 +0800 Subject: [PATCH] Add back Optimize VarInt#write --- .../0128-Pre-compute-VarLong-sizes.patch | 32 ++++---- ...mize-VarInt-write-and-VarLong-write.patch} | 74 +++++++++++++++++-- 2 files changed, 81 insertions(+), 25 deletions(-) rename patches/server/{0129-Optimize-VarLong-write.patch => 0129-Optimize-VarInt-write-and-VarLong-write.patch} (78%) diff --git a/patches/server/0128-Pre-compute-VarLong-sizes.patch b/patches/server/0128-Pre-compute-VarLong-sizes.patch index 0bf3bc6..58253fc 100644 --- a/patches/server/0128-Pre-compute-VarLong-sizes.patch +++ b/patches/server/0128-Pre-compute-VarLong-sizes.patch @@ -21,35 +21,31 @@ Given that we do a lot of varint writing as well, this should provide a small pe 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/main/java/net/minecraft/network/VarLong.java b/src/main/java/net/minecraft/network/VarLong.java -index c4fd722f4203e3f4de7b41de9d6ef47b5cfaed10..b73a50fa8465ea4d392a65146eb2775a50917290 100644 +index c4fd722f4203e3f4de7b41de9d6ef47b5cfaed10..11339185fc70b1a34130c92d28c397d4b05569cb 100644 --- a/src/main/java/net/minecraft/network/VarLong.java +++ b/src/main/java/net/minecraft/network/VarLong.java -@@ -8,7 +8,22 @@ public class VarLong { - private static final int CONTINUATION_BIT_MASK = 128; +@@ -9,6 +9,18 @@ public class VarLong { private static final int DATA_BITS_PER_BYTE = 7; -+ // Gale start - Velocity - pre-compute VarInt and VarLong sizes -+ private static final int[] EXACT_BYTE_LENGTHS = new int[65]; -+ static { -+ for (int i = 0; i < 64; ++i) { -+ EXACT_BYTE_LENGTHS[i] = (64 - i + 6) / 7; -+ } -+ EXACT_BYTE_LENGTHS[64] = 1; // Special case for the number 0 -+ } -+ // Gale end - Velocity - pre-compute VarInt and VarLong sizes public static int getByteSize(long l) { + // Gale start - Velocity - pre-compute VarInt and VarLong sizes -+ return EXACT_BYTE_LENGTHS[Long.numberOfLeadingZeros(l)]; ++ return VARLONG_EXACT_BYTE_LENGTHS[Long.numberOfLeadingZeros(l)]; + } -+ -+ static int getByteSizeOriginal(long l) { // public -> package-private ++ private static final int[] VARLONG_EXACT_BYTE_LENGTHS = new int[65]; ++ static { ++ for (int i = 0; i < 64; ++i) { ++ VARLONG_EXACT_BYTE_LENGTHS[i] = (int) Math.ceil((63d - (i - 1)) / 7d); ++ } ++ VARLONG_EXACT_BYTE_LENGTHS[64] = 1; // Special case for the number 0 ++ } ++ static int getByteSizeOld(long l) { // public -> package-private + // Gale end - Velocity - pre-compute VarInt and VarLong sizes for (int i = 1; i < 10; i++) { if ((l & -1L << i * 7) == 0L) { return i; 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..f8a1b72d70a47dbc75cdb4f7a536dbb58354661f +index 0000000000000000000000000000000000000000..bbea09ffc2180c3c62e15d7dff51f8c220425bfe --- /dev/null +++ b/src/test/java/net/minecraft/network/VarIntLongTest.java @@ -0,0 +1,159 @@ @@ -197,10 +193,10 @@ index 0000000000000000000000000000000000000000..f8a1b72d70a47dbc75cdb4f7a536dbb5 + } + + @Test -+ public void testGetVarLongSizeComparedToOriginal() { ++ public void testGetVarLongSizeComparedToOld() { + longCases.forEach(value -> { + // given -+ int originalSize = VarLong.getByteSizeOriginal(value); ++ int originalSize = VarLong.getByteSizeOld(value); + + // when + int size = VarLong.getByteSize(value); diff --git a/patches/server/0129-Optimize-VarLong-write.patch b/patches/server/0129-Optimize-VarInt-write-and-VarLong-write.patch similarity index 78% rename from patches/server/0129-Optimize-VarLong-write.patch rename to patches/server/0129-Optimize-VarInt-write-and-VarLong-write.patch index d8b1eac..e66b07a 100644 --- a/patches/server/0129-Optimize-VarLong-write.patch +++ b/patches/server/0129-Optimize-VarInt-write-and-VarLong-write.patch @@ -1,7 +1,7 @@ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Martijn Muijsers Date: Tue, 22 Aug 2023 21:38:37 +0200 -Subject: [PATCH] Optimize VarLong#write +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 @@ -20,11 +20,71 @@ Given that we do a lot of varint writing as well, this should provide a small pe 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/main/java/net/minecraft/network/VarInt.java b/src/main/java/net/minecraft/network/VarInt.java +index 74ed47659d3e615c2dae7da98d5a8cf1559625bf..82a8354b5ce503508716af3966b9809a6b4d956c 100644 +--- a/src/main/java/net/minecraft/network/VarInt.java ++++ b/src/main/java/net/minecraft/network/VarInt.java +@@ -51,6 +51,41 @@ public class VarInt { + } + + public static ByteBuf write(ByteBuf buf, int i) { ++ // Gale start - Velocity - optimized VarInt#write ++ if ((i & 0xFFFFFF80) == 0) { ++ buf.writeByte(i); ++ } else if ((i & 0xFFFFC000) == 0) { ++ int w = (i & 0x7F) << 8 ++ | (i >>> 7) ++ | 0x00008000; ++ buf.writeShort(w); ++ } else if ((i & 0xFFE00000) == 0) { ++ int w = (i & 0x7F) << 16 ++ | (i & 0x3F80) << 1 ++ | (i >>> 14) ++ | 0x00808000; ++ buf.writeMedium(w); ++ } else if ((i & 0xF0000000) == 0) { ++ int w = (i & 0x7F) << 24 ++ | ((i & 0x3F80) << 9) ++ | (i & 0x1FC000) >> 6 ++ | (i >>> 21) ++ | 0x80808000; ++ buf.writeInt(w); ++ } else { ++ int w = (i & 0x7F) << 24 ++ | (i & 0x3F80) << 9 ++ | (i & 0x1FC000) >> 6 ++ | ((i >>> 21) & 0x7F) ++ | 0x80808080; ++ buf.writeInt(w); ++ buf.writeByte(i >>> 28); ++ } ++ return buf; ++ } ++ ++ static ByteBuf writeOld(ByteBuf buf, int i) { // public -> package-private ++ // Gale end - Velocity - optimized VarInt#write + // Paper start - Optimize VarInts + // Peel the one and two byte count cases explicitly as they are the most common VarInt sizes + // that the proxy will write, to improve inlining. +@@ -60,11 +95,11 @@ public class VarInt { + int w = (i & 0x7F | 0x80) << 8 | (i >>> 7); + buf.writeShort(w); + } else { +- writeOld(buf, i); ++ writeOld2(buf, i); // rename + } + return buf; + } +- public static ByteBuf writeOld(ByteBuf buf, int i) { ++ public static ByteBuf writeOld2(ByteBuf buf, int i) { // rename + // Paper end - Optimize VarInts + while ((i & -128) != 0) { + buf.writeByte(i & 127 | 128); diff --git a/src/main/java/net/minecraft/network/VarLong.java b/src/main/java/net/minecraft/network/VarLong.java -index b73a50fa8465ea4d392a65146eb2775a50917290..a584a44680a034be4f9848cbfea4af4c87d8de7d 100644 +index 11339185fc70b1a34130c92d28c397d4b05569cb..c127dd051572177e4fc379bb7e2d40571d0abeed 100644 --- a/src/main/java/net/minecraft/network/VarLong.java +++ b/src/main/java/net/minecraft/network/VarLong.java -@@ -54,6 +54,127 @@ public class VarLong { +@@ -51,6 +51,127 @@ public class VarLong { } public static ByteBuf write(ByteBuf buf, long l) { @@ -147,13 +207,13 @@ index b73a50fa8465ea4d392a65146eb2775a50917290..a584a44680a034be4f9848cbfea4af4c + return buf; + } + -+ static ByteBuf writeOriginal(ByteBuf buf, long l) { // public -> package-private ++ static ByteBuf writeOld(ByteBuf buf, long l) { // public -> package-private + // Gale end - Velocity - optimized VarLong#write while ((l & -128L) != 0L) { buf.writeByte((int)(l & 127L) | 128); l >>>= 7; diff --git a/src/test/java/net/minecraft/network/VarIntLongTest.java b/src/test/java/net/minecraft/network/VarIntLongTest.java -index f8a1b72d70a47dbc75cdb4f7a536dbb58354661f..2cd1564f0c8d6924dd8b472b19a1925179c09bee 100644 +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 @@ @@ -190,13 +250,13 @@ index f8a1b72d70a47dbc75cdb4f7a536dbb58354661f..2cd1564f0c8d6924dd8b472b19a19251 + } + + @Test -+ public void testWriteVarLongComparedToOriginal() { ++ 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.writeOriginal(buf1, value); ++ VarLong.writeOld(buf1, value); + + // when + buf2.writeVarLong(value);