From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Spottedleaf Date: Mon, 6 Apr 2020 17:39:25 -0700 Subject: [PATCH] Reduce memory footprint of NBTTagCompound Fastutil maps are going to have a lower memory footprint - which is important because we clone chunk data after reading it for safety. So, reduce the impact of the clone on GC. diff --git a/src/main/java/net/minecraft/nbt/CompoundTag.java b/src/main/java/net/minecraft/nbt/CompoundTag.java index 92095b494a0c4fb89b84f0b1c0376615d28e34ce..ad0251b73d21b36bf19e9aa649817b4da2d0a6b4 100644 --- a/src/main/java/net/minecraft/nbt/CompoundTag.java +++ b/src/main/java/net/minecraft/nbt/CompoundTag.java @@ -46,7 +46,7 @@ public class CompoundTag implements Tag { private static CompoundTag loadCompound(DataInput input, NbtAccounter tracker) throws IOException { tracker.accountBytes(48L); - Map map = Maps.newHashMap(); + it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap map = new it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<>(8, 0.8f); // Paper - reduce memory footprint of NBTTagCompound byte b; while((b = input.readByte()) != 0) { @@ -167,7 +167,7 @@ public class CompoundTag implements Tag { } public CompoundTag() { - this(Maps.newHashMap()); + this(new it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<>(8, 0.8f)); // Paper - reduce memory footprint of NBTTagCompound } @Override @@ -486,8 +486,16 @@ public class CompoundTag implements Tag { @Override public CompoundTag copy() { - Map map = Maps.newHashMap(Maps.transformValues(this.tags, Tag::copy)); - return new CompoundTag(map); + // Paper start - reduce memory footprint of NBTTagCompound + it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap ret = new it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<>(this.tags.size(), 0.8f); + java.util.Iterator> iterator = (this.tags instanceof it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap) ? ((it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap)this.tags).object2ObjectEntrySet().fastIterator() : this.tags.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + ret.put(entry.getKey(), entry.getValue().copy()); + } + + return new CompoundTag(ret); + // Paper end - reduce memory footprint of NBTTagCompound } @Override