2020-05-23 13:40:27 +08:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sat, 23 May 2020 01:31:06 -0400
Subject: [PATCH] Fix Non Full Status Chunk NBT Memory Leak
Any full status chunk that was requested for any status less than full
would hold onto their entire nbt tree and every variable in that function.
This was due to use of a lambda that persists on the Chunk object
until that chunk reaches FULL status.
With introduction of no tick, we greatly increased the number of non
full chunks so this was really starting to hurt.
We further improve it by making a copy of the nbt tag with only the memory
it needs, so that we dont have to hold a copy to the entire compound.
diff --git a/src/main/java/net/minecraft/server/ChunkRegionLoader.java b/src/main/java/net/minecraft/server/ChunkRegionLoader.java
2020-10-18 04:42:18 +08:00
index f26740cd0f6a7758dc45cbacd4919d38bbdbb1c4..4e40067d75a1bce92a3804b99ee76ffc43411b75 100644
2020-05-23 13:40:27 +08:00
--- a/src/main/java/net/minecraft/server/ChunkRegionLoader.java
+++ b/src/main/java/net/minecraft/server/ChunkRegionLoader.java
2020-10-17 18:39:45 +08:00
@@ -156,15 +156,9 @@ public class ChunkRegionLoader {
2020-05-23 13:40:27 +08:00
object2 = protochunkticklist1;
}
- object = new Chunk(worldserver.getMinecraftWorld(), chunkcoordintpair, biomestorage, chunkconverter, (TickList) object1, (TickList) object2, j, achunksection, (chunk) -> {
- loadEntities(nbttagcompound1, chunk);
2020-10-17 18:39:45 +08:00
- // CraftBukkit start - load chunk persistent data from nbt
2020-10-18 04:42:18 +08:00
- NBTBase persistentBase = nbttagcompound1.get("ChunkBukkitValues");
- if (persistentBase instanceof NBTTagCompound) {
- chunk.persistentDataContainer.putAll((NBTTagCompound) persistentBase);
2020-10-17 18:39:45 +08:00
- }
- // CraftBukkit end
2020-05-23 13:40:27 +08:00
- });
+ object = new Chunk(worldserver.getMinecraftWorld(), chunkcoordintpair, biomestorage, chunkconverter, (TickList) object1, (TickList) object2, j, achunksection, // Paper start - fix massive nbt memory leak due to lambda. move lambda into a container method to not leak scope. Only clone needed NBT keys.
2020-10-17 18:39:45 +08:00
+ createLoadEntitiesConsumer(new SafeNBTCopy(nbttagcompound1, "TileEntities", "Entities", "BukkitValues")) // Paper - move CB Chunk PDC into here
2020-05-23 13:40:27 +08:00
+ );// Paper end
} else {
ProtoChunk protochunk = new ProtoChunk(chunkcoordintpair, chunkconverter, achunksection, protochunkticklist, protochunkticklist1, worldserver); // Paper - Anti-Xray - Add parameter
2020-10-17 19:46:35 +08:00
@@ -270,6 +264,50 @@ public class ChunkRegionLoader {
2020-05-23 13:40:27 +08:00
return new InProgressChunkHolder(protochunk1, tasksToExecuteOnMain); // Paper - Async chunk loading
}
}
+ // Paper start
+
+ /**
+ * This wrapper will error out if any key is accessed that wasn't copied so we can catch it easy on an update
+ */
+ private static class SafeNBTCopy extends NBTTagCompound {
+ private final java.util.Set<String> keys = new java.util.HashSet<String>();
+ public SafeNBTCopy(NBTTagCompound base, String... keys) {
+ for (String key : keys) {
+ this.keys.add(key);
2020-10-17 19:20:44 +08:00
+ final NBTBase nbtBase = base.get(key);
+ if (nbtBase != null) {
+ this.set(key, nbtBase);
+ }
2020-05-23 13:40:27 +08:00
+ }
+ }
+
+ @Override
+ public boolean hasKey(String s) {
2020-10-17 19:46:35 +08:00
+ if (super.hasKey(s)) {
2020-05-23 13:40:27 +08:00
+ return true;
2020-10-17 19:46:35 +08:00
+ } else if (keys.contains(s)) {
+ return false;
2020-05-23 13:40:27 +08:00
+ }
+ throw new IllegalStateException("Missing Key " + s + " in SafeNBTCopy");
+ }
+
+ @Override
+ public boolean hasKeyOfType(String s, int i) {
+ return hasKey(s) && super.hasKeyOfType(s, i);
+ }
+ }
+ private static java.util.function.Consumer<Chunk> createLoadEntitiesConsumer(NBTTagCompound nbt) {
2020-10-17 18:39:45 +08:00
+ return (chunk) -> {
+ loadEntities(nbt, chunk);
+ // CraftBukkit start - load chunk persistent data from nbt
2020-10-18 04:42:18 +08:00
+ NBTBase persistentBase = nbt.get("ChunkBukkitValues");
+ if (persistentBase instanceof NBTTagCompound) {
+ chunk.persistentDataContainer.putAll((NBTTagCompound) persistentBase);
2020-10-17 18:39:45 +08:00
+ }
+ // CraftBukkit end
+ };
2020-05-23 13:40:27 +08:00
+ }
+ // Paper end
// Paper start - async chunk save for unload
public static final class AsyncSaveData {