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 e28d859b457ca0e24bc6dc9d6cd4a97f12ae0671..5b31eed8f91b45b46521da5169d8003dfd5ec669 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 ctx) throws Exception { super.channelActive(ctx); this.channel = ctx.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) { @@ -472,7 +474,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 } }