From c91c8c2f9e915e3ba67216cf77c76cb59de9bda8 Mon Sep 17 00:00:00 2001 From: Dylan T Date: Thu, 3 Aug 2023 14:51:51 +0100 Subject: [PATCH] Improving performance of small moving entities (e.g. dropped items) (#5954) * World: cache block AABBs directly in the world this removes some indirection when fetching the AABBs, and also allows the AABB cache to live longer than the block cache. In local testing this showed a 10-20% performance improvement, but it was difficult to properly measure. * World: eliminate padding block checks in getCollisionBoxes() this substantially improves the function's performance for small entities. The padding of 1 block in each direction was previously necessary to account for blocks like fences, which might have an AABB larger than the cell containing them. However, by tracking this information in the collisionBoxCache directly, we can avoid the need to check this at the expense of slightly more complex code. This reduces the number of blocks checked for a moving item entity from 27-64 all the way down to 1-8, which is a major improvement. Locally, this change allowed me to simulate 2100 item entities without lag, compared with 1500 on the previous commit. --- src/world/World.php | 79 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 8 deletions(-) diff --git a/src/world/World.php b/src/world/World.php index f556d2e1f..4f266022f 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -200,6 +200,11 @@ class World implements ChunkManager{ * @phpstan-var array> */ private array $blockCache = []; + /** + * @var AxisAlignedBB[][][] chunkHash => [relativeBlockHash => AxisAlignedBB[]] + * @phpstan-var array>> + */ + private array $blockCollisionBoxCache = []; private int $sendTimeTicker = 0; @@ -646,6 +651,7 @@ class World implements ChunkManager{ $this->provider->close(); $this->blockCache = []; + $this->blockCollisionBoxCache = []; $this->unloaded = true; } @@ -1117,6 +1123,7 @@ class World implements ChunkManager{ public function clearCache(bool $force = false) : void{ if($force){ $this->blockCache = []; + $this->blockCollisionBoxCache = []; }else{ $count = 0; foreach($this->blockCache as $list){ @@ -1126,6 +1133,16 @@ class World implements ChunkManager{ break; } } + + $count = 0; + foreach($this->blockCollisionBoxCache as $list){ + $count += count($list); + if($count > 2048){ + //TODO: Is this really the best logic? + $this->blockCollisionBoxCache = []; + break; + } + } } } @@ -1491,25 +1508,53 @@ class World implements ChunkManager{ return $collides; } + /** + * Returns a list of all block AABBs which overlap the full block area at the given coordinates. + * This checks a padding of 1 block around the coordinates to account for oversized AABBs of blocks like fences. + * Larger AABBs (>= 2 blocks on any axis) are not accounted for. + * + * @return AxisAlignedBB[] + */ + private function getCollisionBoxesForCell(int $x, int $y, int $z) : array{ + $block = $this->getBlockAt($x, $y, $z); + $boxes = $block->getCollisionBoxes(); + + $cellBB = AxisAlignedBB::one()->offset($x, $y, $z); + foreach(Facing::ALL as $facing){ + $extraBoxes = $block->getSide($facing)->getCollisionBoxes(); + foreach($extraBoxes as $extraBox){ + if($extraBox->intersectsWith($cellBB)){ + $boxes[] = $extraBox; + } + } + } + + return $boxes; + } + /** * @return AxisAlignedBB[] * @phpstan-return list */ public function getCollisionBoxes(Entity $entity, AxisAlignedBB $bb, bool $entities = true) : array{ - $minX = (int) floor($bb->minX - 1); - $minY = (int) floor($bb->minY - 1); - $minZ = (int) floor($bb->minZ - 1); - $maxX = (int) floor($bb->maxX + 1); - $maxY = (int) floor($bb->maxY + 1); - $maxZ = (int) floor($bb->maxZ + 1); + $minX = (int) floor($bb->minX); + $minY = (int) floor($bb->minY); + $minZ = (int) floor($bb->minZ); + $maxX = (int) floor($bb->maxX); + $maxY = (int) floor($bb->maxY); + $maxZ = (int) floor($bb->maxZ); $collides = []; for($z = $minZ; $z <= $maxZ; ++$z){ for($x = $minX; $x <= $maxX; ++$x){ + $chunkPosHash = World::chunkHash($x >> Chunk::COORD_BIT_SIZE, $z >> Chunk::COORD_BIT_SIZE); for($y = $minY; $y <= $maxY; ++$y){ - $block = $this->getBlockAt($x, $y, $z); - foreach($block->getCollisionBoxes() as $blockBB){ + $relativeBlockHash = World::chunkBlockHash($x, $y, $z); + + $boxes = $this->blockCollisionBoxCache[$chunkPosHash][$relativeBlockHash] ??= $this->getCollisionBoxesForCell($x, $y, $z); + + foreach($boxes as $blockBB){ if($blockBB->intersectsWith($bb)){ $collides[] = $blockBB; } @@ -1858,6 +1903,21 @@ class World implements ChunkManager{ $relativeBlockHash = World::chunkBlockHash($x, $y, $z); unset($this->blockCache[$chunkHash][$relativeBlockHash]); + unset($this->blockCollisionBoxCache[$chunkHash][$relativeBlockHash]); + //blocks like fences have collision boxes that reach into neighbouring blocks, so we need to invalidate the + //caches for those blocks as well + foreach([ + [0, 0, 1], + [0, 0, -1], + [0, 1, 0], + [0, -1, 0], + [1, 0, 0], + [-1, 0, 0] + ] as [$offsetX, $offsetY, $offsetZ]){ + $sideChunkPosHash = World::chunkHash(($x + $offsetX) >> Chunk::COORD_BIT_SIZE, ($z + $offsetZ) >> Chunk::COORD_BIT_SIZE); + $sideChunkBlockHash = World::chunkBlockHash($x + $offsetX, $y + $offsetY, $z + $offsetZ); + unset($this->blockCollisionBoxCache[$sideChunkPosHash][$sideChunkBlockHash]); + } if(!isset($this->changedBlocks[$chunkHash])){ $this->changedBlocks[$chunkHash] = []; @@ -2454,6 +2514,7 @@ class World implements ChunkManager{ $this->chunks[$chunkHash] = $chunk; unset($this->blockCache[$chunkHash]); + unset($this->blockCollisionBoxCache[$chunkHash]); unset($this->changedBlocks[$chunkHash]); $chunk->setTerrainDirty(); $this->markTickingChunkForRecheck($chunkX, $chunkZ); //this replacement chunk may not meet the conditions for ticking @@ -2733,6 +2794,7 @@ class World implements ChunkManager{ } $this->chunks[$chunkHash] = $chunk; unset($this->blockCache[$chunkHash]); + unset($this->blockCollisionBoxCache[$chunkHash]); $this->initChunk($x, $z, $chunkData); @@ -2887,6 +2949,7 @@ class World implements ChunkManager{ unset($this->chunks[$chunkHash]); unset($this->blockCache[$chunkHash]); + unset($this->blockCollisionBoxCache[$chunkHash]); unset($this->changedBlocks[$chunkHash]); unset($this->registeredTickingChunks[$chunkHash]); $this->markTickingChunkForRecheck($x, $z);