From 633e77a34cf9741a48fd9b5f19e3c83d24fdb02b Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 4 May 2023 23:21:54 +0100 Subject: [PATCH] RuntimeBlockMapping: share states CompoundTags if they are the same this allows saving about 4 MB of memory, because there are many blocks which have identical states, although they have different IDs. this relies on a potentially risky assumption that the tags in knownStates won't be modified. If they are modified, the changes will influence all blockstates which share the tag. However, I don't expect this to happen, and the 4 MB memory saving is substantial enough to be worth the risk. --- src/network/mcpe/convert/RuntimeBlockMapping.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/network/mcpe/convert/RuntimeBlockMapping.php b/src/network/mcpe/convert/RuntimeBlockMapping.php index 64984c3c1..c2123364b 100644 --- a/src/network/mcpe/convert/RuntimeBlockMapping.php +++ b/src/network/mcpe/convert/RuntimeBlockMapping.php @@ -27,10 +27,12 @@ use pocketmine\block\Block; use pocketmine\block\BlockLegacyIds; use pocketmine\data\bedrock\BedrockDataFiles; use pocketmine\data\bedrock\LegacyBlockIdToStringIdMap; +use pocketmine\nbt\LittleEndianNbtSerializer; use pocketmine\nbt\tag\ByteTag; use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\IntTag; use pocketmine\nbt\tag\StringTag; +use pocketmine\nbt\TreeRoot; use pocketmine\network\mcpe\protocol\serializer\NetworkNbtSerializer; use pocketmine\utils\BinaryStream; use pocketmine\utils\Filesystem; @@ -72,7 +74,7 @@ final class RuntimeBlockMapping{ $key = $keyIndex[$key] ??= $key; if($value instanceof CompoundTag){ - $value = self::deduplicateCompound($value, $keyIndex, $valueIndex); + $value = $valueIndex[$value->getType()][(new LittleEndianNbtSerializer())->write(new TreeRoot($value))] ??= self::deduplicateCompound($value, $keyIndex, $valueIndex); }elseif($value instanceof ByteTag || $value instanceof IntTag || $value instanceof StringTag){ $value = $valueIndex[$value->getType()][$value->getValue()] ??= $value; }