From 79b64b314d1f17a1bc8e736ddfd6492e9160c481 Mon Sep 17 00:00:00 2001 From: Taiyou06 Date: Fri, 21 Feb 2025 16:14:36 +0100 Subject: [PATCH 1/5] just use netty event loop, lol lmao. --- ...write-queue-on-Connection.flushQueue.patch | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 leaf-server/minecraft-patches/features/0109-Rewrite-queue-on-Connection.flushQueue.patch diff --git a/leaf-server/minecraft-patches/features/0109-Rewrite-queue-on-Connection.flushQueue.patch b/leaf-server/minecraft-patches/features/0109-Rewrite-queue-on-Connection.flushQueue.patch new file mode 100644 index 00000000..bab32acf --- /dev/null +++ b/leaf-server/minecraft-patches/features/0109-Rewrite-queue-on-Connection.flushQueue.patch @@ -0,0 +1,75 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Taiyou06 +Date: Fri, 21 Feb 2025 15:52:42 +0100 +Subject: [PATCH] Rewrite queue on Connection.flushQueue + + +diff --git a/net/minecraft/network/Connection.java b/net/minecraft/network/Connection.java +index 7b78c0af4a83bd39a5bc2d6554cc677bd4c0c822..f4e535f3f7e9bcb6f0383e4704b8012cde7717a3 100644 +--- a/net/minecraft/network/Connection.java ++++ b/net/minecraft/network/Connection.java +@@ -35,6 +35,7 @@ import io.netty.handler.timeout.TimeoutException; + import java.net.InetSocketAddress; + import java.net.SocketAddress; + import java.nio.channels.ClosedChannelException; ++import java.util.ArrayDeque; + import java.util.Objects; + import java.util.Queue; + import java.util.concurrent.RejectedExecutionException; +@@ -85,7 +86,7 @@ public class Connection extends SimpleChannelInboundHandler> { + private static final ProtocolInfo INITIAL_PROTOCOL = HandshakeProtocols.SERVERBOUND; + private final PacketFlow receiving; + private volatile boolean sendLoginDisconnect = true; +- private final Queue pendingActions = Queues.newConcurrentLinkedQueue(); // Paper - Optimize network ++ private final Queue pendingActions = new ArrayDeque<>(); // Paper - Optimize network - Leaf optimize queue + public Channel channel; + public SocketAddress address; + // Spigot start +@@ -541,10 +542,9 @@ public class Connection extends SimpleChannelInboundHandler> { + if (io.papermc.paper.util.MCUtil.isMainThread()) { + return this.processQueue(); + } else if (this.isPending) { +- // Should only happen during login/status stages +- synchronized (this.pendingActions) { +- return this.processQueue(); +- } ++ // Submit to the event loop to ensure thread confinement ++ this.channel.eventLoop().execute(() -> this.processQueue()); ++ return false; + } + return false; + } +@@ -554,29 +554,17 @@ public class Connection extends SimpleChannelInboundHandler> { + return true; + } + +- // If we are on main, we are safe here in that nothing else should be processing queue off main anymore +- // But if we are not on main due to login/status, the parent is synchronized on packetQueue +- final java.util.Iterator iterator = this.pendingActions.iterator(); +- while (iterator.hasNext()) { +- final WrappedConsumer queued = iterator.next(); // poll -> peek +- +- // Fix NPE (Spigot bug caused by handleDisconnection()) +- if (queued == null) { +- return true; +- } +- +- if (queued.isConsumed()) { +- continue; +- } +- ++ WrappedConsumer queued; ++ while ((queued = this.pendingActions.poll()) != null) { + if (queued instanceof PacketSendAction packetSendAction) { + final Packet packet = packetSendAction.packet; + if (!packet.isReady()) { ++ // Re-add to the front and exit ++ this.pendingActions.add(queued); + return false; + } + } + +- iterator.remove(); + if (queued.tryMarkConsumed()) { + queued.accept(this); + } From d5f3cba67cbae3ac44869cba4b235358071f13a7 Mon Sep 17 00:00:00 2001 From: Taiyou06 Date: Sat, 22 Feb 2025 10:17:03 +0100 Subject: [PATCH 2/5] rebase 1.21.4 --- ...ue.patch => 0110-Rewrite-queue-on-Connection.flushQueue.patch} | 0 ...11-PaperPR-Fix-MC-117075-Block-Entities-Unload-Lag-Spik.patch} | 0 ...atch => 0112-Sepals-Rearrange-the-attackable-conditions.patch} | 0 ...13-SparklyPaper-Skip-dirty-stats-copy-when-requesting-p.patch} | 0 ...14-SparklyPaper-Reset-dirty-flag-when-loading-maps-from.patch} | 0 ...h => 0115-Optimize-checking-nearby-players-for-spawning.patch} | 0 ...-block-check.patch => 0116-Cache-supporting-block-check.patch} | 0 ...17-Avoid-useless-deque-clear-on-LevelTicks-cleanupAfter.patch} | 0 ...18-Replace-brain-activity-maps-with-optimized-collectio.patch} | 0 ...m-in-villagers.patch => 0119-Remove-stream-in-villagers.patch} | 0 ...ager-sensor.patch => 0120-Optimize-baby-villager-sensor.patch} | 0 ...Only-player-pushable.patch => 0121-Only-player-pushable.patch} | 0 ....patch => 0122-Remove-iterators-from-Inventory-contains.patch} | 0 ...ain-Behaviour.patch => 0123-Alternative-Brain-Behaviour.patch} | 0 ...patch => 0124-Cache-eligible-players-for-despawn-checks.patch} | 0 ...Player.patch => 0125-Slightly-optimise-getNearestPlayer.patch} | 0 16 files changed, 0 insertions(+), 0 deletions(-) rename leaf-server/minecraft-patches/features/{0109-Rewrite-queue-on-Connection.flushQueue.patch => 0110-Rewrite-queue-on-Connection.flushQueue.patch} (100%) rename leaf-server/minecraft-patches/features/{0110-PaperPR-Fix-MC-117075-Block-Entities-Unload-Lag-Spik.patch => 0111-PaperPR-Fix-MC-117075-Block-Entities-Unload-Lag-Spik.patch} (100%) rename leaf-server/minecraft-patches/features/{0111-Sepals-Rearrange-the-attackable-conditions.patch => 0112-Sepals-Rearrange-the-attackable-conditions.patch} (100%) rename leaf-server/minecraft-patches/features/{0112-SparklyPaper-Skip-dirty-stats-copy-when-requesting-p.patch => 0113-SparklyPaper-Skip-dirty-stats-copy-when-requesting-p.patch} (100%) rename leaf-server/minecraft-patches/features/{0113-SparklyPaper-Reset-dirty-flag-when-loading-maps-from.patch => 0114-SparklyPaper-Reset-dirty-flag-when-loading-maps-from.patch} (100%) rename leaf-server/minecraft-patches/features/{0114-Optimize-checking-nearby-players-for-spawning.patch => 0115-Optimize-checking-nearby-players-for-spawning.patch} (100%) rename leaf-server/minecraft-patches/features/{0115-Cache-supporting-block-check.patch => 0116-Cache-supporting-block-check.patch} (100%) rename leaf-server/minecraft-patches/features/{0116-Avoid-useless-deque-clear-on-LevelTicks-cleanupAfter.patch => 0117-Avoid-useless-deque-clear-on-LevelTicks-cleanupAfter.patch} (100%) rename leaf-server/minecraft-patches/features/{0117-Replace-brain-activity-maps-with-optimized-collectio.patch => 0118-Replace-brain-activity-maps-with-optimized-collectio.patch} (100%) rename leaf-server/minecraft-patches/features/{0118-Remove-stream-in-villagers.patch => 0119-Remove-stream-in-villagers.patch} (100%) rename leaf-server/minecraft-patches/features/{0119-Optimize-baby-villager-sensor.patch => 0120-Optimize-baby-villager-sensor.patch} (100%) rename leaf-server/minecraft-patches/features/{0120-Only-player-pushable.patch => 0121-Only-player-pushable.patch} (100%) rename leaf-server/minecraft-patches/features/{0121-Remove-iterators-from-Inventory-contains.patch => 0122-Remove-iterators-from-Inventory-contains.patch} (100%) rename leaf-server/minecraft-patches/features/{0122-Alternative-Brain-Behaviour.patch => 0123-Alternative-Brain-Behaviour.patch} (100%) rename leaf-server/minecraft-patches/features/{0123-Cache-eligible-players-for-despawn-checks.patch => 0124-Cache-eligible-players-for-despawn-checks.patch} (100%) rename leaf-server/minecraft-patches/features/{0124-Slightly-optimise-getNearestPlayer.patch => 0125-Slightly-optimise-getNearestPlayer.patch} (100%) diff --git a/leaf-server/minecraft-patches/features/0109-Rewrite-queue-on-Connection.flushQueue.patch b/leaf-server/minecraft-patches/features/0110-Rewrite-queue-on-Connection.flushQueue.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0109-Rewrite-queue-on-Connection.flushQueue.patch rename to leaf-server/minecraft-patches/features/0110-Rewrite-queue-on-Connection.flushQueue.patch diff --git a/leaf-server/minecraft-patches/features/0110-PaperPR-Fix-MC-117075-Block-Entities-Unload-Lag-Spik.patch b/leaf-server/minecraft-patches/features/0111-PaperPR-Fix-MC-117075-Block-Entities-Unload-Lag-Spik.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0110-PaperPR-Fix-MC-117075-Block-Entities-Unload-Lag-Spik.patch rename to leaf-server/minecraft-patches/features/0111-PaperPR-Fix-MC-117075-Block-Entities-Unload-Lag-Spik.patch diff --git a/leaf-server/minecraft-patches/features/0111-Sepals-Rearrange-the-attackable-conditions.patch b/leaf-server/minecraft-patches/features/0112-Sepals-Rearrange-the-attackable-conditions.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0111-Sepals-Rearrange-the-attackable-conditions.patch rename to leaf-server/minecraft-patches/features/0112-Sepals-Rearrange-the-attackable-conditions.patch diff --git a/leaf-server/minecraft-patches/features/0112-SparklyPaper-Skip-dirty-stats-copy-when-requesting-p.patch b/leaf-server/minecraft-patches/features/0113-SparklyPaper-Skip-dirty-stats-copy-when-requesting-p.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0112-SparklyPaper-Skip-dirty-stats-copy-when-requesting-p.patch rename to leaf-server/minecraft-patches/features/0113-SparklyPaper-Skip-dirty-stats-copy-when-requesting-p.patch diff --git a/leaf-server/minecraft-patches/features/0113-SparklyPaper-Reset-dirty-flag-when-loading-maps-from.patch b/leaf-server/minecraft-patches/features/0114-SparklyPaper-Reset-dirty-flag-when-loading-maps-from.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0113-SparklyPaper-Reset-dirty-flag-when-loading-maps-from.patch rename to leaf-server/minecraft-patches/features/0114-SparklyPaper-Reset-dirty-flag-when-loading-maps-from.patch diff --git a/leaf-server/minecraft-patches/features/0114-Optimize-checking-nearby-players-for-spawning.patch b/leaf-server/minecraft-patches/features/0115-Optimize-checking-nearby-players-for-spawning.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0114-Optimize-checking-nearby-players-for-spawning.patch rename to leaf-server/minecraft-patches/features/0115-Optimize-checking-nearby-players-for-spawning.patch diff --git a/leaf-server/minecraft-patches/features/0115-Cache-supporting-block-check.patch b/leaf-server/minecraft-patches/features/0116-Cache-supporting-block-check.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0115-Cache-supporting-block-check.patch rename to leaf-server/minecraft-patches/features/0116-Cache-supporting-block-check.patch diff --git a/leaf-server/minecraft-patches/features/0116-Avoid-useless-deque-clear-on-LevelTicks-cleanupAfter.patch b/leaf-server/minecraft-patches/features/0117-Avoid-useless-deque-clear-on-LevelTicks-cleanupAfter.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0116-Avoid-useless-deque-clear-on-LevelTicks-cleanupAfter.patch rename to leaf-server/minecraft-patches/features/0117-Avoid-useless-deque-clear-on-LevelTicks-cleanupAfter.patch diff --git a/leaf-server/minecraft-patches/features/0117-Replace-brain-activity-maps-with-optimized-collectio.patch b/leaf-server/minecraft-patches/features/0118-Replace-brain-activity-maps-with-optimized-collectio.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0117-Replace-brain-activity-maps-with-optimized-collectio.patch rename to leaf-server/minecraft-patches/features/0118-Replace-brain-activity-maps-with-optimized-collectio.patch diff --git a/leaf-server/minecraft-patches/features/0118-Remove-stream-in-villagers.patch b/leaf-server/minecraft-patches/features/0119-Remove-stream-in-villagers.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0118-Remove-stream-in-villagers.patch rename to leaf-server/minecraft-patches/features/0119-Remove-stream-in-villagers.patch diff --git a/leaf-server/minecraft-patches/features/0119-Optimize-baby-villager-sensor.patch b/leaf-server/minecraft-patches/features/0120-Optimize-baby-villager-sensor.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0119-Optimize-baby-villager-sensor.patch rename to leaf-server/minecraft-patches/features/0120-Optimize-baby-villager-sensor.patch diff --git a/leaf-server/minecraft-patches/features/0120-Only-player-pushable.patch b/leaf-server/minecraft-patches/features/0121-Only-player-pushable.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0120-Only-player-pushable.patch rename to leaf-server/minecraft-patches/features/0121-Only-player-pushable.patch diff --git a/leaf-server/minecraft-patches/features/0121-Remove-iterators-from-Inventory-contains.patch b/leaf-server/minecraft-patches/features/0122-Remove-iterators-from-Inventory-contains.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0121-Remove-iterators-from-Inventory-contains.patch rename to leaf-server/minecraft-patches/features/0122-Remove-iterators-from-Inventory-contains.patch diff --git a/leaf-server/minecraft-patches/features/0122-Alternative-Brain-Behaviour.patch b/leaf-server/minecraft-patches/features/0123-Alternative-Brain-Behaviour.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0122-Alternative-Brain-Behaviour.patch rename to leaf-server/minecraft-patches/features/0123-Alternative-Brain-Behaviour.patch diff --git a/leaf-server/minecraft-patches/features/0123-Cache-eligible-players-for-despawn-checks.patch b/leaf-server/minecraft-patches/features/0124-Cache-eligible-players-for-despawn-checks.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0123-Cache-eligible-players-for-despawn-checks.patch rename to leaf-server/minecraft-patches/features/0124-Cache-eligible-players-for-despawn-checks.patch diff --git a/leaf-server/minecraft-patches/features/0124-Slightly-optimise-getNearestPlayer.patch b/leaf-server/minecraft-patches/features/0125-Slightly-optimise-getNearestPlayer.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0124-Slightly-optimise-getNearestPlayer.patch rename to leaf-server/minecraft-patches/features/0125-Slightly-optimise-getNearestPlayer.patch From 61754c8ffd25d9fae97cdc3cc650126138b04a61 Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Sat, 22 Feb 2025 04:49:13 -0500 Subject: [PATCH 3/5] [ci skip] cleanup --- ...MC-117075-Block-Entities-Unload-Lag-Spik.patch} | 0 ...pals-Rearrange-the-attackable-conditions.patch} | 0 ...-Skip-dirty-stats-copy-when-requesting-p.patch} | 0 ...-Reset-dirty-flag-when-loading-maps-from.patch} | 0 ...ize-checking-nearby-players-for-spawning.patch} | 0 ...tch => 0115-Cache-supporting-block-check.patch} | 0 ...s-deque-clear-on-LevelTicks-cleanupAfter.patch} | 0 ...n-activity-maps-with-optimized-collectio.patch} | 0 ...patch => 0118-Remove-stream-in-villagers.patch} | 0 ...ch => 0119-Optimize-baby-villager-sensor.patch} | 0 ...hable.patch => 0120-Only-player-pushable.patch} | 0 ...Remove-iterators-from-Inventory-contains.patch} | 0 ...atch => 0122-Alternative-Brain-Behaviour.patch} | 0 ...ache-eligible-players-for-despawn-checks.patch} | 0 ... 0124-Slightly-optimise-getNearestPlayer.patch} | 0 ...5-Rewrite-queue-on-Connection.flushQueue.patch} | 14 +++++++++----- 16 files changed, 9 insertions(+), 5 deletions(-) rename leaf-server/minecraft-patches/features/{0111-PaperPR-Fix-MC-117075-Block-Entities-Unload-Lag-Spik.patch => 0110-PaperPR-Fix-MC-117075-Block-Entities-Unload-Lag-Spik.patch} (100%) rename leaf-server/minecraft-patches/features/{0112-Sepals-Rearrange-the-attackable-conditions.patch => 0111-Sepals-Rearrange-the-attackable-conditions.patch} (100%) rename leaf-server/minecraft-patches/features/{0113-SparklyPaper-Skip-dirty-stats-copy-when-requesting-p.patch => 0112-SparklyPaper-Skip-dirty-stats-copy-when-requesting-p.patch} (100%) rename leaf-server/minecraft-patches/features/{0114-SparklyPaper-Reset-dirty-flag-when-loading-maps-from.patch => 0113-SparklyPaper-Reset-dirty-flag-when-loading-maps-from.patch} (100%) rename leaf-server/minecraft-patches/features/{0115-Optimize-checking-nearby-players-for-spawning.patch => 0114-Optimize-checking-nearby-players-for-spawning.patch} (100%) rename leaf-server/minecraft-patches/features/{0116-Cache-supporting-block-check.patch => 0115-Cache-supporting-block-check.patch} (100%) rename leaf-server/minecraft-patches/features/{0117-Avoid-useless-deque-clear-on-LevelTicks-cleanupAfter.patch => 0116-Avoid-useless-deque-clear-on-LevelTicks-cleanupAfter.patch} (100%) rename leaf-server/minecraft-patches/features/{0118-Replace-brain-activity-maps-with-optimized-collectio.patch => 0117-Replace-brain-activity-maps-with-optimized-collectio.patch} (100%) rename leaf-server/minecraft-patches/features/{0119-Remove-stream-in-villagers.patch => 0118-Remove-stream-in-villagers.patch} (100%) rename leaf-server/minecraft-patches/features/{0120-Optimize-baby-villager-sensor.patch => 0119-Optimize-baby-villager-sensor.patch} (100%) rename leaf-server/minecraft-patches/features/{0121-Only-player-pushable.patch => 0120-Only-player-pushable.patch} (100%) rename leaf-server/minecraft-patches/features/{0122-Remove-iterators-from-Inventory-contains.patch => 0121-Remove-iterators-from-Inventory-contains.patch} (100%) rename leaf-server/minecraft-patches/features/{0123-Alternative-Brain-Behaviour.patch => 0122-Alternative-Brain-Behaviour.patch} (100%) rename leaf-server/minecraft-patches/features/{0124-Cache-eligible-players-for-despawn-checks.patch => 0123-Cache-eligible-players-for-despawn-checks.patch} (100%) rename leaf-server/minecraft-patches/features/{0125-Slightly-optimise-getNearestPlayer.patch => 0124-Slightly-optimise-getNearestPlayer.patch} (100%) rename leaf-server/minecraft-patches/features/{0110-Rewrite-queue-on-Connection.flushQueue.patch => 0125-Rewrite-queue-on-Connection.flushQueue.patch} (81%) diff --git a/leaf-server/minecraft-patches/features/0111-PaperPR-Fix-MC-117075-Block-Entities-Unload-Lag-Spik.patch b/leaf-server/minecraft-patches/features/0110-PaperPR-Fix-MC-117075-Block-Entities-Unload-Lag-Spik.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0111-PaperPR-Fix-MC-117075-Block-Entities-Unload-Lag-Spik.patch rename to leaf-server/minecraft-patches/features/0110-PaperPR-Fix-MC-117075-Block-Entities-Unload-Lag-Spik.patch diff --git a/leaf-server/minecraft-patches/features/0112-Sepals-Rearrange-the-attackable-conditions.patch b/leaf-server/minecraft-patches/features/0111-Sepals-Rearrange-the-attackable-conditions.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0112-Sepals-Rearrange-the-attackable-conditions.patch rename to leaf-server/minecraft-patches/features/0111-Sepals-Rearrange-the-attackable-conditions.patch diff --git a/leaf-server/minecraft-patches/features/0113-SparklyPaper-Skip-dirty-stats-copy-when-requesting-p.patch b/leaf-server/minecraft-patches/features/0112-SparklyPaper-Skip-dirty-stats-copy-when-requesting-p.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0113-SparklyPaper-Skip-dirty-stats-copy-when-requesting-p.patch rename to leaf-server/minecraft-patches/features/0112-SparklyPaper-Skip-dirty-stats-copy-when-requesting-p.patch diff --git a/leaf-server/minecraft-patches/features/0114-SparklyPaper-Reset-dirty-flag-when-loading-maps-from.patch b/leaf-server/minecraft-patches/features/0113-SparklyPaper-Reset-dirty-flag-when-loading-maps-from.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0114-SparklyPaper-Reset-dirty-flag-when-loading-maps-from.patch rename to leaf-server/minecraft-patches/features/0113-SparklyPaper-Reset-dirty-flag-when-loading-maps-from.patch diff --git a/leaf-server/minecraft-patches/features/0115-Optimize-checking-nearby-players-for-spawning.patch b/leaf-server/minecraft-patches/features/0114-Optimize-checking-nearby-players-for-spawning.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0115-Optimize-checking-nearby-players-for-spawning.patch rename to leaf-server/minecraft-patches/features/0114-Optimize-checking-nearby-players-for-spawning.patch diff --git a/leaf-server/minecraft-patches/features/0116-Cache-supporting-block-check.patch b/leaf-server/minecraft-patches/features/0115-Cache-supporting-block-check.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0116-Cache-supporting-block-check.patch rename to leaf-server/minecraft-patches/features/0115-Cache-supporting-block-check.patch diff --git a/leaf-server/minecraft-patches/features/0117-Avoid-useless-deque-clear-on-LevelTicks-cleanupAfter.patch b/leaf-server/minecraft-patches/features/0116-Avoid-useless-deque-clear-on-LevelTicks-cleanupAfter.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0117-Avoid-useless-deque-clear-on-LevelTicks-cleanupAfter.patch rename to leaf-server/minecraft-patches/features/0116-Avoid-useless-deque-clear-on-LevelTicks-cleanupAfter.patch diff --git a/leaf-server/minecraft-patches/features/0118-Replace-brain-activity-maps-with-optimized-collectio.patch b/leaf-server/minecraft-patches/features/0117-Replace-brain-activity-maps-with-optimized-collectio.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0118-Replace-brain-activity-maps-with-optimized-collectio.patch rename to leaf-server/minecraft-patches/features/0117-Replace-brain-activity-maps-with-optimized-collectio.patch diff --git a/leaf-server/minecraft-patches/features/0119-Remove-stream-in-villagers.patch b/leaf-server/minecraft-patches/features/0118-Remove-stream-in-villagers.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0119-Remove-stream-in-villagers.patch rename to leaf-server/minecraft-patches/features/0118-Remove-stream-in-villagers.patch diff --git a/leaf-server/minecraft-patches/features/0120-Optimize-baby-villager-sensor.patch b/leaf-server/minecraft-patches/features/0119-Optimize-baby-villager-sensor.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0120-Optimize-baby-villager-sensor.patch rename to leaf-server/minecraft-patches/features/0119-Optimize-baby-villager-sensor.patch diff --git a/leaf-server/minecraft-patches/features/0121-Only-player-pushable.patch b/leaf-server/minecraft-patches/features/0120-Only-player-pushable.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0121-Only-player-pushable.patch rename to leaf-server/minecraft-patches/features/0120-Only-player-pushable.patch diff --git a/leaf-server/minecraft-patches/features/0122-Remove-iterators-from-Inventory-contains.patch b/leaf-server/minecraft-patches/features/0121-Remove-iterators-from-Inventory-contains.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0122-Remove-iterators-from-Inventory-contains.patch rename to leaf-server/minecraft-patches/features/0121-Remove-iterators-from-Inventory-contains.patch diff --git a/leaf-server/minecraft-patches/features/0123-Alternative-Brain-Behaviour.patch b/leaf-server/minecraft-patches/features/0122-Alternative-Brain-Behaviour.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0123-Alternative-Brain-Behaviour.patch rename to leaf-server/minecraft-patches/features/0122-Alternative-Brain-Behaviour.patch diff --git a/leaf-server/minecraft-patches/features/0124-Cache-eligible-players-for-despawn-checks.patch b/leaf-server/minecraft-patches/features/0123-Cache-eligible-players-for-despawn-checks.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0124-Cache-eligible-players-for-despawn-checks.patch rename to leaf-server/minecraft-patches/features/0123-Cache-eligible-players-for-despawn-checks.patch diff --git a/leaf-server/minecraft-patches/features/0125-Slightly-optimise-getNearestPlayer.patch b/leaf-server/minecraft-patches/features/0124-Slightly-optimise-getNearestPlayer.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0125-Slightly-optimise-getNearestPlayer.patch rename to leaf-server/minecraft-patches/features/0124-Slightly-optimise-getNearestPlayer.patch diff --git a/leaf-server/minecraft-patches/features/0110-Rewrite-queue-on-Connection.flushQueue.patch b/leaf-server/minecraft-patches/features/0125-Rewrite-queue-on-Connection.flushQueue.patch similarity index 81% rename from leaf-server/minecraft-patches/features/0110-Rewrite-queue-on-Connection.flushQueue.patch rename to leaf-server/minecraft-patches/features/0125-Rewrite-queue-on-Connection.flushQueue.patch index bab32acf..3f23cf60 100644 --- a/leaf-server/minecraft-patches/features/0110-Rewrite-queue-on-Connection.flushQueue.patch +++ b/leaf-server/minecraft-patches/features/0125-Rewrite-queue-on-Connection.flushQueue.patch @@ -5,7 +5,7 @@ Subject: [PATCH] Rewrite queue on Connection.flushQueue diff --git a/net/minecraft/network/Connection.java b/net/minecraft/network/Connection.java -index 7b78c0af4a83bd39a5bc2d6554cc677bd4c0c822..f4e535f3f7e9bcb6f0383e4704b8012cde7717a3 100644 +index 7b78c0af4a83bd39a5bc2d6554cc677bd4c0c822..eb9d99d657ae01c36af7cf4a75e206b917a14c39 100644 --- a/net/minecraft/network/Connection.java +++ b/net/minecraft/network/Connection.java @@ -35,6 +35,7 @@ import io.netty.handler.timeout.TimeoutException; @@ -21,11 +21,11 @@ index 7b78c0af4a83bd39a5bc2d6554cc677bd4c0c822..f4e535f3f7e9bcb6f0383e4704b8012c private final PacketFlow receiving; private volatile boolean sendLoginDisconnect = true; - private final Queue pendingActions = Queues.newConcurrentLinkedQueue(); // Paper - Optimize network -+ private final Queue pendingActions = new ArrayDeque<>(); // Paper - Optimize network - Leaf optimize queue ++ private final Queue pendingActions = new ArrayDeque<>(); // Paper - Optimize network // Leaf - Rewrite queue on Connection.flushQueue public Channel channel; public SocketAddress address; // Spigot start -@@ -541,10 +542,9 @@ public class Connection extends SimpleChannelInboundHandler> { +@@ -541,10 +542,11 @@ public class Connection extends SimpleChannelInboundHandler> { if (io.papermc.paper.util.MCUtil.isMainThread()) { return this.processQueue(); } else if (this.isPending) { @@ -33,13 +33,15 @@ index 7b78c0af4a83bd39a5bc2d6554cc677bd4c0c822..f4e535f3f7e9bcb6f0383e4704b8012c - synchronized (this.pendingActions) { - return this.processQueue(); - } ++ // Leaf start - Rewrite queue on Connection.flushQueue + // Submit to the event loop to ensure thread confinement -+ this.channel.eventLoop().execute(() -> this.processQueue()); ++ this.channel.eventLoop().execute(this::processQueue); + return false; ++ // Leaf end - Rewrite queue on Connection.flushQueue } return false; } -@@ -554,29 +554,17 @@ public class Connection extends SimpleChannelInboundHandler> { +@@ -554,29 +556,19 @@ public class Connection extends SimpleChannelInboundHandler> { return true; } @@ -58,6 +60,7 @@ index 7b78c0af4a83bd39a5bc2d6554cc677bd4c0c822..f4e535f3f7e9bcb6f0383e4704b8012c - continue; - } - ++ // Leaf start - Rewrite queue on Connection.flushQueue + WrappedConsumer queued; + while ((queued = this.pendingActions.poll()) != null) { if (queued instanceof PacketSendAction packetSendAction) { @@ -70,6 +73,7 @@ index 7b78c0af4a83bd39a5bc2d6554cc677bd4c0c822..f4e535f3f7e9bcb6f0383e4704b8012c } - iterator.remove(); ++ // Leaf end - Rewrite queue on Connection.flushQueue if (queued.tryMarkConsumed()) { queued.accept(this); } From 6185246a7818a1e998bb2cc8f65b93c32165229c Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Mon, 24 Feb 2025 13:56:27 -0500 Subject: [PATCH 4/5] [ci skip] cleanup --- ...ewrite-queue-on-Connection.flushQueue.patch | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/leaf-server/minecraft-patches/features/0125-Rewrite-queue-on-Connection.flushQueue.patch b/leaf-server/minecraft-patches/features/0125-Rewrite-queue-on-Connection.flushQueue.patch index 3f23cf60..4390b4f0 100644 --- a/leaf-server/minecraft-patches/features/0125-Rewrite-queue-on-Connection.flushQueue.patch +++ b/leaf-server/minecraft-patches/features/0125-Rewrite-queue-on-Connection.flushQueue.patch @@ -5,27 +5,19 @@ Subject: [PATCH] Rewrite queue on Connection.flushQueue diff --git a/net/minecraft/network/Connection.java b/net/minecraft/network/Connection.java -index 7b78c0af4a83bd39a5bc2d6554cc677bd4c0c822..eb9d99d657ae01c36af7cf4a75e206b917a14c39 100644 +index 7b78c0af4a83bd39a5bc2d6554cc677bd4c0c822..5498a5bc1e4af413569a9f3b280c561884c66718 100644 --- a/net/minecraft/network/Connection.java +++ b/net/minecraft/network/Connection.java -@@ -35,6 +35,7 @@ import io.netty.handler.timeout.TimeoutException; - import java.net.InetSocketAddress; - import java.net.SocketAddress; - import java.nio.channels.ClosedChannelException; -+import java.util.ArrayDeque; - import java.util.Objects; - import java.util.Queue; - import java.util.concurrent.RejectedExecutionException; -@@ -85,7 +86,7 @@ public class Connection extends SimpleChannelInboundHandler> { +@@ -85,7 +85,7 @@ public class Connection extends SimpleChannelInboundHandler> { private static final ProtocolInfo INITIAL_PROTOCOL = HandshakeProtocols.SERVERBOUND; private final PacketFlow receiving; private volatile boolean sendLoginDisconnect = true; - private final Queue pendingActions = Queues.newConcurrentLinkedQueue(); // Paper - Optimize network -+ private final Queue pendingActions = new ArrayDeque<>(); // Paper - Optimize network // Leaf - Rewrite queue on Connection.flushQueue ++ private final Queue pendingActions = new java.util.ArrayDeque<>(); // Paper - Optimize network // Leaf - Rewrite queue on Connection.flushQueue public Channel channel; public SocketAddress address; // Spigot start -@@ -541,10 +542,11 @@ public class Connection extends SimpleChannelInboundHandler> { +@@ -541,10 +541,11 @@ public class Connection extends SimpleChannelInboundHandler> { if (io.papermc.paper.util.MCUtil.isMainThread()) { return this.processQueue(); } else if (this.isPending) { @@ -41,7 +33,7 @@ index 7b78c0af4a83bd39a5bc2d6554cc677bd4c0c822..eb9d99d657ae01c36af7cf4a75e206b9 } return false; } -@@ -554,29 +556,19 @@ public class Connection extends SimpleChannelInboundHandler> { +@@ -554,29 +555,19 @@ public class Connection extends SimpleChannelInboundHandler> { return true; } From ae5cd82c1254d1859a3dc3f96a380bbe239eaa63 Mon Sep 17 00:00:00 2001 From: Taiyou06 Date: Wed, 5 Mar 2025 15:37:38 +0100 Subject: [PATCH 5/5] add a toggle --- ...write-queue-on-Connection.flushQueue.patch | 102 +++++++++++++----- ...-t-block-main-thread-in-Connection-.patch} | 2 +- .../network/ConnectionFlushQueueRewrite.java | 30 ++++++ 3 files changed, 105 insertions(+), 29 deletions(-) rename leaf-server/minecraft-patches/features/{0125-ShreddedPaper-Don-t-block-main-thread-in-Connection-.patch => 0126-ShreddedPaper-Don-t-block-main-thread-in-Connection-.patch} (91%) create mode 100644 leaf-server/src/main/java/org/dreeam/leaf/config/modules/network/ConnectionFlushQueueRewrite.java diff --git a/leaf-server/minecraft-patches/features/0125-Rewrite-queue-on-Connection.flushQueue.patch b/leaf-server/minecraft-patches/features/0125-Rewrite-queue-on-Connection.flushQueue.patch index 4390b4f0..eed84175 100644 --- a/leaf-server/minecraft-patches/features/0125-Rewrite-queue-on-Connection.flushQueue.patch +++ b/leaf-server/minecraft-patches/features/0125-Rewrite-queue-on-Connection.flushQueue.patch @@ -5,7 +5,7 @@ Subject: [PATCH] Rewrite queue on Connection.flushQueue diff --git a/net/minecraft/network/Connection.java b/net/minecraft/network/Connection.java -index 7b78c0af4a83bd39a5bc2d6554cc677bd4c0c822..5498a5bc1e4af413569a9f3b280c561884c66718 100644 +index 7b78c0af4a83bd39a5bc2d6554cc677bd4c0c822..570a6b844319491b1e07df0a3e55d0b29533c649 100644 --- a/net/minecraft/network/Connection.java +++ b/net/minecraft/network/Connection.java @@ -85,7 +85,7 @@ public class Connection extends SimpleChannelInboundHandler> { @@ -13,27 +13,32 @@ index 7b78c0af4a83bd39a5bc2d6554cc677bd4c0c822..5498a5bc1e4af413569a9f3b280c5618 private final PacketFlow receiving; private volatile boolean sendLoginDisconnect = true; - private final Queue pendingActions = Queues.newConcurrentLinkedQueue(); // Paper - Optimize network -+ private final Queue pendingActions = new java.util.ArrayDeque<>(); // Paper - Optimize network // Leaf - Rewrite queue on Connection.flushQueue ++ private final Queue pendingActions = org.dreeam.leaf.config.modules.network.ConnectionFlushQueueRewrite.enabled ? new java.util.ArrayDeque<>() : Queues.newConcurrentLinkedQueue(); // Paper - Optimize network // Leaf - Rewrite queue on Connection.flushQueue public Channel channel; public SocketAddress address; // Spigot start -@@ -541,10 +541,11 @@ public class Connection extends SimpleChannelInboundHandler> { +@@ -541,9 +541,17 @@ public class Connection extends SimpleChannelInboundHandler> { if (io.papermc.paper.util.MCUtil.isMainThread()) { return this.processQueue(); } else if (this.isPending) { - // Should only happen during login/status stages - synchronized (this.pendingActions) { - return this.processQueue(); -- } -+ // Leaf start - Rewrite queue on Connection.flushQueue -+ // Submit to the event loop to ensure thread confinement -+ this.channel.eventLoop().execute(this::processQueue); -+ return false; -+ // Leaf end - Rewrite queue on Connection.flushQueue ++ if (org.dreeam.leaf.config.modules.network.ConnectionFlushQueueRewrite.enabled) { ++ // Leaf start - Rewrite queue on Connection.flushQueue ++ // Submit to the event loop to ensure thread confinement ++ this.channel.eventLoop().execute(this::processQueue); ++ return false; ++ // Leaf end - Rewrite queue on Connection.flushQueue ++ } else { ++ // Original Paper behavior ++ synchronized (this.pendingActions) { ++ return this.processQueue(); ++ } + } } return false; - } -@@ -554,29 +555,19 @@ public class Connection extends SimpleChannelInboundHandler> { +@@ -554,36 +562,56 @@ public class Connection extends SimpleChannelInboundHandler> { return true; } @@ -42,30 +47,71 @@ index 7b78c0af4a83bd39a5bc2d6554cc677bd4c0c822..5498a5bc1e4af413569a9f3b280c5618 - final java.util.Iterator iterator = this.pendingActions.iterator(); - while (iterator.hasNext()) { - final WrappedConsumer queued = iterator.next(); // poll -> peek -- ++ if (org.dreeam.leaf.config.modules.network.ConnectionFlushQueueRewrite.enabled) { ++ // Leaf start - Rewrite queue on Connection.flushQueue ++ WrappedConsumer queued; ++ while ((queued = this.pendingActions.poll()) != null) { ++ if (queued instanceof PacketSendAction packetSendAction) { ++ final Packet packet = packetSendAction.packet; ++ if (!packet.isReady()) { ++ // Re-add to the front and exit ++ this.pendingActions.add(queued); ++ return false; ++ } ++ } + - // Fix NPE (Spigot bug caused by handleDisconnection()) - if (queued == null) { - return true; -- } -- ++ if (queued.tryMarkConsumed()) { ++ queued.accept(this); ++ } + } ++ // Leaf end - Rewrite queue on Connection.flushQueue ++ } else { ++ // If we are on main, we are safe here in that nothing else should be processing queue off main anymore ++ // But if we are not on main due to login/status, the parent is synchronized on packetQueue ++ final java.util.Iterator iterator = this.pendingActions.iterator(); ++ while (iterator.hasNext()) { ++ final WrappedConsumer queued = iterator.next(); // poll -> peek ++ ++ // Fix NPE (Spigot bug caused by handleDisconnection()) ++ if (queued == null) { ++ return true; ++ } + - if (queued.isConsumed()) { - continue; - } -- -+ // Leaf start - Rewrite queue on Connection.flushQueue -+ WrappedConsumer queued; -+ while ((queued = this.pendingActions.poll()) != null) { - if (queued instanceof PacketSendAction packetSendAction) { - final Packet packet = packetSendAction.packet; - if (!packet.isReady()) { -+ // Re-add to the front and exit -+ this.pendingActions.add(queued); - return false; ++ if (queued.isConsumed()) { ++ continue; ++ } + +- if (queued instanceof PacketSendAction packetSendAction) { +- final Packet packet = packetSendAction.packet; +- if (!packet.isReady()) { +- return false; ++ if (queued instanceof PacketSendAction packetSendAction) { ++ final Packet packet = packetSendAction.packet; ++ if (!packet.isReady()) { ++ return false; ++ } } - } +- } - iterator.remove(); -+ // Leaf end - Rewrite queue on Connection.flushQueue - if (queued.tryMarkConsumed()) { - queued.accept(this); +- if (queued.tryMarkConsumed()) { +- queued.accept(this); ++ iterator.remove(); ++ if (queued.tryMarkConsumed()) { ++ queued.accept(this); ++ } } + } + return true; + } +- // Paper end - Optimize network ++// Paper end - Optimize network + + private static final int MAX_PER_TICK = io.papermc.paper.configuration.GlobalConfiguration.get().misc.maxJoinsPerTick; // Paper - Buffer joins to world + private static int joinAttemptsThisTick; // Paper - Buffer joins to world diff --git a/leaf-server/minecraft-patches/features/0125-ShreddedPaper-Don-t-block-main-thread-in-Connection-.patch b/leaf-server/minecraft-patches/features/0126-ShreddedPaper-Don-t-block-main-thread-in-Connection-.patch similarity index 91% rename from leaf-server/minecraft-patches/features/0125-ShreddedPaper-Don-t-block-main-thread-in-Connection-.patch rename to leaf-server/minecraft-patches/features/0126-ShreddedPaper-Don-t-block-main-thread-in-Connection-.patch index 4dbf348a..7fdc13ad 100644 --- a/leaf-server/minecraft-patches/features/0125-ShreddedPaper-Don-t-block-main-thread-in-Connection-.patch +++ b/leaf-server/minecraft-patches/features/0126-ShreddedPaper-Don-t-block-main-thread-in-Connection-.patch @@ -6,7 +6,7 @@ Subject: [PATCH] ShreddedPaper: Don't block main thread in diff --git a/net/minecraft/network/Connection.java b/net/minecraft/network/Connection.java -index 7b78c0af4a83bd39a5bc2d6554cc677bd4c0c822..66cc43aec8ddc7b4f1fb3655a7a119d003681b8b 100644 +index 570a6b844319491b1e07df0a3e55d0b29533c649..5a8133497d9c1260a3bba7e9dc7f3b059bbe6b1a 100644 --- a/net/minecraft/network/Connection.java +++ b/net/minecraft/network/Connection.java @@ -325,6 +325,7 @@ public class Connection extends SimpleChannelInboundHandler> { diff --git a/leaf-server/src/main/java/org/dreeam/leaf/config/modules/network/ConnectionFlushQueueRewrite.java b/leaf-server/src/main/java/org/dreeam/leaf/config/modules/network/ConnectionFlushQueueRewrite.java new file mode 100644 index 00000000..d3b8fc44 --- /dev/null +++ b/leaf-server/src/main/java/org/dreeam/leaf/config/modules/network/ConnectionFlushQueueRewrite.java @@ -0,0 +1,30 @@ +package org.dreeam.leaf.config.modules.network; + +import org.dreeam.leaf.config.ConfigModules; +import org.dreeam.leaf.config.EnumConfigCategory; + +public class ConnectionFlushQueueRewrite extends ConfigModules { + + public String getBasePath() { + return EnumConfigCategory.NETWORK.getBaseKeyName(); + } + + public static boolean enabled = false; + + @Override + public void onLoaded() { + enabled = config.getBoolean(getBasePath() + ".connection-flush-queue-rewrite", enabled, config.pickStringRegionBased(""" + This replaces ConcurrentLinkedQueue with ArrayDeque for better performance + and uses the Netty event loop to ensure thread safety. + + May increase the Netty thread usage and requires server restart to take effect + Default: false + """, + """ + 此选项将 ConcurrentLinkedQueue 替换为 ArrayDeque 以提高性能, + 并使用 Netty 事件循环以确保线程安全。 + + 默认值: false + """)); + } +}