Paper/Spigot-Server-Patches/0490-Allow-multiple-callbacks-to-schedule-for-Callback-Ex.patch
Aikar f37381ea8a
Optimize Network Manager to not need synchronization
Removes synchronization from sending packets
Makes normal packet sends no longer need to be wrapped and queued like it use to work.
Adds more packet queue immunities on top of keep alive to let the following scenarios go out
without delay:
  - Keep Alive
  - Chat
  - Kick
  - All of the packets during the Player Joined World event

Hoping that latter one helps join timeout issues more too for slow connections.

Removes processing packet queue off of main thread
  - for the few cases where it is allowed, order is not necessary nor
    should it even be happening concurrently in first place (handshaking/login/status)

Ensures packets sent asynchronously are dispatched on main thread

This helps ensure safety for ProtocolLib as packet listeners
are commonly accessing world state. This will allow you to schedule
a packet to be sent async, but itll be dispatched sync for packet
listeners to process.

This should solve some deadlock risks

This may provide a decent performance improvement because thread synchronization incurs a cache reset
so by avoiding ever entering a synchronized block, we get to avoid that, and packet sending is a really
hot activity.
2020-05-06 05:28:47 -04:00

62 lines
2.3 KiB
Diff

From c3224481d028cf472836c79ccfcba95e70979901 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Tue, 21 Apr 2020 03:51:53 -0400
Subject: [PATCH] Allow multiple callbacks to schedule for Callback Executor
ChunkMapDistance polls multiple entries for pendingChunkUpdates
Each of these have the potential to move a chunk in and out of
"Loaded" state, which will result in multiple callbacks being
needed within a single tick of ChunkMapDistance
Use an ArrayDeque to store this Queue
We make sure to also implement a pattern that is recursion safe too.
diff --git a/src/main/java/net/minecraft/server/PlayerChunkMap.java b/src/main/java/net/minecraft/server/PlayerChunkMap.java
index ea0086ceb64..bdf835397aa 100644
--- a/src/main/java/net/minecraft/server/PlayerChunkMap.java
+++ b/src/main/java/net/minecraft/server/PlayerChunkMap.java
@@ -87,24 +87,32 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
public final CallbackExecutor callbackExecutor = new CallbackExecutor();
public static final class CallbackExecutor implements java.util.concurrent.Executor, Runnable {
- private Runnable queued;
+ // Paper start - replace impl with recursive safe multi entry queue
+ // it's possible to schedule multiple tasks currently, so it's vital we change this impl
+ // If we recurse into the executor again, we will append to another queue, ensuring task order consistency
+ private java.util.ArrayDeque<Runnable> queued = new java.util.ArrayDeque<>();
@Override
public void execute(Runnable runnable) {
- if (queued != null) {
- throw new IllegalStateException("Already queued");
+ if (queued == null) {
+ queued = new java.util.ArrayDeque<>();
}
- queued = runnable;
+ queued.add(runnable);
}
@Override
public void run() {
- Runnable task = queued;
+ if (queued == null) {
+ return;
+ }
+ java.util.ArrayDeque<Runnable> queue = queued;
queued = null;
- if (task != null) {
+ Runnable task;
+ while ((task = queue.pollFirst()) != null) {
task.run();
}
}
+ // Paper end
};
// CraftBukkit end
--
2.26.2