2020-05-06 17:48:49 +08:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2020-04-08 15:09:05 +08:00
From: Aikar <aikar@aikar.co>
Date: Wed, 8 Apr 2020 03:06:30 -0400
Subject: [PATCH] Optimize PlayerChunkMap memory use for visibleChunks
No longer clones visible chunks which is causing massive memory
allocation issues, likely the source of Humongous Objects on large servers.
Instead we just synchronize, clear and rebuild, reusing the same object buffers
as before with only 2 small objects created (FastIterator/MapEntry)
This should result in siginificant memory use reduction and improved GC behavior.
2020-04-11 02:11:46 +08:00
diff --git a/src/main/java/com/destroystokyo/paper/util/map/Long2ObjectLinkedOpenHashMapFastCopy.java b/src/main/java/com/destroystokyo/paper/util/map/Long2ObjectLinkedOpenHashMapFastCopy.java
new file mode 100644
2020-05-21 10:22:47 +08:00
index 0000000000000000000000000000000000000000..f6ff4d8132a95895680f5bc81f8f873e78f0bbdb
2020-04-11 02:11:46 +08:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/util/map/Long2ObjectLinkedOpenHashMapFastCopy.java
2020-05-21 10:22:47 +08:00
@@ -0,0 +1,39 @@
2020-04-11 02:11:46 +08:00
+package com.destroystokyo.paper.util.map;
+
+import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap;
+
+public class Long2ObjectLinkedOpenHashMapFastCopy<V> extends Long2ObjectLinkedOpenHashMap<V> {
+
+ public void copyFrom(Long2ObjectLinkedOpenHashMapFastCopy<V> map) {
2020-05-21 10:22:47 +08:00
+ if (key.length != map.key.length) {
2020-04-11 02:11:46 +08:00
+ key = null;
+ key = new long[map.key.length];
+ }
2020-05-21 10:22:47 +08:00
+ if (value.length != map.value.length) {
2020-04-11 02:11:46 +08:00
+ value = null;
+ //noinspection unchecked
+ value = (V[]) new Object[map.value.length];
+ }
2020-05-21 10:22:47 +08:00
+ if (link.length != map.link.length) {
2020-04-11 02:11:46 +08:00
+ link = null;
+ link = new long[map.link.length];
+ }
+ System.arraycopy(map.key, 0, this.key, 0, map.key.length);
+ System.arraycopy(map.value, 0, this.value, 0, map.value.length);
+ System.arraycopy(map.link, 0, this.link, 0, map.link.length);
+ this.size = map.size;
+ this.mask = map.mask;
+ this.first = map.first;
+ this.last = map.last;
+ this.n = map.n;
+ this.maxFill = map.maxFill;
+ this.containsNullKey = map.containsNullKey;
+ }
2020-05-21 10:22:47 +08:00
+
+ @Override
+ public Long2ObjectLinkedOpenHashMapFastCopy<V> clone() {
+ Long2ObjectLinkedOpenHashMapFastCopy<V> clone = (Long2ObjectLinkedOpenHashMapFastCopy<V>) super.clone();
+ clone.copyFrom(this);
+ return clone;
+ }
2020-04-11 02:11:46 +08:00
+}
2020-04-08 15:09:05 +08:00
diff --git a/src/main/java/net/minecraft/server/ChunkProviderServer.java b/src/main/java/net/minecraft/server/ChunkProviderServer.java
2020-11-03 10:22:15 +08:00
index ce671ecabc402053b160df0f9d64d22b784d165c..dea7ff47f7a2934708d887c2c40f9328b78fc84e 100644
2020-04-08 15:09:05 +08:00
--- a/src/main/java/net/minecraft/server/ChunkProviderServer.java
+++ b/src/main/java/net/minecraft/server/ChunkProviderServer.java
2020-08-25 10:22:08 +08:00
@@ -757,7 +757,7 @@ public class ChunkProviderServer extends IChunkProvider {
2020-04-08 15:09:05 +08:00
entityPlayer.playerNaturallySpawnedEvent.callEvent();
};
// Paper end
2020-06-26 09:53:21 +08:00
- this.playerChunkMap.f().forEach((playerchunk) -> { // Paper - no... just no...
2020-04-09 09:07:59 +08:00
+ this.playerChunkMap.forEachVisibleChunk((playerchunk) -> { // Paper - safe iterator incase chunk loads, also no wrapping
2020-06-26 09:53:21 +08:00
Optional<Chunk> optional = ((Either) playerchunk.a().getNow(PlayerChunk.UNLOADED_CHUNK)).left();
2020-04-08 15:09:05 +08:00
if (optional.isPresent()) {
diff --git a/src/main/java/net/minecraft/server/MCUtil.java b/src/main/java/net/minecraft/server/MCUtil.java
2020-08-25 10:22:08 +08:00
index c470c6527b214026c230feaae0c0875c86dea673..df3150072fd36dac28d83309e50342c9cfa326b3 100644
2020-04-08 15:09:05 +08:00
--- a/src/main/java/net/minecraft/server/MCUtil.java
+++ b/src/main/java/net/minecraft/server/MCUtil.java
2020-08-25 10:22:08 +08:00
@@ -599,7 +599,7 @@ public final class MCUtil {
2020-04-08 15:09:05 +08:00
WorldServer world = ((org.bukkit.craftbukkit.CraftWorld)bukkitWorld).getHandle();
PlayerChunkMap chunkMap = world.getChunkProvider().playerChunkMap;
- Long2ObjectLinkedOpenHashMap<PlayerChunk> visibleChunks = chunkMap.visibleChunks;
+ Long2ObjectLinkedOpenHashMap<PlayerChunk> visibleChunks = chunkMap.getVisibleChunks();
2020-08-02 13:39:36 +08:00
ChunkMapDistance chunkMapDistance = chunkMap.chunkDistanceManager;
2020-04-08 15:09:05 +08:00
List<PlayerChunk> allChunks = new ArrayList<>(visibleChunks.values());
List<EntityPlayer> players = world.players;
diff --git a/src/main/java/net/minecraft/server/PlayerChunkMap.java b/src/main/java/net/minecraft/server/PlayerChunkMap.java
2020-08-25 10:22:08 +08:00
index f839091ae4385e763a19d680585a9363fad6b744..d3a597d878de4cb625484cbf3a9838961d38ada0 100644
2020-04-08 15:09:05 +08:00
--- a/src/main/java/net/minecraft/server/PlayerChunkMap.java
+++ b/src/main/java/net/minecraft/server/PlayerChunkMap.java
2020-08-25 10:22:08 +08:00
@@ -57,8 +57,33 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
2020-04-11 02:11:46 +08:00
2020-04-08 15:09:05 +08:00
private static final Logger LOGGER = LogManager.getLogger();
public static final int GOLDEN_TICKET = 33 + ChunkStatus.b();
2020-04-11 02:11:46 +08:00
- public final Long2ObjectLinkedOpenHashMap<PlayerChunk> updatingChunks = new Long2ObjectLinkedOpenHashMap();
2020-04-08 15:09:05 +08:00
- public volatile Long2ObjectLinkedOpenHashMap<PlayerChunk> visibleChunks;
2020-05-21 10:22:47 +08:00
+ // Paper start - faster copying
+ public final Long2ObjectLinkedOpenHashMap<PlayerChunk> updatingChunks = new com.destroystokyo.paper.util.map.Long2ObjectLinkedOpenHashMapFastCopy<>(); // Paper - faster copying
+ public final Long2ObjectLinkedOpenHashMap<PlayerChunk> visibleChunks = new ProtectedVisibleChunksMap(); // Paper - faster copying
+
+ private class ProtectedVisibleChunksMap extends com.destroystokyo.paper.util.map.Long2ObjectLinkedOpenHashMapFastCopy<PlayerChunk> {
+ @Override
+ public PlayerChunk put(long k, PlayerChunk playerChunk) {
+ throw new UnsupportedOperationException("Updating visible Chunks");
+ }
+
+ @Override
+ public PlayerChunk remove(long k) {
+ throw new UnsupportedOperationException("Removing visible Chunks");
+ }
+
+ @Override
+ public PlayerChunk get(long k) {
+ return PlayerChunkMap.this.getVisibleChunk(k);
+ }
+
+ public PlayerChunk safeGet(long k) {
+ return super.get(k);
+ }
+ }
+ // Paper end
+ public final com.destroystokyo.paper.util.map.Long2ObjectLinkedOpenHashMapFastCopy<PlayerChunk> pendingVisibleChunks = new com.destroystokyo.paper.util.map.Long2ObjectLinkedOpenHashMapFastCopy<PlayerChunk>(); // Paper - this is used if the visible chunks is updated while iterating only
+ public transient com.destroystokyo.paper.util.map.Long2ObjectLinkedOpenHashMapFastCopy<PlayerChunk> visibleChunksClone; // Paper - used for async access of visible chunks, clone and cache only when needed
2020-04-08 15:09:05 +08:00
private final Long2ObjectLinkedOpenHashMap<PlayerChunk> pendingUnload;
final LongSet loadedChunks; // Paper - private -> package
public final WorldServer world;
2020-08-25 10:22:08 +08:00
@@ -131,7 +156,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
2020-04-08 15:09:05 +08:00
2020-06-26 09:53:21 +08:00
public PlayerChunkMap(WorldServer worldserver, Convertable.ConversionSession convertable_conversionsession, DataFixer datafixer, DefinedStructureManager definedstructuremanager, Executor executor, IAsyncTaskHandler<Runnable> iasynctaskhandler, ILightAccess ilightaccess, ChunkGenerator chunkgenerator, WorldLoadListener worldloadlistener, Supplier<WorldPersistentData> supplier, int i, boolean flag) {
super(new File(convertable_conversionsession.a(worldserver.getDimensionKey()), "region"), datafixer, flag);
2020-04-08 15:09:05 +08:00
- this.visibleChunks = this.updatingChunks.clone();
+ //this.visibleChunks = this.updatingChunks.clone(); // Paper - no more cloning
this.pendingUnload = new Long2ObjectLinkedOpenHashMap();
this.loadedChunks = new LongOpenHashSet();
this.unloadQueue = new LongOpenHashSet();
2020-08-25 10:22:08 +08:00
@@ -223,9 +248,52 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
2020-04-08 15:09:05 +08:00
return (PlayerChunk) this.updatingChunks.get(i);
}
+ // Paper start - remove cloning of visible chunks unless accessed as a collection async
+ private static final boolean DEBUG_ASYNC_VISIBLE_CHUNKS = Boolean.getBoolean("paper.debug-async-visible-chunks");
2020-04-09 09:07:59 +08:00
+ private boolean isIterating = false;
2020-04-11 02:11:46 +08:00
+ private boolean hasPendingVisibleUpdate = false;
2020-04-09 09:07:59 +08:00
+ public void forEachVisibleChunk(java.util.function.Consumer<PlayerChunk> consumer) {
+ org.spigotmc.AsyncCatcher.catchOp("forEachVisibleChunk");
+ boolean prev = isIterating;
+ isIterating = true;
+ try {
+ for (PlayerChunk value : this.visibleChunks.values()) {
+ consumer.accept(value);
+ }
+ } finally {
+ this.isIterating = prev;
2020-04-11 02:11:46 +08:00
+ if (!this.isIterating && this.hasPendingVisibleUpdate) {
2020-05-21 10:22:47 +08:00
+ ((ProtectedVisibleChunksMap)this.visibleChunks).copyFrom(this.pendingVisibleChunks);
2020-04-11 02:11:46 +08:00
+ this.pendingVisibleChunks.clear();
+ this.hasPendingVisibleUpdate = false;
2020-04-09 09:07:59 +08:00
+ }
+ }
+ }
2020-04-08 15:09:05 +08:00
+ public Long2ObjectLinkedOpenHashMap<PlayerChunk> getVisibleChunks() {
+ if (Thread.currentThread() == this.world.serverThread) {
+ return this.visibleChunks;
+ } else {
+ synchronized (this.visibleChunks) {
+ if (DEBUG_ASYNC_VISIBLE_CHUNKS) new Throwable("Async getVisibleChunks").printStackTrace();
+ if (this.visibleChunksClone == null) {
2020-05-21 10:22:47 +08:00
+ this.visibleChunksClone = this.hasPendingVisibleUpdate ? this.pendingVisibleChunks.clone() : ((ProtectedVisibleChunksMap)this.visibleChunks).clone();
2020-04-08 15:09:05 +08:00
+ }
+ return this.visibleChunksClone;
+ }
+ }
+ }
+ // Paper end
+
@Nullable
public PlayerChunk getVisibleChunk(long i) { // Paper - protected -> public
2020-04-11 02:11:46 +08:00
- return (PlayerChunk) this.visibleChunks.get(i);
2020-04-08 15:09:05 +08:00
+ // Paper start - mt safe get
+ if (Thread.currentThread() != this.world.serverThread) {
+ synchronized (this.visibleChunks) {
2020-05-21 10:22:47 +08:00
+ return (PlayerChunk) (this.hasPendingVisibleUpdate ? this.pendingVisibleChunks.get(i) : ((ProtectedVisibleChunksMap)this.visibleChunks).safeGet(i));
2020-04-08 15:09:05 +08:00
+ }
+ }
2020-05-21 10:22:47 +08:00
+ return (PlayerChunk) (this.hasPendingVisibleUpdate ? this.pendingVisibleChunks.get(i) : ((ProtectedVisibleChunksMap)this.visibleChunks).safeGet(i));
2020-04-08 15:09:05 +08:00
+ // Paper end
}
2020-04-11 02:11:46 +08:00
protected IntSupplier c(long i) {
2020-08-25 10:22:08 +08:00
@@ -413,8 +481,9 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
2020-04-08 15:09:05 +08:00
// Paper end
protected void save(boolean flag) {
+ Long2ObjectLinkedOpenHashMap<PlayerChunk> visibleChunks = this.getVisibleChunks(); // Paper remove clone of visible Chunks unless saving off main thread (watchdog kill)
if (flag) {
- List<PlayerChunk> list = (List) this.visibleChunks.values().stream().filter(PlayerChunk::hasBeenLoaded).peek(PlayerChunk::m).collect(Collectors.toList());
+ List<PlayerChunk> list = (List) visibleChunks.values().stream().filter(PlayerChunk::hasBeenLoaded).peek(PlayerChunk::m).collect(Collectors.toList()); // Paper - remove cloning of visible chunks
MutableBoolean mutableboolean = new MutableBoolean();
do {
2020-08-25 10:22:08 +08:00
@@ -442,7 +511,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
2020-04-08 15:09:05 +08:00
// this.i(); // Paper - nuke IOWorker
PlayerChunkMap.LOGGER.info("ThreadedAnvilChunkStorage ({}): All chunks are saved", this.w.getName());
} else {
- this.visibleChunks.values().stream().filter(PlayerChunk::hasBeenLoaded).forEach((playerchunk) -> {
+ visibleChunks.values().stream().filter(PlayerChunk::hasBeenLoaded).forEach((playerchunk) -> {
IChunkAccess ichunkaccess = (IChunkAccess) playerchunk.getChunkSave().getNow(null); // CraftBukkit - decompile error
if (ichunkaccess instanceof ProtoChunkExtension || ichunkaccess instanceof Chunk) {
2020-08-25 10:22:08 +08:00
@@ -613,7 +682,20 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
2020-04-11 02:11:46 +08:00
if (!this.updatingChunksModified) {
2020-04-08 15:09:05 +08:00
return false;
} else {
- this.visibleChunks = this.updatingChunks.clone();
+ // Paper start - stop cloning visibleChunks
+ synchronized (this.visibleChunks) {
2020-04-11 02:11:46 +08:00
+ if (isIterating) {
+ hasPendingVisibleUpdate = true;
2020-05-21 10:22:47 +08:00
+ this.pendingVisibleChunks.copyFrom((com.destroystokyo.paper.util.map.Long2ObjectLinkedOpenHashMapFastCopy<PlayerChunk>)this.updatingChunks);
2020-04-11 02:11:46 +08:00
+ } else {
+ hasPendingVisibleUpdate = false;
+ this.pendingVisibleChunks.clear();
2020-05-21 10:22:47 +08:00
+ ((ProtectedVisibleChunksMap)this.visibleChunks).copyFrom((com.destroystokyo.paper.util.map.Long2ObjectLinkedOpenHashMapFastCopy<PlayerChunk>)this.updatingChunks);
2020-04-11 02:11:46 +08:00
+ this.visibleChunksClone = null;
+ }
2020-04-08 15:09:05 +08:00
+ }
+ // Paper end
+
this.updatingChunksModified = false;
return true;
}
2020-08-25 10:22:08 +08:00
@@ -1084,12 +1166,12 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
2020-04-08 15:09:05 +08:00
}
protected Iterable<PlayerChunk> f() {
- return Iterables.unmodifiableIterable(this.visibleChunks.values());
+ return Iterables.unmodifiableIterable(this.getVisibleChunks().values()); // Paper
}
void a(Writer writer) throws IOException {
CSVWriter csvwriter = CSVWriter.a().a("x").a("z").a("level").a("in_memory").a("status").a("full_status").a("accessible_ready").a("ticking_ready").a("entity_ticking_ready").a("ticket").a("spawning").a("entity_count").a("block_entity_count").a(writer);
- ObjectBidirectionalIterator objectbidirectionaliterator = this.visibleChunks.long2ObjectEntrySet().iterator();
+ ObjectBidirectionalIterator objectbidirectionaliterator = this.getVisibleChunks().long2ObjectEntrySet().iterator(); // Paper
while (objectbidirectionaliterator.hasNext()) {
Entry<PlayerChunk> entry = (Entry) objectbidirectionaliterator.next();
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
2020-08-25 10:22:08 +08:00
index 20aeec95138a08accc4cb7b1f2d80d63bb9bf920..5ef12e85d7374c137e2d7ff3e0571995070cc222 100644
2020-04-08 15:09:05 +08:00
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
2020-08-25 10:22:08 +08:00
@@ -74,6 +74,7 @@ import net.minecraft.server.GroupDataEntity;
2020-04-09 09:07:59 +08:00
import net.minecraft.server.IBlockData;
import net.minecraft.server.IChunkAccess;
2020-08-25 10:22:08 +08:00
import net.minecraft.server.IRegistry;
2020-04-09 09:07:59 +08:00
+import net.minecraft.server.MCUtil;
import net.minecraft.server.MinecraftKey;
2020-05-10 10:30:28 +08:00
import net.minecraft.server.MinecraftServer;
2020-04-09 09:07:59 +08:00
import net.minecraft.server.MovingObjectPosition;
2020-07-01 08:07:16 +08:00
@@ -294,6 +295,7 @@ public class CraftWorld implements World {
2020-04-08 15:09:05 +08:00
return ret;
}
public int getTileEntityCount() {
2020-04-09 09:07:59 +08:00
+ return MCUtil.ensureMain(() -> {
2020-04-08 15:09:05 +08:00
// We don't use the full world tile entity list, so we must iterate chunks
Long2ObjectLinkedOpenHashMap<PlayerChunk> chunks = world.getChunkProvider().playerChunkMap.visibleChunks;
int size = 0;
2020-07-01 08:07:16 +08:00
@@ -305,11 +307,13 @@ public class CraftWorld implements World {
2020-04-09 09:07:59 +08:00
size += chunk.tileEntities.size();
}
return size;
+ });
}
public int getTickableTileEntityCount() {
2020-04-08 15:09:05 +08:00
return world.tileEntityListTick.size();
}
public int getChunkCount() {
2020-04-09 09:07:59 +08:00
+ return MCUtil.ensureMain(() -> {
2020-04-08 15:09:05 +08:00
int ret = 0;
for (PlayerChunk chunkHolder : world.getChunkProvider().playerChunkMap.visibleChunks.values()) {
2020-07-01 08:07:16 +08:00
@@ -318,7 +322,7 @@ public class CraftWorld implements World {
2020-04-09 09:07:59 +08:00
}
}
- return ret;
+ return ret; });
}
public int getPlayerCount() {
return world.players.size();
2020-08-25 10:22:08 +08:00
@@ -443,6 +447,14 @@ public class CraftWorld implements World {
2020-04-08 15:09:05 +08:00
@Override
public Chunk[] getLoadedChunks() {
2020-04-09 09:07:59 +08:00
+ // Paper start
+ if (Thread.currentThread() != world.getMinecraftWorld().serverThread) {
+ synchronized (world.getChunkProvider().playerChunkMap.visibleChunks) {
+ Long2ObjectLinkedOpenHashMap<PlayerChunk> chunks = world.getChunkProvider().playerChunkMap.visibleChunks;
+ return chunks.values().stream().map(PlayerChunk::getFullChunk).filter(Objects::nonNull).map(net.minecraft.server.Chunk::getBukkitChunk).toArray(Chunk[]::new);
+ }
+ }
+ // Paper end
2020-04-08 15:09:05 +08:00
Long2ObjectLinkedOpenHashMap<PlayerChunk> chunks = world.getChunkProvider().playerChunkMap.visibleChunks;
return chunks.values().stream().map(PlayerChunk::getFullChunk).filter(Objects::nonNull).map(net.minecraft.server.Chunk::getBukkitChunk).toArray(Chunk[]::new);
}