From 6da2bd4bb388651c807d77815a0db53384a3d9aa Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 27 Jun 2019 16:53:24 +0100 Subject: [PATCH] SimpleChunkManager: implement last-access performance optimization this improves performance of writing blocks by ~30%. --- src/pocketmine/world/SimpleChunkManager.php | 29 ++++++++++++--------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/pocketmine/world/SimpleChunkManager.php b/src/pocketmine/world/SimpleChunkManager.php index f2cee8527a..6705b0fe5b 100644 --- a/src/pocketmine/world/SimpleChunkManager.php +++ b/src/pocketmine/world/SimpleChunkManager.php @@ -27,6 +27,7 @@ use pocketmine\block\Block; use pocketmine\block\BlockFactory; use pocketmine\block\BlockLegacyIds; use pocketmine\world\format\Chunk; +use pocketmine\world\utils\SubChunkIteratorManager; use const INT32_MAX; use const INT32_MIN; @@ -37,6 +38,9 @@ class SimpleChunkManager implements ChunkManager{ protected $worldHeight; + /** @var SubChunkIteratorManager */ + protected $terrainPointer; + /** * SimpleChunkManager constructor. * @@ -44,48 +48,49 @@ class SimpleChunkManager implements ChunkManager{ */ public function __construct(int $worldHeight = World::Y_MAX){ $this->worldHeight = $worldHeight; + $this->terrainPointer = new SubChunkIteratorManager($this); } public function getBlockAt(int $x, int $y, int $z) : Block{ - if($chunk = $this->getChunk($x >> 4, $z >> 4)){ - return BlockFactory::fromFullBlock($chunk->getFullBlock($x & 0xf, $y, $z & 0xf)); + if($this->terrainPointer->moveTo($x, $y, $z, false)){ + return BlockFactory::fromFullBlock($this->terrainPointer->currentSubChunk->getFullBlock($x & 0xf, $y & 0xf, $z & 0xf)); } return BlockFactory::get(BlockLegacyIds::AIR); } public function setBlockAt(int $x, int $y, int $z, Block $block) : bool{ - if(($chunk = $this->getChunk($x >> 4, $z >> 4)) !== null){ - $chunk->setFullBlock($x & 0xf, $y, $z & 0xf, $block->getFullId()); + if($this->terrainPointer->moveTo($x, $y, $z, false)){ + $this->terrainPointer->currentSubChunk->setFullBlock($x & 0xf, $y & 0xf, $z & 0xf, $block->getFullId()); return true; } return false; } public function getBlockLightAt(int $x, int $y, int $z) : int{ - if($chunk = $this->getChunk($x >> 4, $z >> 4)){ - return $chunk->getBlockLight($x & 0xf, $y, $z & 0xf); + if($this->terrainPointer->moveTo($x, $y, $z, false)){ + return $this->terrainPointer->currentSubChunk->getBlockLight($x & 0xf, $y & 0xf, $z & 0xf); } return 0; } public function setBlockLightAt(int $x, int $y, int $z, int $level) : void{ - if($chunk = $this->getChunk($x >> 4, $z >> 4)){ - $chunk->setBlockLight($x & 0xf, $y, $z & 0xf, $level); + if($this->terrainPointer->moveTo($x, $y, $z, false)){ + $this->terrainPointer->currentSubChunk->setBlockLight($x & 0xf, $y & 0xf, $z & 0xf, $level); } } public function getBlockSkyLightAt(int $x, int $y, int $z) : int{ - if($chunk = $this->getChunk($x >> 4, $z >> 4)){ - return $chunk->getBlockSkyLight($x & 0xf, $y, $z & 0xf); + if($this->terrainPointer->moveTo($x, $y, $z, false)){ + return $this->terrainPointer->currentSubChunk->getBlockSkyLight($x & 0xf, $y & 0xf, $z & 0xf); } return 0; } public function setBlockSkyLightAt(int $x, int $y, int $z, int $level) : void{ - if($chunk = $this->getChunk($x >> 4, $z >> 4)){ - $chunk->setBlockSkyLight($x & 0xf, $y, $z & 0xf, $level); + if($this->terrainPointer->moveTo($x, $y, $z, false)){ + $this->terrainPointer->currentSubChunk->setBlockSkyLight($x & 0xf, $y & 0xf, $z & 0xf, $level); } }