9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-27 10:59:16 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0200-Slightly-optimized-VarInt-write.patch
Dreeam f5b95a6716 Updated Upstream (Paper)
Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@b0da38c2 Repository details in RuntimeException for MavenLibraryResolver#addRepository (#12939)
PaperMC/Paper@1922be90 Update custom tags (#12183)
PaperMC/Paper@79cf1353 Ignore HopperInventorySearchEvent when it has no listeners (#13009)
PaperMC/Paper@ea014f7a feat: add stuckEntityPoiRetryDelay config (#12949)
PaperMC/Paper@a9e76749 Support for showNotification in PlayerRecipeDiscoverEvent (#12992)
PaperMC/Paper@5622c9dd Expose attribute sentiment (#12974)
PaperMC/Paper@42b653b1 Expose more argument types (#12665)
PaperMC/Paper@52d9a221 [ci/skip] Fix typo in Display javadoc (#13010)
PaperMC/Paper@614e9acf Improve APIs around riptide tridents (#12996)
PaperMC/Paper@51706e5a Fixed DyeItem sheep dye hunk
2025-08-25 15:52:00 -04:00

88 lines
3.2 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Taiyou06 <kaandindar21@gmail.com>
Date: Mon, 24 Feb 2025 21:11:09 +0100
Subject: [PATCH] Slightly optimized VarInt#write
Use switch case instead of if-else for branches. It makes it able to use
tableswitch instruction for better efficiency.
Improves it around 1% to 5%
diff --git a/net/minecraft/network/VarInt.java b/net/minecraft/network/VarInt.java
index 6f8dd31582f0e1d3a71acc7a142c1f4ec0539d9e..4c9095995e8515c61d54f1c056be35a12a138ef3 100644
--- a/net/minecraft/network/VarInt.java
+++ b/net/minecraft/network/VarInt.java
@@ -52,35 +52,44 @@ public class VarInt {
public static ByteBuf write(ByteBuf buffer, int value) {
// Gale start - Velocity - optimized VarInt#write
- if ((value & 0xFFFFFF80) == 0) {
- buffer.writeByte(value);
- } else if ((value & 0xFFFFC000) == 0) {
- int w = (value & 0x7F) << 8
- | (value >>> 7)
- | 0x00008000;
- buffer.writeShort(w);
- } else if ((value & 0xFFE00000) == 0) {
- int w = (value & 0x7F) << 16
- | (value & 0x3F80) << 1
- | (value >>> 14)
- | 0x00808000;
- buffer.writeMedium(w);
- } else if ((value & 0xF0000000) == 0) {
- int w = (value & 0x7F) << 24
- | ((value & 0x3F80) << 9)
- | (value & 0x1FC000) >> 6
- | (value >>> 21)
- | 0x80808000;
- buffer.writeInt(w);
- } else {
- int w = (value & 0x7F) << 24
- | (value & 0x3F80) << 9
- | (value & 0x1FC000) >> 6
- | ((value >>> 21) & 0x7F)
- | 0x80808080;
- buffer.writeInt(w);
- buffer.writeByte(value >>> 28);
+ // Leaf start - Slightly optimized VarInt#write
+ int bytesNeeded = getByteSize(value);
+
+ switch (bytesNeeded) {
+ case 1:
+ buffer.writeByte(value);
+ break;
+ case 2:
+ int w2 = ((value & 0x7F) << 8) | (value >>> 7) | 0x00008000;
+ buffer.writeShort(w2);
+ break;
+ case 3:
+ int w3 = (value & 0x7F) << 16
+ | (value & 0x3F80) << 1
+ | (value >>> 14)
+ | 0x00808000;
+ buffer.writeMedium(w3);
+ break;
+ case 4:
+ int w4 = (value & 0x7F) << 24
+ | ((value & 0x3F80) << 9)
+ | (value & 0x1FC000) >> 6
+ | (value >>> 21)
+ | 0x80808000;
+ buffer.writeInt(w4);
+ break;
+ case 5:
+ int w5 = (value & 0x7F) << 24
+ | (value & 0x3F80) << 9
+ | (value & 0x1FC000) >> 6
+ | ((value >>> 21) & 0x7F)
+ | 0x80808080;
+ buffer.writeInt(w5);
+ buffer.writeByte(value >>> 28);
+ break;
}
+ // Leaf end - Slightly optimized VarInt#write
+
return buffer;
}