Skip to content

Commit 633e77a

Browse files
committed
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.
1 parent 092d130 commit 633e77a

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/network/mcpe/convert/RuntimeBlockMapping.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
use pocketmine\block\BlockLegacyIds;
2828
use pocketmine\data\bedrock\BedrockDataFiles;
2929
use pocketmine\data\bedrock\LegacyBlockIdToStringIdMap;
30+
use pocketmine\nbt\LittleEndianNbtSerializer;
3031
use pocketmine\nbt\tag\ByteTag;
3132
use pocketmine\nbt\tag\CompoundTag;
3233
use pocketmine\nbt\tag\IntTag;
3334
use pocketmine\nbt\tag\StringTag;
35+
use pocketmine\nbt\TreeRoot;
3436
use pocketmine\network\mcpe\protocol\serializer\NetworkNbtSerializer;
3537
use pocketmine\utils\BinaryStream;
3638
use pocketmine\utils\Filesystem;
@@ -72,7 +74,7 @@ private static function deduplicateCompound(CompoundTag $tag, array &$keyIndex,
7274
$key = $keyIndex[$key] ??= $key;
7375

7476
if($value instanceof CompoundTag){
75-
$value = self::deduplicateCompound($value, $keyIndex, $valueIndex);
77+
$value = $valueIndex[$value->getType()][(new LittleEndianNbtSerializer())->write(new TreeRoot($value))] ??= self::deduplicateCompound($value, $keyIndex, $valueIndex);
7678
}elseif($value instanceof ByteTag || $value instanceof IntTag || $value instanceof StringTag){
7779
$value = $valueIndex[$value->getType()][$value->getValue()] ??= $value;
7880
}

0 commit comments

Comments
 (0)