2021-12-05 15:00:13 +01:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2022-09-26 10:02:51 +02:00
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
2021-12-05 15:00:13 +01:00
Date: Thu, 7 May 2020 05:48:54 -0700
Subject: [PATCH] Optimise chunk tick iteration
Use a dedicated list of entity ticking chunks to reduce the cost
2022-02-18 18:44:46 +01:00
diff --git a/src/main/java/net/minecraft/server/level/ChunkHolder.java b/src/main/java/net/minecraft/server/level/ChunkHolder.java
2023-06-09 01:04:53 +02:00
index 4164204ba80f68a768de0ed1721c6447b972a631..4ae1ba645d9fdc1eb6d5a3e4f8ceed9b4841e003 100644
2022-02-18 18:44:46 +01:00
--- a/src/main/java/net/minecraft/server/level/ChunkHolder.java
+++ b/src/main/java/net/minecraft/server/level/ChunkHolder.java
2023-06-09 01:04:53 +02:00
@@ -84,6 +84,11 @@ public class ChunkHolder {
2022-02-18 18:44:46 +01:00
this.playersInMobSpawnRange = this.chunkMap.playerMobSpawnMap.getObjectsInRange(key);
this.playersInChunkTickRange = this.chunkMap.playerChunkTickRangeMap.getObjectsInRange(key);
2022-09-01 18:51:59 +02:00
// Paper end - optimise anyPlayerCloseEnoughForSpawning
2022-02-18 18:44:46 +01:00
+ // Paper start - optimise chunk tick iteration
+ if (this.needsBroadcastChanges()) {
+ this.chunkMap.needsChangeBroadcasting.add(this);
+ }
+ // Paper end - optimise chunk tick iteration
}
2022-09-01 18:51:59 +02:00
public void onChunkRemove() {
2023-06-09 01:04:53 +02:00
@@ -91,6 +96,11 @@ public class ChunkHolder {
2022-02-18 18:44:46 +01:00
this.playersInMobSpawnRange = null;
this.playersInChunkTickRange = null;
2022-09-01 18:51:59 +02:00
// Paper end - optimise anyPlayerCloseEnoughForSpawning
2022-02-18 18:44:46 +01:00
+ // Paper start - optimise chunk tick iteration
+ if (this.needsBroadcastChanges()) {
+ this.chunkMap.needsChangeBroadcasting.remove(this);
+ }
+ // Paper end - optimise chunk tick iteration
}
2022-09-01 18:51:59 +02:00
// Paper end
2023-06-09 01:04:53 +02:00
@@ -230,7 +240,7 @@ public class ChunkHolder {
2022-02-18 18:44:46 +01:00
if (i < 0 || i >= this.changedBlocksPerSection.length) return; // CraftBukkit - SPIGOT-6086, SPIGOT-6296
if (this.changedBlocksPerSection[i] == null) {
- this.hasChangedSections = true;
+ this.hasChangedSections = true; this.addToBroadcastMap(); // Paper - optimise chunk tick iteration
this.changedBlocksPerSection[i] = new ShortOpenHashSet();
}
2023-06-09 01:04:53 +02:00
@@ -254,6 +264,7 @@ public class ChunkHolder {
2022-06-08 12:40:44 +02:00
int k = this.lightEngine.getMaxLightSection();
2022-02-18 18:44:46 +01:00
2022-06-08 12:40:44 +02:00
if (y >= j && y <= k) {
2022-03-01 06:43:03 +01:00
+ this.addToBroadcastMap(); // Paper - optimise chunk tick iteration
2022-06-08 12:40:44 +02:00
int l = y - j;
2022-02-18 18:44:46 +01:00
2022-06-08 12:40:44 +02:00
if (lightType == LightLayer.SKY) {
2023-06-09 01:04:53 +02:00
@@ -268,8 +279,19 @@ public class ChunkHolder {
2022-02-18 18:44:46 +01:00
}
}
+ // Paper start - optimise chunk tick iteration
+ public final boolean needsBroadcastChanges() {
+ return this.hasChangedSections || !this.skyChangedLightSectionFilter.isEmpty() || !this.blockChangedLightSectionFilter.isEmpty();
+ }
+
+ private void addToBroadcastMap() {
+ org.spigotmc.AsyncCatcher.catchOp("ChunkHolder update");
+ this.chunkMap.needsChangeBroadcasting.add(this);
+ }
+ // Paper end - optimise chunk tick iteration
+
public void broadcastChanges(LevelChunk chunk) {
- if (this.hasChangedSections || !this.skyChangedLightSectionFilter.isEmpty() || !this.blockChangedLightSectionFilter.isEmpty()) {
+ if (this.needsBroadcastChanges()) { // Paper - moved into above, other logic needs to call
Level world = chunk.getLevel();
2023-06-08 08:31:22 +02:00
List list;
2022-02-18 18:44:46 +01:00
diff --git a/src/main/java/net/minecraft/server/level/ChunkMap.java b/src/main/java/net/minecraft/server/level/ChunkMap.java
2023-09-21 19:39:51 +02:00
index 80b2ff12e48e1aabebd9ebcf5958c1f5d073d55b..bcf0dfe50add8e260a280e45673727f964bac6fd 100644
2022-02-18 18:44:46 +01:00
--- a/src/main/java/net/minecraft/server/level/ChunkMap.java
+++ b/src/main/java/net/minecraft/server/level/ChunkMap.java
2023-06-08 08:31:22 +02:00
@@ -115,6 +115,8 @@ import org.bukkit.craftbukkit.generator.CustomChunkGenerator;
2022-09-26 10:02:51 +02:00
import org.bukkit.entity.Player;
// CraftBukkit end
+import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet; // Paper
+
public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider {
private static final byte CHUNK_TYPE_REPLACEABLE = -1;
2023-06-08 08:31:22 +02:00
@@ -152,6 +154,7 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
2022-02-18 18:44:46 +01:00
private final Queue<Runnable> unloadQueue;
int viewDistance;
public final com.destroystokyo.paper.util.misc.PlayerAreaMap playerMobDistanceMap; // Paper
+ public final ReferenceOpenHashSet<ChunkHolder> needsChangeBroadcasting = new ReferenceOpenHashSet<>();
2022-09-26 10:02:51 +02:00
// Paper - rewrite chunk system
2021-12-05 15:00:13 +01:00
diff --git a/src/main/java/net/minecraft/server/level/ServerChunkCache.java b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
2023-06-09 01:04:53 +02:00
index 5c5fe2087a7617324ab8e18389e3ffa9ac413026..828de28f2777e2477a9c6545c8af96c4ca4e352b 100644
2021-12-05 15:00:13 +01:00
--- a/src/main/java/net/minecraft/server/level/ServerChunkCache.java
+++ b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
2022-12-07 22:05:01 +01:00
@@ -48,6 +48,7 @@ import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemp
2022-03-01 06:43:03 +01:00
import net.minecraft.world.level.storage.DimensionDataStorage;
import net.minecraft.world.level.storage.LevelData;
import net.minecraft.world.level.storage.LevelStorageSource;
2022-02-18 18:44:46 +01:00
+import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet; // Paper
public class ServerChunkCache extends ChunkSource {
2023-06-09 01:04:53 +02:00
@@ -574,42 +575,59 @@ public class ServerChunkCache extends ChunkSource {
2021-12-05 15:00:13 +01:00
this.lastSpawnState = spawnercreature_d;
gameprofilerfiller.popPush("filteringLoadedChunks");
- List<ServerChunkCache.ChunkAndHolder> list = Lists.newArrayListWithCapacity(l);
- Iterator iterator = this.chunkMap.getChunks().iterator();
+ // Paper - moved down
this.level.timings.chunkTicks.startTiming(); // Paper
- while (iterator.hasNext()) {
- ChunkHolder playerchunk = (ChunkHolder) iterator.next();
- LevelChunk chunk = playerchunk.getTickingChunk();
-
- if (chunk != null) {
- list.add(new ServerChunkCache.ChunkAndHolder(chunk, playerchunk));
- }
- }
+ // Paper - moved down
gameprofilerfiller.popPush("spawnAndTick");
boolean flag2 = this.level.getGameRules().getBoolean(GameRules.RULE_DOMOBSPAWNING) && !this.level.players().isEmpty(); // CraftBukkit
- Collections.shuffle(list);
+ // Paper - only shuffle if per-player mob spawning is disabled
// Paper - moved natural spawn event up
- Iterator iterator1 = list.iterator();
2023-06-08 08:31:22 +02:00
+ // Paper start - optimise chunk tick iteratio
2021-12-05 15:00:13 +01:00
+ Iterator<LevelChunk> iterator1;
2022-06-09 10:51:45 +02:00
+ if (this.level.paperConfig().entities.spawning.perPlayerMobSpawns) {
2021-12-05 15:00:13 +01:00
+ iterator1 = this.entityTickingChunks.iterator();
+ } else {
+ iterator1 = this.entityTickingChunks.unsafeIterator();
+ List<LevelChunk> shuffled = Lists.newArrayListWithCapacity(this.entityTickingChunks.size());
+ while (iterator1.hasNext()) {
+ shuffled.add(iterator1.next());
+ }
+ Collections.shuffle(shuffled);
+ iterator1 = shuffled.iterator();
+ }
+ try {
while (iterator1.hasNext()) {
- ServerChunkCache.ChunkAndHolder chunkproviderserver_a = (ServerChunkCache.ChunkAndHolder) iterator1.next();
- LevelChunk chunk1 = chunkproviderserver_a.chunk;
+ LevelChunk chunk1 = iterator1.next();
+ ChunkHolder holder = chunk1.playerChunk;
+ if (holder != null) {
2022-02-18 18:44:46 +01:00
+ // Paper - move down
2021-12-05 22:07:44 +01:00
+ // Paper end - optimise chunk tick iteration
2021-12-05 15:00:13 +01:00
ChunkPos chunkcoordintpair = chunk1.getPos();
2022-03-01 06:43:03 +01:00
- if (this.level.isNaturalSpawningAllowed(chunkcoordintpair) && this.chunkMap.anyPlayerCloseEnoughForSpawning(chunkproviderserver_a.holder, chunkcoordintpair, false)) { // Paper - optimise anyPlayerCloseEnoughForSpawning
2022-09-26 10:02:51 +02:00
+ if ((true || this.level.isNaturalSpawningAllowed(chunkcoordintpair)) && this.chunkMap.anyPlayerCloseEnoughForSpawning(holder, chunkcoordintpair, false)) { // Paper - optimise anyPlayerCloseEnoughForSpawning // Paper - the chunk is known ticking
2021-12-05 15:00:13 +01:00
chunk1.incrementInhabitedTime(j);
- if (flag2 && (this.spawnEnemies || this.spawnFriendlies) && this.level.getWorldBorder().isWithinBounds(chunkcoordintpair) && this.chunkMap.anyPlayerCloseEnoughForSpawning(chunkproviderserver_a.holder, chunkcoordintpair, true)) { // Spigot // Paper - optimise anyPlayerCloseEnoughForSpawning
+ if (flag2 && (this.spawnEnemies || this.spawnFriendlies) && this.level.getWorldBorder().isWithinBounds(chunkcoordintpair) && this.chunkMap.anyPlayerCloseEnoughForSpawning(holder, chunkcoordintpair, true)) { // Spigot // Paper - optimise anyPlayerCloseEnoughForSpawning & optimise chunk tick iteration
NaturalSpawner.spawnForChunk(this.level, chunk1, spawnercreature_d, this.spawnFriendlies, this.spawnEnemies, flag1);
}
2022-09-26 10:02:51 +02:00
- if (this.level.shouldTickBlocksAt(chunkcoordintpair.toLong())) {
+ if (true || this.level.shouldTickBlocksAt(chunkcoordintpair.toLong())) { // Paper - the chunk is known ticking
2021-12-05 15:00:13 +01:00
this.level.tickChunk(chunk1, k);
}
}
2021-12-05 22:07:44 +01:00
+ // Paper start - optimise chunk tick iteration
2021-12-05 15:00:13 +01:00
+ }
2023-03-23 22:57:03 +01:00
+ }
2021-12-05 15:00:13 +01:00
+
+ } finally {
+ if (iterator1 instanceof io.papermc.paper.util.maplist.IteratorSafeOrderedReferenceSet.Iterator safeIterator) {
+ safeIterator.finishedIterating();
+ }
2023-03-23 22:57:03 +01:00
}
2021-12-05 15:00:13 +01:00
+ // Paper end - optimise chunk tick iteration
this.level.timings.chunkTicks.stopTiming(); // Paper
gameprofilerfiller.popPush("customSpawners");
if (flag2) {
2023-06-09 01:04:53 +02:00
@@ -617,15 +635,24 @@ public class ServerChunkCache extends ChunkSource {
2022-02-18 18:44:46 +01:00
this.level.tickCustomSpawners(this.spawnEnemies, this.spawnFriendlies);
2021-12-05 15:00:13 +01:00
} // Paper - timings
}
2022-02-18 18:44:46 +01:00
-
2023-03-23 22:57:03 +01:00
- gameprofilerfiller.popPush("broadcast");
2021-12-05 15:00:13 +01:00
- list.forEach((chunkproviderserver_a1) -> {
- this.level.timings.broadcastChunkUpdates.startTiming(); // Paper - timing
- chunkproviderserver_a1.holder.broadcastChanges(chunkproviderserver_a1.chunk);
- this.level.timings.broadcastChunkUpdates.stopTiming(); // Paper - timing
- });
2023-03-23 22:57:03 +01:00
gameprofilerfiller.pop();
+ // Paper start - use set of chunks requiring updates, rather than iterating every single one loaded
+ gameprofilerfiller.popPush("broadcast");
2022-02-18 18:44:46 +01:00
+ this.level.timings.broadcastChunkUpdates.startTiming(); // Paper - timing
+ if (!this.chunkMap.needsChangeBroadcasting.isEmpty()) {
+ ReferenceOpenHashSet<ChunkHolder> copy = this.chunkMap.needsChangeBroadcasting.clone();
+ this.chunkMap.needsChangeBroadcasting.clear();
+ for (ChunkHolder holder : copy) {
2022-03-29 10:32:18 +02:00
+ holder.broadcastChanges(holder.getFullChunkNowUnchecked()); // LevelChunks are NEVER unloaded
2022-02-18 18:44:46 +01:00
+ if (holder.needsBroadcastChanges()) {
+ // I DON'T want to KNOW what DUMB plugins might be doing.
+ this.chunkMap.needsChangeBroadcasting.add(holder);
+ }
+ }
+ }
+ this.level.timings.broadcastChunkUpdates.stopTiming(); // Paper - timing
gameprofilerfiller.pop();
+ // Paper end - use set of chunks requiring updates, rather than iterating every single one loaded
2021-12-05 22:58:01 +01:00
this.chunkMap.tick();
}
2022-02-18 18:44:46 +01:00
}