9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-25 18:09:17 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0198-Remove-iterators-from-Inventory.patch
Dreeam 9a4efaa230 Drop patch that causes performance regression
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.
2025-08-04 19:25:56 +08:00

127 lines
4.7 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Taiyou06 <kaandindar21@gmail.com>
Date: Thu, 13 Feb 2025 01:25:40 +0100
Subject: [PATCH] Remove iterators from Inventory
diff --git a/net/minecraft/world/entity/player/Inventory.java b/net/minecraft/world/entity/player/Inventory.java
index a6bb436dc80daf6901dc027a6011ead4b3ed27e2..b835f8e8caf44e6782b22a76910b26e6f29cb985 100644
--- a/net/minecraft/world/entity/player/Inventory.java
+++ b/net/minecraft/world/entity/player/Inventory.java
@@ -456,13 +456,15 @@ public class Inventory implements Container, Nameable {
}
}
- for (EquipmentSlot equipmentSlot : EQUIPMENT_SLOT_MAPPING.values()) {
+ // Leaf start - Remove iterators from Inventory
+ for (EquipmentSlot equipmentSlot : EQUIPMENT_SLOTS_SORTED_BY_INDEX) {
ItemStack itemStack = this.equipment.get(equipmentSlot);
if (itemStack == stack) {
this.equipment.set(equipmentSlot, ItemStack.EMPTY);
return;
}
}
+ // Leaf end - Remove iterators from Inventory
}
@Override
@@ -515,17 +517,20 @@ public class Inventory implements Container, Nameable {
@Override
public boolean isEmpty() {
- for (ItemStack itemStack : this.items) {
+ // Leaf start - Remove iterators from Inventory
+ for (int i = 0; i < this.items.size(); i++) {
+ ItemStack itemStack = this.items.get(i);
if (!itemStack.isEmpty()) {
return false;
}
}
- for (EquipmentSlot equipmentSlot : EQUIPMENT_SLOT_MAPPING.values()) {
+ for (EquipmentSlot equipmentSlot : EQUIPMENT_SLOTS_SORTED_BY_INDEX) {
if (!this.equipment.get(equipmentSlot).isEmpty()) {
return false;
}
}
+ // Leaf end - Remove iterators from Inventory
return true;
}
@@ -572,31 +577,58 @@ public class Inventory implements Container, Nameable {
}
public boolean contains(ItemStack stack) {
- for (ItemStack itemStack : this) {
+ // Leaf start - Remove iterators from Inventory
+ for (int i = 0; i < this.items.size(); i++) {
+ ItemStack itemStack = this.items.get(i);
if (!itemStack.isEmpty() && ItemStack.isSameItemSameComponents(itemStack, stack)) {
return true;
}
}
+ for (EquipmentSlot equipmentSlot : EQUIPMENT_SLOTS_SORTED_BY_INDEX) {
+ ItemStack itemStack = this.equipment.get(equipmentSlot);
+ if (!itemStack.isEmpty() && ItemStack.isSameItemSameComponents(itemStack, stack)) {
+ return true;
+ }
+ }
+ // Leaf end - Remove iterators from Inventory
return false;
}
public boolean contains(TagKey<Item> tag) {
- for (ItemStack itemStack : this) {
+ // Leaf start - Remove iterators from Inventory
+ for (int i = 0; i < this.items.size(); i++) {
+ ItemStack itemStack = this.items.get(i);
+ if (!itemStack.isEmpty() && itemStack.is(tag)) {
+ return true;
+ }
+ }
+ for (EquipmentSlot equipmentSlot : EQUIPMENT_SLOTS_SORTED_BY_INDEX) {
+ ItemStack itemStack = this.equipment.get(equipmentSlot);
if (!itemStack.isEmpty() && itemStack.is(tag)) {
return true;
}
}
+ // Leaf end - Remove iterators from Inventory
return false;
}
public boolean contains(Predicate<ItemStack> predicate) {
- for (ItemStack itemStack : this) {
+ // Leaf start - Remove iterators from Inventory
+ for (int i = 0; i < this.items.size(); i++) {
+ ItemStack itemStack = this.items.get(i);
if (predicate.test(itemStack)) {
return true;
}
}
+ for (EquipmentSlot equipmentSlot : EQUIPMENT_SLOTS_SORTED_BY_INDEX) {
+ ItemStack itemStack = this.equipment.get(equipmentSlot);
+ if (predicate.test(itemStack)) {
+ return true;
+ }
+ }
+ // Leaf end - Remove iterators from Inventory
return false;
}
@@ -616,9 +648,12 @@ public class Inventory implements Container, Nameable {
}
public void fillStackedContents(StackedItemContents contents) {
- for (ItemStack itemStack : this.items) {
+ // Leaf start - Remove iterators from Inventory
+ for (int i = 0; i < this.items.size(); i++) {
+ ItemStack itemStack = this.items.get(i);
contents.accountSimpleStack(itemStack);
}
+ // Leaf end - Remove iterators from Inventory
}
public ItemStack removeFromSelected(boolean removeStack) {