2021-12-05 15:00:13 +01:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
|
From: Spottedleaf <spottedleaf@spottedleaf.dev>
|
|
|
|
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
|
|
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/level/ServerChunkCache.java b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
|
2022-01-01 04:05:42 +01:00
|
|
|
index e306dbf3487676e01c483d121c1f26e141358e3f..482676d325a7d502fd74a67236eb05d97fac1d85 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
|
2021-12-05 22:58:01 +01:00
|
|
|
@@ -999,34 +999,46 @@ 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();
|
|
|
|
+ // Paper start - optimise chunk tick iteration
|
|
|
|
+ Iterator<LevelChunk> iterator1;
|
|
|
|
+ if (this.level.paperConfig.perPlayerMobSpawns) {
|
|
|
|
+ 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) {
|
|
|
|
+ gameprofilerfiller.popPush("broadcast");
|
|
|
|
+ this.level.timings.broadcastChunkUpdates.startTiming(); // Paper - timing
|
|
|
|
+ holder.broadcastChanges(chunk1);
|
|
|
|
+ this.level.timings.broadcastChunkUpdates.stopTiming(); // Paper - timing
|
|
|
|
+ gameprofilerfiller.pop();
|
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();
|
|
|
|
|
|
|
|
- if (this.level.isPositionEntityTicking(chunkcoordintpair) && this.chunkMap.anyPlayerCloseEnoughForSpawning(chunkproviderserver_a.holder, chunkcoordintpair, false)) { // Paper - optimise anyPlayerCloseEnoughForSpawning
|
|
|
|
+ if ((true || this.level.isPositionEntityTicking(chunkcoordintpair)) && this.chunkMap.anyPlayerCloseEnoughForSpawning(holder, chunkcoordintpair, false)) { // Paper - optimise anyPlayerCloseEnoughForSpawning & optimise chunk tick iteration
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-12-05 22:58:01 +01:00
|
|
|
@@ -1034,7 +1046,16 @@ public class ServerChunkCache extends ChunkSource {
|
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
|
|
|
+ }
|
|
|
|
}
|
|
|
|
+
|
|
|
|
+ } finally {
|
|
|
|
+ if (iterator1 instanceof io.papermc.paper.util.maplist.IteratorSafeOrderedReferenceSet.Iterator safeIterator) {
|
|
|
|
+ safeIterator.finishedIterating();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // Paper end - optimise chunk tick iteration
|
|
|
|
this.level.timings.chunkTicks.stopTiming(); // Paper
|
|
|
|
gameprofilerfiller.popPush("customSpawners");
|
|
|
|
if (flag2) {
|
2021-12-05 22:58:01 +01:00
|
|
|
@@ -1043,13 +1064,7 @@ public class ServerChunkCache extends ChunkSource {
|
2021-12-05 15:00:13 +01:00
|
|
|
} // Paper - timings
|
|
|
|
}
|
|
|
|
|
|
|
|
- gameprofilerfiller.popPush("broadcast");
|
|
|
|
- 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
|
|
|
|
- });
|
|
|
|
- gameprofilerfiller.pop();
|
|
|
|
+ // Paper - no, iterating just ONCE is expensive enough! Don't do it TWICE! Code moved up
|
|
|
|
gameprofilerfiller.pop();
|
2021-12-05 22:58:01 +01:00
|
|
|
this.chunkMap.tick();
|
|
|
|
}
|