From eb53b795d53164c6a29a98af2726835317005df1 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 28 Jul 2023 16:06:29 +0100 Subject: [PATCH] ItemEntity: fixed O(n^2) performance issue when many of the same unstackable item are in the same place this produced a 40% performance improvement in a simulation with 800 item entities. If the items were all different, then this would still be a problem. However, many of the same unstackable items occupying the same space is a problem for SkyBlock farms, so this should improve performance for SkyBlock quite a bit. --- src/entity/object/ItemEntity.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index d28d70773d9..233b5411226 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -131,7 +131,7 @@ protected function entityBaseTick(int $tickDiff = 1) : bool{ } } - if($this->hasMovementUpdate() && $this->despawnDelay % self::MERGE_CHECK_PERIOD === 0){ + if($this->hasMovementUpdate() && $this->isMergeCandidate() && $this->despawnDelay % self::MERGE_CHECK_PERIOD === 0){ $mergeable = [$this]; //in case the merge target ends up not being this $mergeTarget = $this; foreach($this->getWorld()->getNearbyEntities($this->boundingBox->expandedCopy(0.5, 0.5, 0.5), $this) as $entity){ @@ -173,12 +173,19 @@ protected function entityBaseTick(int $tickDiff = 1) : bool{ } } + private function isMergeCandidate() : bool{ + return $this->pickupDelay !== self::NEVER_DESPAWN && $this->item->getCount() < $this->item->getMaxStackSize(); + } + /** * Returns whether this item entity can merge with the given one. */ public function isMergeable(ItemEntity $entity) : bool{ + if(!$this->isMergeCandidate() || !$entity->isMergeCandidate()){ + return false; + } $item = $entity->item; - return $entity !== $this && $entity->pickupDelay !== self::NEVER_DESPAWN && $item->canStackWith($this->item) && $item->getCount() + $this->item->getCount() <= $item->getMaxStackSize(); + return $entity !== $this && $item->canStackWith($this->item) && $item->getCount() + $this->item->getCount() <= $item->getMaxStackSize(); } /**