From 7e20385bdbafb5f6ae8ef5e4c476a69151ceac3d Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 27 Mar 2018 10:13:28 +0100 Subject: [PATCH] BaseInventory: improved performance of getContents() this old code is extremely inefficient. This showed up distinctly in my crafting bruteforce benchmarks, where I discovered that getContents() accounted for the vast majority of the time taken to match shaped recipes. --- src/pocketmine/inventory/BaseInventory.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/pocketmine/inventory/BaseInventory.php b/src/pocketmine/inventory/BaseInventory.php index cb1bb478d..6905bad75 100644 --- a/src/pocketmine/inventory/BaseInventory.php +++ b/src/pocketmine/inventory/BaseInventory.php @@ -99,10 +99,13 @@ abstract class BaseInventory implements Inventory{ */ public function getContents(bool $includeEmpty = false) : array{ $contents = []; - for($i = 0, $size = $this->getSize(); $i < $size; ++$i){ - $item = $this->getItem($i); - if($includeEmpty or !$item->isNull()){ - $contents[$i] = $item; + $air = null; + + foreach($this->slots as $i => $slot){ + if($slot !== null){ + $contents[$i] = clone $slot; + }elseif($includeEmpty){ + $contents[$i] = $air ?? ($air = ItemFactory::get(Item::AIR, 0, 0)); } }