diff --git a/src/data/bedrock/block/CachingBlockStateDeserializer.php b/src/data/bedrock/block/CachingBlockStateDeserializer.php deleted file mode 100644 index 2078b7cc5..000000000 --- a/src/data/bedrock/block/CachingBlockStateDeserializer.php +++ /dev/null @@ -1,51 +0,0 @@ - - */ - private array $simpleCache = []; - - public function __construct( - private BlockStateDeserializer $realDeserializer - ){} - - public function deserialize(BlockStateData $stateData) : int{ - if(count($stateData->getStates()) === 0){ - //if a block has zero properties, we can keep a map of string ID -> internal blockstate ID - return $this->simpleCache[$stateData->getName()] ??= $this->realDeserializer->deserialize($stateData); - } - - //we can't cache blocks that have properties - go ahead and deserialize the slow way - return $this->realDeserializer->deserialize($stateData); - } - - public function getRealDeserializer() : BlockStateDeserializer{ return $this->realDeserializer; } -} diff --git a/src/data/bedrock/block/CachingBlockStateSerializer.php b/src/data/bedrock/block/CachingBlockStateSerializer.php deleted file mode 100644 index f6129622d..000000000 --- a/src/data/bedrock/block/CachingBlockStateSerializer.php +++ /dev/null @@ -1,43 +0,0 @@ - - */ - private array $cache = []; - - public function __construct( - private BlockStateSerializer $realSerializer - ){} - - public function serialize(int $stateId) : BlockStateData{ - return $this->cache[$stateId] ??= $this->realSerializer->serialize($stateId); - } - - public function getRealSerializer() : BlockStateSerializer{ return $this->realSerializer; } -} diff --git a/src/data/bedrock/block/DelegatingBlockStateDeserializer.php b/src/data/bedrock/block/DelegatingBlockStateDeserializer.php deleted file mode 100644 index 1844d44fc..000000000 --- a/src/data/bedrock/block/DelegatingBlockStateDeserializer.php +++ /dev/null @@ -1,29 +0,0 @@ - + */ + private array $cache = []; + public function __construct(){ $this->registerCandleSerializers(); $this->registerCauldronSerializers(); @@ -188,7 +194,8 @@ final class BlockObjectToBlockStateSerializer implements BlockStateSerializer{ public function serialize(int $stateId) : BlockStateData{ //TODO: singleton usage not ideal - return $this->serializeBlock(BlockFactory::getInstance()->fromStateId($stateId)); + //TODO: we may want to deduplicate cache entries to avoid wasting memory + return $this->cache[$stateId] ??= $this->serializeBlock(BlockFactory::getInstance()->fromStateId($stateId)); } /** diff --git a/src/data/bedrock/block/convert/BlockStateToBlockObjectDeserializer.php b/src/data/bedrock/block/convert/BlockStateToBlockObjectDeserializer.php index adb9fe368..14871be25 100644 --- a/src/data/bedrock/block/convert/BlockStateToBlockObjectDeserializer.php +++ b/src/data/bedrock/block/convert/BlockStateToBlockObjectDeserializer.php @@ -51,6 +51,7 @@ use pocketmine\data\bedrock\block\convert\BlockStateReader as Reader; use pocketmine\math\Axis; use pocketmine\math\Facing; use function array_key_exists; +use function count; use function min; final class BlockStateToBlockObjectDeserializer implements BlockStateDeserializer{ @@ -61,6 +62,12 @@ final class BlockStateToBlockObjectDeserializer implements BlockStateDeserialize */ private array $deserializeFuncs = []; + /** + * @var int[] + * @phpstan-var array + */ + private array $simpleCache = []; + public function __construct(){ $this->registerCandleDeserializers(); $this->registerCauldronDeserializers(); @@ -69,6 +76,12 @@ final class BlockStateToBlockObjectDeserializer implements BlockStateDeserialize } public function deserialize(BlockStateData $stateData) : int{ + if(count($stateData->getStates()) === 0){ + //if a block has zero properties, we can keep a map of string ID -> internal blockstate ID + return $this->simpleCache[$stateData->getName()] ??= $this->deserializeBlock($stateData)->getStateId(); + } + + //we can't cache blocks that have properties - go ahead and deserialize the slow way return $this->deserializeBlock($stateData)->getStateId(); } diff --git a/src/world/format/io/GlobalBlockStateHandlers.php b/src/world/format/io/GlobalBlockStateHandlers.php index 195b9337a..49924f5ed 100644 --- a/src/world/format/io/GlobalBlockStateHandlers.php +++ b/src/world/format/io/GlobalBlockStateHandlers.php @@ -24,11 +24,7 @@ declare(strict_types=1); namespace pocketmine\world\format\io; use pocketmine\data\bedrock\block\BlockStateData; -use pocketmine\data\bedrock\block\BlockStateDeserializer; -use pocketmine\data\bedrock\block\BlockStateSerializer; use pocketmine\data\bedrock\block\BlockTypeNames; -use pocketmine\data\bedrock\block\CachingBlockStateDeserializer; -use pocketmine\data\bedrock\block\CachingBlockStateSerializer; use pocketmine\data\bedrock\block\convert\BlockObjectToBlockStateSerializer; use pocketmine\data\bedrock\block\convert\BlockStateToBlockObjectDeserializer; use pocketmine\data\bedrock\block\upgrade\BlockDataUpgrader; @@ -49,20 +45,20 @@ use const pocketmine\BEDROCK_BLOCK_UPGRADE_SCHEMA_PATH; */ final class GlobalBlockStateHandlers{ - private static ?BlockStateSerializer $blockStateSerializer = null; + private static ?BlockObjectToBlockStateSerializer $blockStateSerializer = null; - private static ?BlockStateDeserializer $blockStateDeserializer = null; + private static ?BlockStateToBlockObjectDeserializer $blockStateDeserializer = null; private static ?BlockDataUpgrader $blockDataUpgrader = null; private static ?BlockStateData $unknownBlockStateData = null; - public static function getDeserializer() : BlockStateDeserializer{ - return self::$blockStateDeserializer ??= new CachingBlockStateDeserializer(new BlockStateToBlockObjectDeserializer()); + public static function getDeserializer() : BlockStateToBlockObjectDeserializer{ + return self::$blockStateDeserializer ??= new BlockStateToBlockObjectDeserializer(); } - public static function getSerializer() : BlockStateSerializer{ - return self::$blockStateSerializer ??= new CachingBlockStateSerializer(new BlockObjectToBlockStateSerializer()); + public static function getSerializer() : BlockObjectToBlockStateSerializer{ + return self::$blockStateSerializer ??= new BlockObjectToBlockStateSerializer(); } public static function getUpgrader() : BlockDataUpgrader{