1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
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
index b5c8f3f57d09de4caffeb9f3e20e9bf4daba1cdd..d6981bbcf480c5856b51960013d144beba2361b3 100644
--- a/src/main/java/net/minecraft/server/level/ServerChunkCache.java
+++ b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
@@ -1007,19 +1007,35 @@ public class ServerChunkCache extends ChunkSource {
this.lastSpawnState = spawnercreature_d;
this.level.getProfiler().pop();
- List<ChunkHolder> list = Lists.newArrayList(this.chunkMap.getChunks());
-
- Collections.shuffle(list);
+ // Paper - moved down, enabled if per-player = false
// Paper - moved natural spawn event up
this.level.timings.chunkTicks.startTiming(); // Paper
- list.forEach((playerchunk) -> {
- Optional<LevelChunk> optional = ((Either) playerchunk.getTickingChunkFuture().getNow(ChunkHolder.UNLOADED_LEVEL_CHUNK)).left();
-
- if (optional.isPresent()) {
- LevelChunk chunk = (LevelChunk) optional.get();
+ // Paper start
+ java.util.Iterator<LevelChunk> iterator;
+ if (this.level.paperConfig.perPlayerMobSpawns) {
+ iterator = this.entityTickingChunks.iterator();
+ } else {
+ iterator = this.entityTickingChunks.unsafeIterator();
+ List<LevelChunk> shuffled = new java.util.ArrayList<>(this.entityTickingChunks.size());
+ while (iterator.hasNext()) {
+ shuffled.add(iterator.next());
+ }
+ Collections.shuffle(shuffled);
+ iterator = shuffled.iterator();
+ }
+ try { while (iterator.hasNext()) {
+ LevelChunk chunk = iterator.next();
+ ChunkHolder playerchunk = chunk.playerChunk;
+ if (playerchunk != null) {
+ this.level.getProfiler().push("broadcast");
+ this.level.timings.broadcastChunkUpdates.startTiming(); // Paper - timings
+ playerchunk.broadcastChanges(chunk);
+ this.level.timings.broadcastChunkUpdates.stopTiming(); // Paper - timings
+ this.level.getProfiler().pop();
+ // Paper end
ChunkPos chunkcoordintpair = chunk.getPos();
- if (this.level.isPositionEntityTicking(chunkcoordintpair) && !this.chunkMap.isOutsideOfRange(playerchunk, chunkcoordintpair, false)) { // Paper - optimise isOutsideOfRange
+ if ((true || this.level.isPositionEntityTicking(chunkcoordintpair)) && !this.chunkMap.isOutsideOfRange(playerchunk, chunkcoordintpair, false)) { // Paper - optimise isOutsideOfRange // Paper - we only iterate entity ticking chunks
chunk.setInhabitedTime(chunk.getInhabitedTime() + j);
if (flag1 && (this.spawnEnemies || this.spawnFriendlies) && this.level.getWorldBorder().isWithinBounds(chunk.getPos()) && !this.chunkMap.isOutsideOfRange(playerchunk, chunkcoordintpair, true)) { // Spigot // Paper - optimise isOutsideOfRange
NaturalSpawner.spawnForChunk(this.level, chunk, spawnercreature_d, this.spawnFriendlies, this.spawnEnemies, flag2);
@@ -1030,7 +1046,13 @@ public class ServerChunkCache extends ChunkSource {
// this.level.timings.doTickTiles.stopTiming(); // Spigot // Paper
}
}
- });
+ } // Paper start - optimise chunk tick iteration
+ } finally {
+ if (iterator instanceof io.papermc.paper.util.maplist.IteratorSafeOrderedReferenceSet.Iterator) {
+ ((io.papermc.paper.util.maplist.IteratorSafeOrderedReferenceSet.Iterator<LevelChunk>)iterator).finishedIterating();
+ }
+ }
+ // Paper end - optimise chunk tick iteration
this.level.timings.chunkTicks.stopTiming(); // Paper
this.level.getProfiler().push("customSpawners");
if (flag1) {
@@ -1039,21 +1061,7 @@ public class ServerChunkCache extends ChunkSource {
} // Paper - timings
}
- this.level.getProfiler().popPush("broadcast");
- this.chunkMap.getChunks().forEach((playerchunk) -> { // Paper - no... just no...
- Optional<LevelChunk> optional = ((Either) playerchunk.getTickingChunkFuture().getNow(ChunkHolder.UNLOADED_LEVEL_CHUNK)).left(); // CraftBukkit - decompile error
-
- Objects.requireNonNull(playerchunk);
-
- // Paper start - timings
- optional.ifPresent(chunk -> {
- this.level.timings.broadcastChunkUpdates.startTiming(); // Paper - timings
- playerchunk.broadcastChanges(chunk);
- this.level.timings.broadcastChunkUpdates.stopTiming(); // Paper - timings
- });
- // Paper end
- });
- this.level.getProfiler().pop();
+ // Paper - no, iterating just ONCE is expensive enough! Don't do it TWICE! Code moved up
this.level.getProfiler().pop();
}
|