From bfb789c8626b0bd275e98d4ced8195280345c029 Mon Sep 17 00:00:00 2001 From: Aikar Date: Sat, 23 May 2020 17:15:40 -0400 Subject: [PATCH] Optimize sending packets to nearby locations (sounds/effects) Instead of using the entire world or player list, use the distance maps to only iterate players who are even seeing the chunk the packet is originating from. This will drastically cut down on packet sending cost for worlds with lots of players in them. Closes #3437 --- Spigot-Server-Patches/MC-Utils.patch | 14 ++++++ ...-packets-to-nearby-locations-sounds-.patch | 44 +++++++++++++++++++ paper | 7 +++ 3 files changed, 65 insertions(+) create mode 100644 Spigot-Server-Patches/Optimize-sending-packets-to-nearby-locations-sounds-.patch diff --git a/Spigot-Server-Patches/MC-Utils.patch b/Spigot-Server-Patches/MC-Utils.patch index be1fee685f..ecbe64ce6f 100644 --- a/Spigot-Server-Patches/MC-Utils.patch +++ b/Spigot-Server-Patches/MC-Utils.patch @@ -3004,6 +3004,20 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 public void b(PacketDataSerializer packetdataserializer) { this.a(); packetdataserializer.writeByte(this.i); +diff --git a/src/main/java/net/minecraft/server/DimensionManager.java b/src/main/java/net/minecraft/server/DimensionManager.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/net/minecraft/server/DimensionManager.java ++++ b/src/main/java/net/minecraft/server/DimensionManager.java +@@ -0,0 +0,0 @@ public class DimensionManager implements MinecraftSerializable { + return this.folder.isEmpty() ? file : new File(file, this.folder); + } + ++ public WorldServer world; // Paper - store ref to world this manager is for + public WorldProvider getWorldProvider(World world) { ++ if (this.world == null) this.world = (WorldServer) world; // Paper + return (WorldProvider) this.providerFactory.apply(world, this); + } + diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/net/minecraft/server/Entity.java diff --git a/Spigot-Server-Patches/Optimize-sending-packets-to-nearby-locations-sounds-.patch b/Spigot-Server-Patches/Optimize-sending-packets-to-nearby-locations-sounds-.patch new file mode 100644 index 0000000000..5c0921f7fc --- /dev/null +++ b/Spigot-Server-Patches/Optimize-sending-packets-to-nearby-locations-sounds-.patch @@ -0,0 +1,44 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Aikar +Date: Sat, 23 May 2020 17:03:41 -0400 +Subject: [PATCH] Optimize sending packets to nearby locations (sounds/effects) + +Instead of using the entire world or player list, use the distance +maps to only iterate players who are even seeing the chunk the packet +is originating from. + +This will drastically cut down on packet sending cost for worlds with +lots of players in them. + +diff --git a/src/main/java/net/minecraft/server/PlayerList.java b/src/main/java/net/minecraft/server/PlayerList.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/net/minecraft/server/PlayerList.java ++++ b/src/main/java/net/minecraft/server/PlayerList.java +@@ -0,0 +0,0 @@ public abstract class PlayerList { + world = (WorldServer) entityhuman.world; + } + +- List players1 = world == null ? players : world.players; +- for (int j = 0; j < players1.size(); ++j) { +- EntityHuman entity = players1.get(j); +- if (!(entity instanceof EntityPlayer)) continue; +- EntityPlayer entityplayer = (EntityPlayer) entity; ++ // Paper start ++ if (world == null && dimensionmanager != null) { ++ world = dimensionmanager.world; ++ } ++ if (world == null) { ++ LOGGER.error("Sending packet to invalid world" + entityhuman + " " + dimensionmanager + " - " + packet.getClass().getName(), new Throwable()); ++ return; // ??? shouldn't happen... ++ } ++ com.destroystokyo.paper.util.misc.PooledLinkedHashSets.PooledObjectLinkedOpenHashSet nearbyPlayers = world.getChunkProvider().playerChunkMap.playerViewDistanceBroadcastMap.getObjectsInRange(MCUtil.fastFloor(d0) >> 4, MCUtil.fastFloor(d2) >> 4); ++ if (nearbyPlayers == null) { ++ return; ++ } ++ Object[] backingSet = nearbyPlayers.getBackingSet(); ++ for (Object object : backingSet) { ++ if (!(object instanceof EntityPlayer)) continue; ++ EntityPlayer entityplayer = (EntityPlayer) object; + // Paper end + + // CraftBukkit start - Test if player receiving packet can see the source of the packet diff --git a/paper b/paper index 19a6731168..687c3813e6 100755 --- a/paper +++ b/paper @@ -68,6 +68,13 @@ case "$1" in ls -la Paper-Server/target/paper*.jar ) || failed=1 ;; + "pc" | "paperclip") + ( + set -e + cd "$basedir" + scripts/paperclip.sh "$basedir" || exit 1 + ) || failed=1 + ;; "make") ( if [[ "$2" = "bacon" ]] ; then