World: added cache for isChunkTickable()

this considerably reduces the amount of work done by the function, since it's usually checking the same chunks over and over again.
This commit is contained in:
Dylan K. Taylor 2022-12-19 21:20:21 +00:00
parent 923dcec4e7
commit ee7d4728d8
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -1151,6 +1151,8 @@ class World implements ChunkManager{
/** @var bool[] $chunkTickList chunkhash => dummy */
$chunkTickList = [];
$chunkTickableCache = [];
$centerChunks = [];
$selector = new ChunkSelector();
@ -1170,7 +1172,7 @@ class World implements ChunkManager{
$centerChunkZ
) as $hash){
World::getXZ($hash, $chunkX, $chunkZ);
if(!isset($chunkTickList[$hash]) && isset($this->chunks[$hash]) && $this->isChunkTickable($chunkX, $chunkZ)){
if(!isset($chunkTickList[$hash]) && isset($this->chunks[$hash]) && $this->isChunkTickable($chunkX, $chunkZ, $chunkTickableCache)){
$chunkTickList[$hash] = true;
}
}
@ -1185,14 +1187,29 @@ class World implements ChunkManager{
}
}
private function isChunkTickable(int $chunkX, int $chunkZ) : bool{
/**
* @param bool[] &$cache
*
* @phpstan-param array<int, bool> $cache
* @phpstan-param-out array<int, bool> $cache
*/
private function isChunkTickable(int $chunkX, int $chunkZ, array &$cache) : bool{
for($cx = -1; $cx <= 1; ++$cx){
for($cz = -1; $cz <= 1; ++$cz){
$chunkHash = World::chunkHash($chunkX + $cx, $chunkZ + $cz);
if(isset($cache[$chunkHash])){
if(!$cache[$chunkHash]){
return false;
}
continue;
}
if($this->isChunkLocked($chunkX + $cx, $chunkZ + $cz)){
$cache[$chunkHash] = false;
return false;
}
$adjacentChunk = $this->getChunk($chunkX + $cx, $chunkZ + $cz);
if($adjacentChunk === null || !$adjacentChunk->isPopulated()){
$cache[$chunkHash] = false;
return false;
}
$lightPopulatedState = $adjacentChunk->isLightPopulated();
@ -1200,8 +1217,11 @@ class World implements ChunkManager{
if($lightPopulatedState === false){
$this->orderLightPopulation($chunkX + $cx, $chunkZ + $cz);
}
$cache[$chunkHash] = false;
return false;
}
$cache[$chunkHash] = true;
}
}