9
0
mirror of https://github.com/Dreeam-qwq/Gale.git synced 2025-12-22 16:29:26 +00:00

Pre-compute VarInt and VarLong sizes

This commit is contained in:
Martijn Muijsers
2023-08-21 21:50:48 +02:00
parent f529593302
commit 39a3873cd2
8 changed files with 69 additions and 2 deletions

View File

@@ -29,7 +29,7 @@ index 2868dab7b100d9c325b0e5056f86660d631dec4b..2acad4c3fd58178b0f8b22bdb04eeeeb
}
diff --git a/src/main/java/org/galemc/gale/version/GaleSemanticVersion.java b/src/main/java/org/galemc/gale/version/GaleSemanticVersion.java
new file mode 100644
index 0000000000000000000000000000000000000000..33df2d291c53c31f7594bca1ca5c92d80c9047fc
index 0000000000000000000000000000000000000000..86f7e352f47ee808f4a8ed00f5773aeb4eab6051
--- /dev/null
+++ b/src/main/java/org/galemc/gale/version/GaleSemanticVersion.java
@@ -0,0 +1,37 @@
@@ -57,7 +57,7 @@ index 0000000000000000000000000000000000000000..33df2d291c53c31f7594bca1ca5c92d8
+ * The <code>patch</code> version is incremented for small changes that do not affect the goal of any feature,
+ * such as bug fixes, performance improvements or changes in wording.
+ */
+ public static final @NotNull String version = "0.5.7";
+ public static final @NotNull String version = "0.5.8";
+
+ /**
+ * The "<code>major.minor</code>" portion of the {@link #version}.

View File

@@ -0,0 +1,67 @@
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 VarInt and 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/main/java/net/minecraft/network/FriendlyByteBuf.java b/src/main/java/net/minecraft/network/FriendlyByteBuf.java
index 9938bb90bef84cf784f9a1ceb02a1a45aa8b48a1..ec8203e0f69d976fc35fd2c031b9ecd5565c9591 100644
--- a/src/main/java/net/minecraft/network/FriendlyByteBuf.java
+++ b/src/main/java/net/minecraft/network/FriendlyByteBuf.java
@@ -102,24 +102,27 @@ public class FriendlyByteBuf extends ByteBuf {
this.source = parent;
}
- public static int getVarIntSize(int value) {
- for (int j = 1; j < 5; ++j) {
- if ((value & -1 << j * 7) == 0) {
- return j;
- }
+ // Gale start - Velocity - pre-compute VarInt and VarLong sizes
+ private static final int[] VARINT_EXACT_BYTE_LENGTHS = new int[33];
+ private static final int[] VARLONG_EXACT_BYTE_LENGTHS = new int[65];
+ static {
+ for (int i = 0; i < 32; ++i) {
+ VARINT_EXACT_BYTE_LENGTHS[i] = (32 - i + 6) / 7;
}
+ VARINT_EXACT_BYTE_LENGTHS[32] = 1; // Special case for the number 0.
+ for (int i = 0; i < 64; ++i) {
+ VARLONG_EXACT_BYTE_LENGTHS[i] = (64 - i + 6) / 7;
+ }
+ VARLONG_EXACT_BYTE_LENGTHS[32] = 1; // Special case for the number 0.
+ }
+ // Gale end - Velocity - pre-compute VarInt and VarLong sizes
- return 5;
+ public static int getVarIntSize(int value) {
+ return VARINT_EXACT_BYTE_LENGTHS[Integer.numberOfLeadingZeros(value)]; // Gale - Velocity - pre-compute VarInt and VarLong sizes
}
public static int getVarLongSize(long value) {
- for (int j = 1; j < 10; ++j) {
- if ((value & -1L << j * 7) == 0L) {
- return j;
- }
- }
-
- return 10;
+ return VARLONG_EXACT_BYTE_LENGTHS[Long.numberOfLeadingZeros(value)]; // Gale - Velocity - pre-compute VarInt and VarLong sizes
}
/** @deprecated */