mirror of
https://github.com/Dreeam-qwq/Gale.git
synced 2025-12-21 15:59:28 +00:00
Optimize FriendlyByteBuf#writeVarInt and FriendlyByteBuf#writeVarLong
This commit is contained in:
@@ -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..605c4946a40fa65fee407a6041681af7158fb4dc
|
||||
index 0000000000000000000000000000000000000000..b226b2aea6b1c002e3b9242e3d20c08a50a7ab80
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/org/galemc/gale/version/GaleSemanticVersion.java
|
||||
@@ -0,0 +1,37 @@
|
||||
@@ -57,7 +57,7 @@ index 0000000000000000000000000000000000000000..605c4946a40fa65fee407a6041681af7
|
||||
+ * 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.6.0";
|
||||
+ public static final @NotNull String version = "0.6.1";
|
||||
+
|
||||
+ /**
|
||||
+ * The "<code>major.minor</code>" portion of the {@link #version}.
|
||||
|
||||
@@ -21,47 +21,213 @@ 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/FriendlyByteBuf.java b/src/main/java/net/minecraft/network/FriendlyByteBuf.java
|
||||
index 9938bb90bef84cf784f9a1ceb02a1a45aa8b48a1..ec8203e0f69d976fc35fd2c031b9ecd5565c9591 100644
|
||||
index 9938bb90bef84cf784f9a1ceb02a1a45aa8b48a1..c37d1a3cae9063a8cbc84b6420a7f89d650a874e 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 {
|
||||
@@ -102,7 +102,28 @@ 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.
|
||||
+ VARLONG_EXACT_BYTE_LENGTHS[64] = 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 getVarIntSize(int value) {
|
||||
+ // Gale start - Velocity - pre-compute VarInt and VarLong sizes
|
||||
+ return VARINT_EXACT_BYTE_LENGTHS[Integer.numberOfLeadingZeros(value)];
|
||||
+ }
|
||||
+
|
||||
+ static int getVarIntSizeOriginal(int value) { // public -> package-private
|
||||
+ // Gale end - Velocity - pre-compute VarInt and VarLong sizes
|
||||
for (int j = 1; j < 5; ++j) {
|
||||
if ((value & -1 << j * 7) == 0) {
|
||||
return j;
|
||||
@@ -113,6 +134,12 @@ public class FriendlyByteBuf extends ByteBuf {
|
||||
}
|
||||
|
||||
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 */
|
||||
+ // Gale start - Velocity - pre-compute VarInt and VarLong sizes
|
||||
+ return VARLONG_EXACT_BYTE_LENGTHS[Long.numberOfLeadingZeros(value)];
|
||||
+ }
|
||||
+
|
||||
+ static int getVarLongSizeOriginal(long value) { // public -> package-private
|
||||
+ // Gale end - Velocity - pre-compute VarInt and VarLong sizes
|
||||
for (int j = 1; j < 10; ++j) {
|
||||
if ((value & -1L << j * 7) == 0L) {
|
||||
return j;
|
||||
diff --git a/src/test/java/net/minecraft/network/FriendlyByteBufTest.java b/src/test/java/net/minecraft/network/FriendlyByteBufTest.java
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..e529888932a88e4876a3287ac7ebe8ad2bf77513
|
||||
--- /dev/null
|
||||
+++ b/src/test/java/net/minecraft/network/FriendlyByteBufTest.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.Assert;
|
||||
+import org.junit.Test;
|
||||
+
|
||||
+public class FriendlyByteBufTest {
|
||||
+
|
||||
+ 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 testGetVarIntSizeComparedToOriginal() {
|
||||
+ integerCases.forEach(value -> {
|
||||
+ // given
|
||||
+ int originalSize = FriendlyByteBuf.getVarIntSizeOriginal(value);
|
||||
+
|
||||
+ // when
|
||||
+ int size = FriendlyByteBuf.getVarIntSize(value);
|
||||
+
|
||||
+ // then
|
||||
+ Assert.assertEquals("Optimized size (" + size + ") is not equal to original size (" + originalSize + ") for test case value " + value + " (binary: " + padStringWithLeadingZeros(Integer.toBinaryString(value), 32) + ")", originalSize, size);
|
||||
+ });
|
||||
+ }
|
||||
+
|
||||
+ @Test
|
||||
+ public void testGetVarLongSizeComparedToOriginal() {
|
||||
+ longCases.forEach(value -> {
|
||||
+ // given
|
||||
+ int originalSize = FriendlyByteBuf.getVarLongSizeOriginal(value);
|
||||
+
|
||||
+ // when
|
||||
+ int size = FriendlyByteBuf.getVarLongSize(value);
|
||||
+
|
||||
+ // then
|
||||
+ Assert.assertEquals("Optimized size (" + size + ") is not equal to original size (" + originalSize + ") for test case value " + value + " (binary: " + padStringWithLeadingZeros(Long.toBinaryString(value), 64) + ")", originalSize, size);
|
||||
+ });
|
||||
+ }
|
||||
+ // Gale end - Velocity - pre-compute VarInt and VarLong sizes
|
||||
+
|
||||
+}
|
||||
|
||||
@@ -0,0 +1,256 @@
|
||||
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 FriendlyByteBuf#writeVarInt and
|
||||
FriendlyByteBuf#writeVarLong
|
||||
|
||||
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 c37d1a3cae9063a8cbc84b6420a7f89d650a874e..76d92f56a9deab824dcb7ca652c5fcd287f0cbfd 100644
|
||||
--- a/src/main/java/net/minecraft/network/FriendlyByteBuf.java
|
||||
+++ b/src/main/java/net/minecraft/network/FriendlyByteBuf.java
|
||||
@@ -640,6 +640,41 @@ public class FriendlyByteBuf extends ByteBuf {
|
||||
}
|
||||
|
||||
public FriendlyByteBuf writeVarInt(int value) {
|
||||
+ // Gale start - Velocity - optimized FriendlyByteBuf#writeVarInt
|
||||
+ if ((value & 0xFFFFFF80) == 0) {
|
||||
+ this.writeByte(value);
|
||||
+ } else if ((value & 0xFFFFC000) == 0) {
|
||||
+ int w = (value & 0x7F) << 8
|
||||
+ | (value >>> 7)
|
||||
+ | 0x00008000;
|
||||
+ this.writeShort(w);
|
||||
+ } else if ((value & 0xFFE00000) == 0) {
|
||||
+ int w = (value & 0x7F) << 16
|
||||
+ | (value & 0x3F80) << 1
|
||||
+ | (value >>> 14)
|
||||
+ | 0x00808000;
|
||||
+ this.writeMedium(w);
|
||||
+ } else if ((value & 0xF0000000) == 0) {
|
||||
+ int w = (value & 0x7F) << 24
|
||||
+ | ((value & 0x3F80) << 9)
|
||||
+ | (value & 0x1FC000) >> 6
|
||||
+ | (value >>> 21)
|
||||
+ | 0x80808000;
|
||||
+ this.writeInt(w);
|
||||
+ } else {
|
||||
+ int w = (value & 0x7F) << 24
|
||||
+ | (value & 0x3F80) << 9
|
||||
+ | (value & 0x1FC000) >> 6
|
||||
+ | ((value >>> 21) & 0x7F)
|
||||
+ | 0x80808080;
|
||||
+ this.writeInt(w);
|
||||
+ this.writeByte(value >>> 28);
|
||||
+ }
|
||||
+ return this;
|
||||
+ }
|
||||
+
|
||||
+ FriendlyByteBuf writeVarIntOriginal(int value) { // public -> package-private
|
||||
+ // Gale end - Velocity - optimized FriendlyByteBuf#writeVarInt
|
||||
while ((value & -128) != 0) {
|
||||
this.writeByte(value & 127 | 128);
|
||||
value >>>= 7;
|
||||
@@ -650,6 +685,127 @@ public class FriendlyByteBuf extends ByteBuf {
|
||||
}
|
||||
|
||||
public FriendlyByteBuf writeVarLong(long value) {
|
||||
+ // Gale start - Velocity - optimized FriendlyByteBuf#writeVarLong
|
||||
+ if ((value & 0xFFFFFFFFFFFFFF80L) == 0) {
|
||||
+ this.writeByte((int) value);
|
||||
+ } else if (value < 0) {
|
||||
+ // The case of writing arbitrary longs is common
|
||||
+ // Here, the number is negative, which has probability 1/2 for arbitrary numbers
|
||||
+ int least7bits = (int) (value & 0xFFFFFFFL);
|
||||
+ int w = (least7bits & 0x7F) << 24
|
||||
+ | (least7bits & 0x3F80) << 9
|
||||
+ | (least7bits & 0x1FC000) >> 6
|
||||
+ | ((least7bits >>> 21) & 0x7F)
|
||||
+ | 0x80808080;
|
||||
+ long nonLeast7Bits = value >>> 28;
|
||||
+ int secondLeast7bits = (int) (nonLeast7Bits & 0xFFFFFFFL);
|
||||
+ int w2 = (secondLeast7bits & 0x7F) << 24
|
||||
+ | ((secondLeast7bits & 0x3F80) << 9)
|
||||
+ | (secondLeast7bits & 0x1FC000) >> 6
|
||||
+ | (secondLeast7bits >>> 21)
|
||||
+ | 0x80808080;
|
||||
+ int thirdLeast7Bits = (int) (nonLeast7Bits >>> 28);
|
||||
+ int w3 = (thirdLeast7Bits & 0x7F) << 8
|
||||
+ | (thirdLeast7Bits >>> 7)
|
||||
+ | 0x00008000;
|
||||
+ this.writeInt(w);
|
||||
+ this.writeInt(w2);
|
||||
+ this.writeShort(w3);
|
||||
+ } else if ((value & 0xFFFFFFFFFFFFC000L) == 0) {
|
||||
+ int least7bits = (int) value;
|
||||
+ int w = (least7bits & 0x7F) << 8
|
||||
+ | (least7bits >>> 7)
|
||||
+ | 0x00008000;
|
||||
+ this.writeShort(w);
|
||||
+ } else if ((value & 0xFFFFFFFFFFE00000L) == 0) {
|
||||
+ int least7bits = (int) value;
|
||||
+ int w = (least7bits & 0x7F) << 16
|
||||
+ | (least7bits & 0x3F80) << 1
|
||||
+ | (least7bits >>> 14)
|
||||
+ | 0x00808000;
|
||||
+ this.writeMedium(w);
|
||||
+ } else if ((value & 0xFFFFFFFFF0000000L) == 0) {
|
||||
+ int least7bits = (int) value;
|
||||
+ int w = (least7bits & 0x7F) << 24
|
||||
+ | ((least7bits & 0x3F80) << 9)
|
||||
+ | (least7bits & 0x1FC000) >> 6
|
||||
+ | (least7bits >>> 21)
|
||||
+ | 0x80808000;
|
||||
+ this.writeInt(w);
|
||||
+ } else if ((value & 0xFFFFFFF800000000L) == 0) {
|
||||
+ int least7bits = (int) (value & 0xFFFFFFFL);
|
||||
+ int w = (least7bits & 0x7F) << 24
|
||||
+ | (least7bits & 0x3F80) << 9
|
||||
+ | (least7bits & 0x1FC000) >> 6
|
||||
+ | ((least7bits >>> 21) & 0x7F)
|
||||
+ | 0x80808080;
|
||||
+ this.writeInt(w);
|
||||
+ this.writeByte((int) (value >>> 28));
|
||||
+ } else if ((value & 0xFFFFFC0000000000L) == 0) {
|
||||
+ int least7bits = (int) (value & 0xFFFFFFFL);
|
||||
+ int w = (least7bits & 0x7F) << 24
|
||||
+ | (least7bits & 0x3F80) << 9
|
||||
+ | (least7bits & 0x1FC000) >> 6
|
||||
+ | ((least7bits >>> 21) & 0x7F)
|
||||
+ | 0x80808080;
|
||||
+ int secondLeast7bits = (int) (value >>> 28);
|
||||
+ int w2 = (secondLeast7bits & 0x7F) << 8
|
||||
+ | (secondLeast7bits >>> 7)
|
||||
+ | 0x00008000;
|
||||
+ this.writeInt(w);
|
||||
+ this.writeShort(w2);
|
||||
+ } else if ((value & 0xFFFE000000000000L) == 0) {
|
||||
+ int least7bits = (int) (value & 0xFFFFFFFL);
|
||||
+ int w = (least7bits & 0x7F) << 24
|
||||
+ | (least7bits & 0x3F80) << 9
|
||||
+ | (least7bits & 0x1FC000) >> 6
|
||||
+ | ((least7bits >>> 21) & 0x7F)
|
||||
+ | 0x80808080;
|
||||
+ int secondLeast7bits = (int) (value >>> 28);
|
||||
+ int w2 = (secondLeast7bits & 0x7F) << 16
|
||||
+ | (secondLeast7bits & 0x3F80) << 1
|
||||
+ | (secondLeast7bits >>> 14)
|
||||
+ | 0x00808000;
|
||||
+ this.writeInt(w);
|
||||
+ this.writeMedium(w2);
|
||||
+ } else if ((value & 0xFF00000000000000L) == 0) {
|
||||
+ int least7bits = (int) (value & 0xFFFFFFFL);
|
||||
+ int w = (least7bits & 0x7F) << 24
|
||||
+ | (least7bits & 0x3F80) << 9
|
||||
+ | (least7bits & 0x1FC000) >> 6
|
||||
+ | ((least7bits >>> 21) & 0x7F)
|
||||
+ | 0x80808080;
|
||||
+ int secondLeast7bits = (int) (value >>> 28);
|
||||
+ int w2 = (secondLeast7bits & 0x7F) << 24
|
||||
+ | ((secondLeast7bits & 0x3F80) << 9)
|
||||
+ | (secondLeast7bits & 0x1FC000) >> 6
|
||||
+ | (secondLeast7bits >>> 21)
|
||||
+ | 0x80808000;
|
||||
+ this.writeInt(w);
|
||||
+ this.writeInt(w2);
|
||||
+ } else {
|
||||
+ int least7bits = (int) (value & 0xFFFFFFFL);
|
||||
+ int w = (least7bits & 0x7F) << 24
|
||||
+ | (least7bits & 0x3F80) << 9
|
||||
+ | (least7bits & 0x1FC000) >> 6
|
||||
+ | ((least7bits >>> 21) & 0x7F)
|
||||
+ | 0x80808080;
|
||||
+ long nonLeast7Bits = value >>> 28;
|
||||
+ int secondLeast7bits = (int) (nonLeast7Bits & 0xFFFFFFFL);
|
||||
+ int w2 = (secondLeast7bits & 0x7F) << 24
|
||||
+ | ((secondLeast7bits & 0x3F80) << 9)
|
||||
+ | (secondLeast7bits & 0x1FC000) >> 6
|
||||
+ | (secondLeast7bits >>> 21)
|
||||
+ | 0x80808080;
|
||||
+ this.writeInt(w);
|
||||
+ this.writeInt(w2);
|
||||
+ this.writeByte((int) (nonLeast7Bits >>> 28));
|
||||
+ }
|
||||
+ return this;
|
||||
+ }
|
||||
+
|
||||
+ FriendlyByteBuf writeVarLongOriginal(long value) { // public -> package-private
|
||||
+ // Gale end - Velocity - optimized FriendlyByteBuf#writeVarLong
|
||||
while ((value & -128L) != 0L) {
|
||||
this.writeByte((int) (value & 127L) | 128);
|
||||
value >>>= 7;
|
||||
diff --git a/src/test/java/net/minecraft/network/FriendlyByteBufTest.java b/src/test/java/net/minecraft/network/FriendlyByteBufTest.java
|
||||
index e529888932a88e4876a3287ac7ebe8ad2bf77513..f8bcfc6da906dc168b326b71d7c534ea21b90b7b 100644
|
||||
--- a/src/test/java/net/minecraft/network/FriendlyByteBufTest.java
|
||||
+++ b/src/test/java/net/minecraft/network/FriendlyByteBufTest.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 FriendlyByteBufTest {
|
||||
}
|
||||
// Gale end - Velocity - pre-compute VarInt and VarLong sizes
|
||||
|
||||
+ // Gale - Velocity - optimized FriendlyByteBuf#writeVarInt and FriendlyByteBuf#writeVarLong
|
||||
+ @Test
|
||||
+ public void testWriteVarIntComparedToOriginal() {
|
||||
+ integerCases.forEach(value -> {
|
||||
+ // given
|
||||
+ int capacity = 5;
|
||||
+ FriendlyByteBuf buf1 = new FriendlyByteBuf(Unpooled.buffer(capacity));
|
||||
+ FriendlyByteBuf buf2 = new FriendlyByteBuf(Unpooled.buffer(capacity));
|
||||
+ buf1.writeVarIntOriginal(value);
|
||||
+
|
||||
+ // when
|
||||
+ buf2.writeVarInt(value);
|
||||
+
|
||||
+ // then
|
||||
+ Assert.assertEquals("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) + ")", buf1.writerIndex(), buf2.writerIndex());
|
||||
+ for (int i = 0; i < capacity; i++) {
|
||||
+ Assert.assertEquals("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) + ")", buf1.getByte(i), buf2.getByte(i));
|
||||
+ }
|
||||
+ });
|
||||
+ }
|
||||
+
|
||||
+ @Test
|
||||
+ public void testWriteVarLongComparedToOriginal() {
|
||||
+ longCases.forEach(value -> {
|
||||
+ // given
|
||||
+ int capacity = 10;
|
||||
+ FriendlyByteBuf buf1 = new FriendlyByteBuf(Unpooled.buffer(capacity));
|
||||
+ FriendlyByteBuf buf2 = new FriendlyByteBuf(Unpooled.buffer(capacity));
|
||||
+ buf1.writeVarLongOriginal(value);
|
||||
+
|
||||
+ // when
|
||||
+ buf2.writeVarLong(value);
|
||||
+
|
||||
+ // then
|
||||
+ Assert.assertEquals("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) + ")", buf1.writerIndex(), buf2.writerIndex());
|
||||
+ for (int i = 0; i < capacity; i++) {
|
||||
+ Assert.assertEquals("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) + ")", buf1.getByte(i), buf2.getByte(i));
|
||||
+ }
|
||||
+ });
|
||||
+ }
|
||||
+ // Gale end - Velocity - optimized FriendlyByteBuf#writeVarInt and FriendlyByteBuf#writeVarLong
|
||||
+
|
||||
}
|
||||
Reference in New Issue
Block a user