Files
MiraiMC/patches/server/0012-Optimize-inventory-API-item-handling.patch
Etil 6d4e96dd1a Add Pufferfish patches
It includes async mob spawning (up to 15% improvement)
2021-11-13 16:53:43 +01:00

66 lines
3.1 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Etil <81570777+etil2jz@users.noreply.github.com>
Date: Fri, 12 Nov 2021 20:52:54 +0100
Subject: [PATCH] Optimize inventory API item handling
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java
index 396a4ae3d5a829eda78ef98561333aea300aa722..97b1c48979075ee8a04262c654c6b2130511db79 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java
@@ -273,11 +273,13 @@ public class CraftInventory implements Inventory {
}
private int firstPartial(ItemStack item) {
- ItemStack[] inventory = this.getStorageContents();
- ItemStack filteredItem = CraftItemStack.asCraftCopy(item);
if (item == null) {
return -1;
}
+ // Origami start - Optimize inventory API (moved down from before null check)
+ ItemStack[] inventory = this.getStorageContents();
+ ItemStack filteredItem = CraftItemStack.asCraftCopy(item);
+ // Origami end
for (int i = 0; i < inventory.length; i++) {
ItemStack cItem = inventory[i];
if (cItem != null && cItem.getAmount() < cItem.getMaxStackSize() && cItem.isSimilar(filteredItem)) {
@@ -295,10 +297,11 @@ public class CraftInventory implements Inventory {
/* TODO: some optimization
* - Create a 'firstPartial' with a 'fromIndex'
* - Record the lastPartial per Material
- * - Cache firstEmpty result
+ * - Cache firstEmpty result // Implemented in Origami
*/
- for (int i = 0; i < items.length; i++) {
+ int firstFree = -2; // Origami - Cache firstEmpty result
+ for (int i = 0; i < items.length; i++) {
ItemStack item = items[i];
while (true) {
// Do we already have a stack of it?
@@ -307,7 +310,11 @@ public class CraftInventory implements Inventory {
// Drat! no partial stack
if (firstPartial == -1) {
// Find a free spot!
- int firstFree = this.firstEmpty();
+ // Origami start - Cache firstEmpty result
+ if (firstFree == -2) {
+ firstFree = this.firstEmpty();
+ }
+ // Origami end
if (firstFree == -1) {
// No space at all!
@@ -320,9 +327,11 @@ public class CraftInventory implements Inventory {
stack.setAmount(this.getMaxItemStack());
this.setItem(firstFree, stack);
item.setAmount(item.getAmount() - this.getMaxItemStack());
+ firstFree = -2; // Origami - Cache firstEmpty result
} else {
// Just store it
this.setItem(firstFree, item);
+ firstFree = -2; // Origami - Cache firstEmpty result
break;
}
}