mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-23 00:55:57 +00:00
LightArray: Avoid allocating 7 useless arrays for every node processed
instead, use a const array of the offsets and add them to the coordinates, which avoids the allocations. In synthetic benchmarks, this method takes 40-50% less CPU time by eliding ZEND_INIT_ARRAY and ZEND_ADD_ARRAY opcodes. In practice, the benefit will likely be much smaller (perhaps even irrelevant).
This commit is contained in:
parent
15e5bdb210
commit
f1583f44df
@ -31,6 +31,14 @@ use function max;
|
||||
|
||||
//TODO: make light updates asynchronous
|
||||
abstract class LightUpdate{
|
||||
private const ADJACENTS = [
|
||||
[ 1, 0, 0],
|
||||
[-1, 0, 0],
|
||||
[ 0, 1, 0],
|
||||
[ 0, -1, 0],
|
||||
[ 0, 0, 1],
|
||||
[ 0, 0, -1]
|
||||
];
|
||||
|
||||
/**
|
||||
* @var \SplFixedArray|int[]
|
||||
@ -76,15 +84,8 @@ abstract class LightUpdate{
|
||||
|
||||
protected function getHighestAdjacentLight(int $x, int $y, int $z) : int{
|
||||
$adjacent = 0;
|
||||
foreach([
|
||||
[$x + 1, $y, $z],
|
||||
[$x - 1, $y, $z],
|
||||
[$x, $y + 1, $z],
|
||||
[$x, $y - 1, $z],
|
||||
[$x, $y, $z + 1],
|
||||
[$x, $y, $z - 1]
|
||||
] as [$x1, $y1, $z1]){
|
||||
if(($adjacent = max($adjacent, $this->getEffectiveLight($x1, $y1, $z1))) === 15){
|
||||
foreach(self::ADJACENTS as [$ox, $oy, $oz]){
|
||||
if(($adjacent = max($adjacent, $this->getEffectiveLight($x + $ox, $y + $oy, $z + $oz))) === 15){
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -125,16 +126,11 @@ abstract class LightUpdate{
|
||||
$touched++;
|
||||
[$x, $y, $z, $oldAdjacentLight] = $context->removalQueue->dequeue();
|
||||
|
||||
$points = [
|
||||
[$x + 1, $y, $z],
|
||||
[$x - 1, $y, $z],
|
||||
[$x, $y + 1, $z],
|
||||
[$x, $y - 1, $z],
|
||||
[$x, $y, $z + 1],
|
||||
[$x, $y, $z - 1]
|
||||
];
|
||||
foreach(self::ADJACENTS as [$ox, $oy, $oz]){
|
||||
$cx = $x + $ox;
|
||||
$cy = $y + $oy;
|
||||
$cz = $z + $oz;
|
||||
|
||||
foreach($points as [$cx, $cy, $cz]){
|
||||
if($this->subChunkExplorer->moveTo($cx, $cy, $cz) !== SubChunkExplorerStatus::INVALID){
|
||||
$this->computeRemoveLight($cx, $cy, $cz, $oldAdjacentLight, $context);
|
||||
}elseif($this->getEffectiveLight($cx, $cy, $cz) > 0 and !isset($context->spreadVisited[$index = World::blockHash($cx, $cy, $cz)])){
|
||||
@ -155,16 +151,11 @@ abstract class LightUpdate{
|
||||
continue;
|
||||
}
|
||||
|
||||
$points = [
|
||||
[$x + 1, $y, $z],
|
||||
[$x - 1, $y, $z],
|
||||
[$x, $y + 1, $z],
|
||||
[$x, $y - 1, $z],
|
||||
[$x, $y, $z + 1],
|
||||
[$x, $y, $z - 1]
|
||||
];
|
||||
foreach(self::ADJACENTS as [$ox, $oy, $oz]){
|
||||
$cx = $x + $ox;
|
||||
$cy = $y + $oy;
|
||||
$cz = $z + $oz;
|
||||
|
||||
foreach($points as [$cx, $cy, $cz]){
|
||||
if($this->subChunkExplorer->moveTo($cx, $cy, $cz) !== SubChunkExplorerStatus::INVALID){
|
||||
$this->computeSpreadLight($cx, $cy, $cz, $newAdjacentLight, $context);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user