From 73659318f602c553ef8f96e52f8f8b9129452463 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 19 Oct 2023 16:46:36 +0100 Subject: [PATCH] World: Avoid unnecessary Vector3 usages in neighbour block update scheduling The old code was allocating 6 Vector3s which were all immediately discarded. In addition, we didn't need to take the performance hit of reading Vector3 properties when we could have just passed integers directly. The real performance difference of this is likely to be close to zero, but it's still worth doing. --- src/world/World.php | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/world/World.php b/src/world/World.php index d017fedcd..8c8ba82c8 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -1455,9 +1455,9 @@ class World implements ChunkManager{ $this->scheduledBlockUpdateQueue->insert(new Vector3((int) $pos->x, (int) $pos->y, (int) $pos->z), $delay + $this->server->getTick()); } - private function tryAddToNeighbourUpdateQueue(Vector3 $pos) : void{ - if($this->isInWorld($pos->x, $pos->y, $pos->z)){ - $hash = World::blockHash($pos->x, $pos->y, $pos->z); + private function tryAddToNeighbourUpdateQueue(int $x, int $y, int $z) : void{ + if($this->isInWorld($x, $y, $z)){ + $hash = World::blockHash($x, $y, $z); if(!isset($this->neighbourBlockUpdateQueueIndex[$hash])){ $this->neighbourBlockUpdateQueue->enqueue($hash); $this->neighbourBlockUpdateQueueIndex[$hash] = true; @@ -1465,17 +1465,28 @@ class World implements ChunkManager{ } } + /** + * Identical to {@link World::notifyNeighbourBlockUpdate()}, but without the Vector3 requirement. We don't want or + * need Vector3 in the places where this is called. + * + * TODO: make this the primary method in PM6 + */ + private function internalNotifyNeighbourBlockUpdate(int $x, int $y, int $z) : void{ + $this->tryAddToNeighbourUpdateQueue($x, $y, $z); + foreach(Facing::OFFSET as [$dx, $dy, $dz]){ + $this->tryAddToNeighbourUpdateQueue($x + $dx, $y + $dy, $z + $dz); + } + } + /** * Notify the blocks at and around the position that the block at the position may have changed. * This will cause onNearbyBlockChange() to be called for these blocks. + * TODO: Accept plain integers in PM6 - the Vector3 requirement is an unnecessary inconvenience * * @see Block::onNearbyBlockChange() */ public function notifyNeighbourBlockUpdate(Vector3 $pos) : void{ - $this->tryAddToNeighbourUpdateQueue($pos); - foreach($pos->sides() as $side){ - $this->tryAddToNeighbourUpdateQueue($side); - } + $this->internalNotifyNeighbourBlockUpdate($pos->getFloorX(), $pos->getFloorY(), $pos->getFloorZ()); } /** @@ -1943,7 +1954,7 @@ class World implements ChunkManager{ if($update){ $this->updateAllLight($x, $y, $z); - $this->notifyNeighbourBlockUpdate($pos); + $this->internalNotifyNeighbourBlockUpdate($x, $y, $z); } $this->timings->setBlock->stopTiming();