From e4c889ae1648a711b6763eda672601d0885fe145 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 18 Jan 2017 20:04:19 +0000 Subject: [PATCH] Check if the array contents are already the same, massive performance improvement for Anvil in a lot of cases --- src/pocketmine/level/format/io/ChunkUtils.php | 59 +++++++++++-------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/src/pocketmine/level/format/io/ChunkUtils.php b/src/pocketmine/level/format/io/ChunkUtils.php index c44130f29..6fbc56934 100644 --- a/src/pocketmine/level/format/io/ChunkUtils.php +++ b/src/pocketmine/level/format/io/ChunkUtils.php @@ -34,19 +34,22 @@ class ChunkUtils{ */ public static final function reorderByteArray(string $array) : string{ $result = str_repeat("\x00", 4096); - $i = 0; - $zM = 0; - $yM = 0; - for($x = 0; $x < 16; ++$x){ - $zM = $x + 256; - for($z = $x; $z < $zM; $z += 16){ - $yM = $z + 4096; - for($y = $z; $y < $yM; $y += 256){ - $result{$i} = $array{$y}; - ++$i; + if($array !== $result){ + $i = 0; + $zM = 0; + $yM = 0; + for($x = 0; $x < 16; ++$x){ + $zM = $x + 256; + for($z = $x; $z < $zM; $z += 16){ + $yM = $z + 4096; + for($y = $z; $y < $yM; $y += 256){ + $result{$i} = $array{$y}; + ++$i; + } } } } + return $result; } @@ -60,26 +63,30 @@ class ChunkUtils{ */ public static final function reorderNibbleArray(string $array, string $commonValue = "\x00") : string{ $result = str_repeat($commonValue, 2048); - $i = 0; - for($x = 0; $x < 8; ++$x){ - for($z = 0; $z < 16; ++$z){ - $zx = (($z << 3) | $x); - for($y = 0; $y < 8; ++$y){ - $j = (($y << 8) | $zx); - $j80 = ($j | 0x80); - if($array{$j} === $commonValue and $array{$j80} === $commonValue){ - //values are already filled - }else{ - $i1 = ord($array{$j}); - $i2 = ord($array{$j80}); - $result{$i} = chr(($i2 << 4) | ($i1 & 0x0f)); - $result{$i | 0x80} = chr(($i1 >> 4) | ($i2 & 0xf0)); + + if($array !== $result){ + $i = 0; + for($x = 0; $x < 8; ++$x){ + for($z = 0; $z < 16; ++$z){ + $zx = (($z << 3) | $x); + for($y = 0; $y < 8; ++$y){ + $j = (($y << 8) | $zx); + $j80 = ($j | 0x80); + if($array{$j} === $commonValue and $array{$j80} === $commonValue){ + //values are already filled + }else{ + $i1 = ord($array{$j}); + $i2 = ord($array{$j80}); + $result{$i} = chr(($i2 << 4) | ($i1 & 0x0f)); + $result{$i | 0x80} = chr(($i1 >> 4) | ($i2 & 0xf0)); + } + $i++; } - $i++; } + $i += 128; } - $i += 128; } + return $result; }