mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-25 18:09:17 +00:00
Originally vanilla logic is to use stream, and Mojang switched it to Guava's Collections2 since 1.21.4. It is much faster than using stream or manually adding to a new ArrayList. Manually adding to a new ArrayList requires allocating a new object array. However, the Collections2 lazy handles filter condition on iteration, so much better.
88 lines
3.2 KiB
Diff
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;
|
|
}
|
|
|