mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-28 14:19:53 +00:00
SubChunk: reduce memory usage on fast-deserialized chunks
this saves about 25MB RAM on an idle server on HEAD commit with 856 chunks loaded.
This commit is contained in:
parent
261ba017a6
commit
419fc418fe
@ -32,11 +32,13 @@ use function defined;
|
|||||||
use function ord;
|
use function ord;
|
||||||
use function str_repeat;
|
use function str_repeat;
|
||||||
use function strlen;
|
use function strlen;
|
||||||
use function substr_count;
|
|
||||||
|
|
||||||
if(!defined(__NAMESPACE__ . '\ZERO_NIBBLE_ARRAY')){
|
if(!defined(__NAMESPACE__ . '\ZERO_NIBBLE_ARRAY')){
|
||||||
define(__NAMESPACE__ . '\ZERO_NIBBLE_ARRAY', str_repeat("\x00", 2048));
|
define(__NAMESPACE__ . '\ZERO_NIBBLE_ARRAY', str_repeat("\x00", 2048));
|
||||||
}
|
}
|
||||||
|
if(!defined(__NAMESPACE__ . '\FIFTEEN_NIBBLE_ARRAY')){
|
||||||
|
define(__NAMESPACE__ . '\FIFTEEN_NIBBLE_ARRAY', str_repeat("\xff", 2048));
|
||||||
|
}
|
||||||
|
|
||||||
class SubChunk implements SubChunkInterface{
|
class SubChunk implements SubChunkInterface{
|
||||||
/** @var PalettedBlockArray[] */
|
/** @var PalettedBlockArray[] */
|
||||||
@ -47,10 +49,12 @@ class SubChunk implements SubChunkInterface{
|
|||||||
/** @var string */
|
/** @var string */
|
||||||
protected $skyLight;
|
protected $skyLight;
|
||||||
|
|
||||||
private static function assignData(&$target, string $data, int $length, string $value = "\x00") : void{
|
private static function assignData(&$target, string $data, string $default) : void{
|
||||||
if(strlen($data) !== $length){
|
if($data === "" or $data === $default){
|
||||||
assert($data === "", "Invalid non-zero length given, expected $length, got " . strlen($data));
|
$target = $default;
|
||||||
$target = str_repeat($value, $length);
|
}elseif(strlen($data) !== 2048){
|
||||||
|
assert(false, "Invalid length given, expected 2048, got " . strlen($data));
|
||||||
|
$target = $default;
|
||||||
}else{
|
}else{
|
||||||
$target = $data;
|
$target = $data;
|
||||||
}
|
}
|
||||||
@ -66,8 +70,8 @@ class SubChunk implements SubChunkInterface{
|
|||||||
public function __construct(array $blocks, string $skyLight = "", string $blockLight = ""){
|
public function __construct(array $blocks, string $skyLight = "", string $blockLight = ""){
|
||||||
$this->blockLayers = $blocks;
|
$this->blockLayers = $blocks;
|
||||||
|
|
||||||
self::assignData($this->skyLight, $skyLight, 2048, "\xff");
|
self::assignData($this->skyLight, $skyLight, FIFTEEN_NIBBLE_ARRAY);
|
||||||
self::assignData($this->blockLight, $blockLight, 2048);
|
self::assignData($this->blockLight, $blockLight, ZERO_NIBBLE_ARRAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isEmpty(bool $checkLight = true) : bool{
|
public function isEmpty(bool $checkLight = true) : bool{
|
||||||
@ -81,7 +85,7 @@ class SubChunk implements SubChunkInterface{
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
(!$checkLight or (
|
(!$checkLight or (
|
||||||
substr_count($this->skyLight, "\xff") === 2048 and
|
$this->skyLight === FIFTEEN_NIBBLE_ARRAY and
|
||||||
$this->blockLight === ZERO_NIBBLE_ARRAY
|
$this->blockLight === ZERO_NIBBLE_ARRAY
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user