Optimise CraftWorld#getLoadedChunks

We can use the existing full chunk map so that we do not need
to iterate over all ChunkHolders. Additionally, do not use streams
to do the iteration either.
This commit is contained in:
Spottedleaf
2025-06-05 08:16:25 -07:00
parent f00d9bce79
commit 24cd24c8cc
2 changed files with 18 additions and 3 deletions

View File

@@ -5,7 +5,7 @@
@VisibleForDebug
private NaturalSpawner.SpawnState lastSpawnState;
+ // Paper start
+ private final ca.spottedleaf.concurrentutil.map.ConcurrentLong2ReferenceChainedHashTable<net.minecraft.world.level.chunk.LevelChunk> fullChunks = new ca.spottedleaf.concurrentutil.map.ConcurrentLong2ReferenceChainedHashTable<>();
+ public final ca.spottedleaf.concurrentutil.map.ConcurrentLong2ReferenceChainedHashTable<net.minecraft.world.level.chunk.LevelChunk> fullChunks = new ca.spottedleaf.concurrentutil.map.ConcurrentLong2ReferenceChainedHashTable<>();
+ public int getFullChunksCount() {
+ return this.fullChunks.size();
+ }

View File

@@ -421,8 +421,23 @@ public class CraftWorld extends CraftRegionAccessor implements World {
@Override
public Chunk[] getLoadedChunks() {
List<ChunkHolder> chunks = ca.spottedleaf.moonrise.common.PlatformHooks.get().getVisibleChunkHolders(this.world); // Paper
return chunks.stream().map(ChunkHolder::getFullChunkNow).filter(Objects::nonNull).map(CraftChunk::new).toArray(Chunk[]::new);
// Paper start - optimise getLoadedChunks
net.minecraft.server.level.ServerChunkCache serverChunkCache = this.getHandle().chunkSource;
ca.spottedleaf.moonrise.common.list.ReferenceList<Chunk> chunks = new ca.spottedleaf.moonrise.common.list.ReferenceList<>(new Chunk[serverChunkCache.fullChunks.size()]);
for (java.util.PrimitiveIterator.OfLong iterator = serverChunkCache.fullChunks.keyIterator(); iterator.hasNext();) {
long chunk = iterator.nextLong();
chunks.add(new CraftChunk(this.world, ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkX(chunk), ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkZ(chunk)));
}
Chunk[] raw = chunks.getRawDataUnchecked();
int size = chunks.size();
if (raw.length == size) {
// always true when on main
return raw;
}
return java.util.Arrays.copyOf(raw, size);
// Paper end - optimise getLoadedChunks
}
@Override