From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com> Date: Fri, 11 Jul 2025 20:39:54 +0300 Subject: [PATCH] Paper PR: Optimise non-flush packet sending Original project: https://github.com/PaperMC/Paper Original license: GPLv3 Paper pull request: https://github.com/PaperMC/Paper/pull/10172 Places like entity tracking make heavy use of packet sending, and internally netty will use some very expensive thread wakeup calls when scheduling. Thanks to various hacks in ProtocolLib as well as other plugins, we cannot simply use a queue of packets to group send on execute. We have to call execute for each packet. Tux's suggestion here is exactly what was needed - tag the Runnable indicating it should not make a wakeup call. Big thanks to Tux for making this possible as I had given up on this optimisation before he came along. Locally this patch drops the entity tracker tick by a full 1.5x. diff --git a/net/minecraft/network/Connection.java b/net/minecraft/network/Connection.java index f6e5fb11b471c34cbc7f3082b23c0a2a14331363..b63e976bfa89c0be75910954788500901c322ba1 100644 --- a/net/minecraft/network/Connection.java +++ b/net/minecraft/network/Connection.java @@ -150,6 +150,7 @@ public class Connection extends SimpleChannelInboundHandler> { // Paper start - Optimize network public boolean isPending = true; public boolean queueImmunity; + private io.netty.channel.SingleThreadEventLoop eventLoop; // Paper - Optimise non-flush packet sending // Paper end - Optimize network public Connection(PacketFlow receiving) { @@ -160,6 +161,7 @@ public class Connection extends SimpleChannelInboundHandler> { public void channelActive(ChannelHandlerContext context) throws Exception { super.channelActive(context); this.channel = context.channel(); + this.eventLoop = (io.netty.channel.SingleThreadEventLoop) this.channel.eventLoop(); // Paper - Optimise non-flush packet sending this.address = this.channel.remoteAddress(); this.preparing = false; // Spigot if (this.delayedDisconnect != null) { @@ -474,7 +476,13 @@ public class Connection extends SimpleChannelInboundHandler> { if (this.channel.eventLoop().inEventLoop()) { this.doSendPacket(packet, sendListener, flush); } else { - this.channel.eventLoop().execute(() -> this.doSendPacket(packet, sendListener, flush)); + // Paper start - Optimise non-flush packet sending + if (!flush && org.bxteam.divinemc.config.DivineConfig.NetworkCategory.optimizeNonFlushPacketSending) { + this.eventLoop.lazyExecute(() -> this.doSendPacket(packet, sendListener, flush)); + } else { + this.channel.eventLoop().execute(() -> this.doSendPacket(packet, sendListener, flush)); + } + // Paper end - Optimise non-flush packet sending } }