From 3fe4a2dd2fc6e851bec0fd85d65fc03ce245200c Mon Sep 17 00:00:00 2001 From: Aikar Date: Wed, 10 Oct 2018 22:48:35 -0400 Subject: [PATCH] Improvements to Async Chunks If a chunk load comes in on a chunk load or gen thread, execute it synchronously on that thread instead of enqueueing it. It doesn't make sense to enqueue it as that thread is then going to future.join() it and block until it's ready anyways. This opens risk to a deadlock if every load or gen thread is waiting on a recursive chunk but it will never finish because all of the threads are waiting. --- .../Async-Chunk-Loading-and-Generation.patch | 33 ++++++++++++++++--- ...tect-and-repair-corrupt-Region-Files.patch | 5 +-- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/Spigot-Server-Patches/Async-Chunk-Loading-and-Generation.patch b/Spigot-Server-Patches/Async-Chunk-Loading-and-Generation.patch index e85cf88ad9..2f77669981 100644 --- a/Spigot-Server-Patches/Async-Chunk-Loading-and-Generation.patch +++ b/Spigot-Server-Patches/Async-Chunk-Loading-and-Generation.patch @@ -904,7 +904,7 @@ index 98d182fdb8..487d98eb1b 100644 diff --git a/src/main/java/net/minecraft/server/PaperAsyncChunkProvider.java b/src/main/java/net/minecraft/server/PaperAsyncChunkProvider.java new file mode 100644 -index 0000000000..2dfa59b204 +index 0000000000..4fc5fad09e --- /dev/null +++ b/src/main/java/net/minecraft/server/PaperAsyncChunkProvider.java @@ -0,0 +0,0 @@ @@ -965,6 +965,8 @@ index 0000000000..2dfa59b204 + private final Long2ObjectMap pendingChunks = Long2ObjectMaps.synchronize(new Long2ObjectOpenHashMap<>()); + private final ConcurrentLinkedQueue mainThreadQueue = new ConcurrentLinkedQueue<>(); + private final IAsyncTaskHandler asyncHandler; ++ private final ThreadLocal isChunkThread = ThreadLocal.withInitial(() -> false); ++ private final ThreadLocal isChunkGenThread = ThreadLocal.withInitial(() -> false); + + private final WorldServer world; + private final IChunkLoader chunkLoader; @@ -1069,6 +1071,9 @@ index 0000000000..2dfa59b204 + // Listen for when result is ready + final CompletableFuture future = new CompletableFuture<>(); + PendingChunkRequest request = pending.addListener(future, gen); ++ if (isChunkThread.get()) { ++ pending.loadTask.run(); ++ } + + if (isBlockingMain && pending.hasFinished) { + processChunkLoads(); @@ -1248,6 +1253,11 @@ index 0000000000..2dfa59b204 + } + } + ++ private Chunk generateChunkExecutor() { ++ isChunkThread.set(true); ++ isChunkGenThread.set(true); ++ return generateChunk(); ++ } + private Chunk generateChunk() { + synchronized (this) { + if (requests.get() <= 0) { @@ -1405,8 +1415,16 @@ index 0000000000..2dfa59b204 + if (loadTask == null) { + // Take care of a race condition in that a request could be cancelled after the synchronize + // on pendingChunks, but before a listener is added, which would erase these pending tasks. -+ genTask = generationExecutor.createPendingTask(this::generateChunk, taskPriority); -+ loadTask = EXECUTOR.submitTask(this, taskPriority); ++ if (shouldGenSync) { ++ genTask = generationExecutor.createPendingTask(this::generateChunk, taskPriority); ++ } else { ++ genTask = generationExecutor.createPendingTask(this::generateChunkExecutor, taskPriority); ++ } ++ loadTask = EXECUTOR.createPendingTask(this, taskPriority); ++ if (!isChunkThread.get()) { ++ // We will execute it outside of the synchronized context immediately after ++ EXECUTOR.submitTask(loadTask); ++ } + } + return new PendingChunkRequest(this, gen); + } @@ -1414,6 +1432,7 @@ index 0000000000..2dfa59b204 + + @Override + public void run() { ++ isChunkThread.set(true); + try { + if (!loadFinished(loadChunk(x, z))) { + return; @@ -1435,7 +1454,13 @@ index 0000000000..2dfa59b204 + mainThreadQueue.notify(); + } + } else { -+ generationExecutor.submitTask(genTask); ++ if (isChunkGenThread.get()) { ++ // ideally we should never run into 1 chunk generating another chunk... ++ // but if we do, let's apply same solution ++ genTask.run(); ++ } else { ++ generationExecutor.submitTask(genTask); ++ } + } + } + diff --git a/Spigot-Server-Patches/Detect-and-repair-corrupt-Region-Files.patch b/Spigot-Server-Patches/Detect-and-repair-corrupt-Region-Files.patch index 186b1ad5b8..8b787d2661 100644 --- a/Spigot-Server-Patches/Detect-and-repair-corrupt-Region-Files.patch +++ b/Spigot-Server-Patches/Detect-and-repair-corrupt-Region-Files.patch @@ -11,7 +11,7 @@ I don't know why mojang only checks for 4096, when anything less than 8192 is a But to be safe, it will attempt to back up the file. diff --git a/src/main/java/net/minecraft/server/RegionFile.java b/src/main/java/net/minecraft/server/RegionFile.java -index 5d2853b9ce..5c0d4f912a 100644 +index 5d2853b9ce..6a2edfd1e1 100644 --- a/src/main/java/net/minecraft/server/RegionFile.java +++ b/src/main/java/net/minecraft/server/RegionFile.java @@ -0,0 +0,0 @@ import javax.annotation.Nullable; @@ -63,9 +63,6 @@ index 5d2853b9ce..5c0d4f912a 100644 } + // Paper start -+ public void deleteChunk(int x, int z) { -+ deleteChunk(x + z * 32); -+ } + public synchronized void deleteChunk(int j1) { + backup(); + int k = offsets[j1];