From 4c36d6db57ce0c8ef728c108d21c2066cad2bcbe Mon Sep 17 00:00:00 2001 From: Spottedleaf Date: Fri, 21 Jun 2024 11:44:53 -0700 Subject: [PATCH] Fix unload queue storing chunks in wrong sections The unload queue stored the chunks in the same section as the chunk coordinate, when it needed to apply the unload shift. Additionally, change the default region shift to the ticket propagator shift as there is no benefit to using a low region shift since no regionizing is occuring. This makes the unload queue shift 6, which should reduce the number of sections to deal with while processing unloads. --- .../mixin/chunk_system/ServerLevelMixin.java | 7 +++--- .../chunk_system/queue/ChunkUnloadQueue.java | 24 +++++++++++-------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/ServerLevelMixin.java b/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/ServerLevelMixin.java index 6079404..c84f057 100644 --- a/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/ServerLevelMixin.java +++ b/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/ServerLevelMixin.java @@ -14,6 +14,7 @@ import ca.spottedleaf.moonrise.patches.chunk_system.player.RegionizedPlayerChunk import ca.spottedleaf.moonrise.patches.chunk_system.scheduling.ChunkHolderManager; import ca.spottedleaf.moonrise.patches.chunk_system.scheduling.ChunkTaskScheduler; import ca.spottedleaf.moonrise.patches.chunk_system.scheduling.NewChunkHolder; +import ca.spottedleaf.moonrise.patches.chunk_system.scheduling.ThreadedTicketLevelPropagator; import net.minecraft.core.BlockPos; import net.minecraft.core.Holder; import net.minecraft.core.RegistryAccess; @@ -179,9 +180,9 @@ public abstract class ServerLevelMixin extends Level implements ChunkSystemServe @Override public final int moonrise$getRegionChunkShift() { - // current default in Folia - // note that there is no actual regionizing taking place in Moonrise... - return 2; + // note that there is no actual regionizing taking place in Moonrise, so we set this to the highest shift to minimize + // performance impact + return ThreadedTicketLevelPropagator.SECTION_SHIFT; } @Override diff --git a/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/queue/ChunkUnloadQueue.java b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/queue/ChunkUnloadQueue.java index bc07e71..7eafc5b 100644 --- a/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/queue/ChunkUnloadQueue.java +++ b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/queue/ChunkUnloadQueue.java @@ -63,12 +63,13 @@ public final class ChunkUnloadQueue { final int shift = this.coordinateShift; final int sectionX = chunkX >> shift; final int sectionZ = chunkZ >> shift; + final long sectionKey = CoordinateUtils.getChunkKey(sectionX, sectionZ); final long chunkKey = CoordinateUtils.getChunkKey(chunkX, chunkZ); - UnloadSection section = this.unloadSections.get(chunkKey); + UnloadSection section = this.unloadSections.get(sectionKey); if (section == null) { section = new UnloadSection(this.orderGenerator.getAndIncrement()); - this.unloadSections.put(chunkKey, section); + this.unloadSections.put(sectionKey, section); } return section.chunks.add(chunkKey); @@ -80,9 +81,10 @@ public final class ChunkUnloadQueue { final int shift = this.coordinateShift; final int sectionX = chunkX >> shift; final int sectionZ = chunkZ >> shift; + final long sectionKey = CoordinateUtils.getChunkKey(sectionX, sectionZ); final long chunkKey = CoordinateUtils.getChunkKey(chunkX, chunkZ); - final UnloadSection section = this.unloadSections.get(chunkKey); + final UnloadSection section = this.unloadSections.get(sectionKey); if (section == null) { return false; @@ -93,7 +95,7 @@ public final class ChunkUnloadQueue { } if (section.chunks.isEmpty()) { - this.unloadSections.remove(chunkKey); + this.unloadSections.remove(sectionKey); } return true; @@ -114,14 +116,16 @@ public final class ChunkUnloadQueue { sectionJson.add("coordinates", coordinates); final UnloadSection actualSection = this.getSectionUnsynchronized(section.sectionX(), section.sectionZ()); - for (final LongIterator iterator = actualSection.chunks.clone().iterator(); iterator.hasNext();) { - final long coordinate = iterator.nextLong(); + if (actualSection != null) { + for (final LongIterator iterator = actualSection.chunks.clone().iterator(); iterator.hasNext(); ) { + final long coordinate = iterator.nextLong(); - final JsonObject coordinateJson = new JsonObject(); - coordinates.add(coordinateJson); + final JsonObject coordinateJson = new JsonObject(); + coordinates.add(coordinateJson); - coordinateJson.addProperty("chunkX", Integer.valueOf(CoordinateUtils.getChunkX(coordinate))); - coordinateJson.addProperty("chunkZ", Integer.valueOf(CoordinateUtils.getChunkZ(coordinate))); + coordinateJson.addProperty("chunkX", Integer.valueOf(CoordinateUtils.getChunkX(coordinate))); + coordinateJson.addProperty("chunkZ", Integer.valueOf(CoordinateUtils.getChunkZ(coordinate))); + } } }