From b92befc98058c1b8723616cd8d8bc3d721806547 Mon Sep 17 00:00:00 2001 From: dan28000 <84990628+dan28000@users.noreply.github.com> Date: Sat, 22 Nov 2025 15:12:37 +0100 Subject: [PATCH] Simplify patch for RCT (#40) --- .../0051-Regionized-Chunk-Ticking.patch | 80 +++++++------------ .../features/0068-Optimize-Moonrise.patch | 6 +- .../async/rct/RegionizedChunkTicking.java | 56 +++++++------ 3 files changed, 59 insertions(+), 83 deletions(-) diff --git a/divinemc-server/minecraft-patches/features/0051-Regionized-Chunk-Ticking.patch b/divinemc-server/minecraft-patches/features/0051-Regionized-Chunk-Ticking.patch index 1b62102..0c07603 100644 --- a/divinemc-server/minecraft-patches/features/0051-Regionized-Chunk-Ticking.patch +++ b/divinemc-server/minecraft-patches/features/0051-Regionized-Chunk-Ticking.patch @@ -34,7 +34,7 @@ index 04dd1bec1aff470e67a21fb0b25932685992ec82..72a0a80f1fffa43e143c80c689db5302 Objects.checkFromToIndex(0, size, raw.length); diff --git a/net/minecraft/server/level/ServerChunkCache.java b/net/minecraft/server/level/ServerChunkCache.java -index 6ea62fbffda38e477ef8e119608fc93793db95c3..dda53860397ee52f64209a8d08a7707cfa2f7592 100644 +index 6ea62fbffda38e477ef8e119608fc93793db95c3..66072f0851be5ca75f9fbef88297625272e7a597 100644 --- a/net/minecraft/server/level/ServerChunkCache.java +++ b/net/minecraft/server/level/ServerChunkCache.java @@ -57,7 +57,7 @@ import org.slf4j.Logger; @@ -59,68 +59,39 @@ index 6ea62fbffda38e477ef8e119608fc93793db95c3..dda53860397ee52f64209a8d08a7707c @Nullable @VisibleForDebug private NaturalSpawner.SpawnState lastSpawnState; -@@ -156,34 +158,46 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon +@@ -156,7 +158,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon // Paper end - rewrite chunk system // Paper start - chunk tick iteration optimisations private final ca.spottedleaf.moonrise.common.util.SimpleThreadUnsafeRandom shuffleRandom = new ca.spottedleaf.moonrise.common.util.SimpleThreadUnsafeRandom(0L); - private void iterateTickingChunksFaster() { -+ private void iterateTickingChunksFaster(final CompletableFuture spawns) { // DivineMC - Regionized Chunk Ticking ++ protected void iterateTickingChunksFaster(final CompletableFuture spawns) { // DivineMC - private -> protected Regionized Chunk Ticking final ServerLevel world = this.level; final int randomTickSpeed = world.getGameRules().getInt(GameRules.RULE_RANDOMTICKING); - // TODO check on update: impl of forEachBlockTickingChunk will only iterate ENTITY ticking chunks! - // TODO check on update: consumer just runs tickChunk -- final ca.spottedleaf.moonrise.common.list.ReferenceList entityTickingChunks = ((ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemServerLevel)world).moonrise$getEntityTickingChunks(); -+ final ca.spottedleaf.moonrise.common.list.ReferenceList entityTickingChunks = ((ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemServerLevel)world).moonrise$getEntityTickingChunks(); // DivineMC - Regionized Chunk Ticking +@@ -176,14 +178,16 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon - // note: we can use the backing array here because: - // 1. we do not care about new additions - // 2. _removes_ are impossible at this stage in the tick -- final LevelChunk[] raw = entityTickingChunks.getRawDataUnchecked(); -+ final LevelChunk[] raw = entityTickingChunks.toArray(new LevelChunk[0]); // DivineMC - use toArray instead of getRawDataUnchecked this way is safe and doesn't have performance impact - final int size = entityTickingChunks.size(); - -- java.util.Objects.checkFromToIndex(0, size, raw.length); -- for (int i = 0; i < size; ++i) { -- world.tickChunk(raw[i], randomTickSpeed); -- -- // call mid-tick tasks for chunk system -- if ((i & 7) == 0) { + // call mid-tick tasks for chunk system + if ((i & 7) == 0) { - // DivineMC start - Parallel world ticking - if (!org.bxteam.divinemc.config.DivineConfig.AsyncCategory.enableParallelWorldTicking) { - ((ca.spottedleaf.moonrise.patches.chunk_system.server.ChunkSystemMinecraftServer) this.level.getServer()).moonrise$executeMidTickTasks(); - continue; -+ // DivineMC start - Regionized Chunk Ticking -+ if (org.bxteam.divinemc.config.DivineConfig.AsyncCategory.enableRegionizedChunkTicking) { -+ if (this instanceof org.bxteam.divinemc.async.rct.RegionizedChunkTicking rct) { -+ rct.execute(spawns, raw); -+ } -+ } else { -+ java.util.Objects.checkFromToIndex(0, size, raw.length); -+ for (int i = 0; i < size; ++i) { -+ world.tickChunk(raw[i], randomTickSpeed); -+ -+ // call mid-tick tasks for chunk system -+ if ((i & 7) == 0) { -+ // DivineMC start - Parallel world ticking -+ if (!org.bxteam.divinemc.config.DivineConfig.AsyncCategory.enableParallelWorldTicking) { -+ ((ca.spottedleaf.moonrise.patches.chunk_system.server.ChunkSystemMinecraftServer) this.level.getServer()).moonrise$executeMidTickTasks(); -+ continue; -+ } -+ // DivineMC end - Parallel world ticking - } +- } - // DivineMC end - Parallel world ticking -+ } -+ -+ if (org.bxteam.divinemc.config.DivineConfig.AsyncCategory.asyncNaturalSpawn) { -+ spawns.join(); ++ ((ca.spottedleaf.moonrise.patches.chunk_system.server.ChunkSystemMinecraftServer)this.level.getServer()).moonrise$executeMidTickTasks(); ++ continue; } } ++ ++ // DivineMC start - Regionized Chunk Ticking ++ if (org.bxteam.divinemc.config.DivineConfig.AsyncCategory.asyncNaturalSpawn) { ++ spawns.join(); ++ } + // DivineMC end - Regionized Chunk Ticking } // Paper end - chunk tick iteration optimisations -@@ -502,14 +516,21 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon +@@ -502,14 +506,21 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon long gameTime = this.level.getGameTime(); long l = gameTime - this.lastInhabitedUpdate; this.lastInhabitedUpdate = gameTime; @@ -146,7 +117,7 @@ index 6ea62fbffda38e477ef8e119608fc93793db95c3..dda53860397ee52f64209a8d08a7707c // DivineMC start - Pufferfish: Optimize mob spawning if (org.bxteam.divinemc.config.DivineConfig.AsyncCategory.enableAsyncSpawning) { for (ServerPlayer player : this.level.players) { -@@ -553,14 +574,18 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon +@@ -553,14 +564,18 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon } private void broadcastChangedChunks() { @@ -171,7 +142,7 @@ index 6ea62fbffda38e477ef8e119608fc93793db95c3..dda53860397ee52f64209a8d08a7707c } private void tickChunks(long timeInhabited) { -@@ -610,6 +635,24 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon +@@ -610,6 +625,24 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon filteredSpawningCategories = List.of(); } @@ -196,7 +167,7 @@ index 6ea62fbffda38e477ef8e119608fc93793db95c3..dda53860397ee52f64209a8d08a7707c List list = this.spawningChunks; try { -@@ -627,12 +670,12 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon +@@ -627,12 +660,12 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon } finally { list.clear(); } @@ -214,7 +185,7 @@ index 6ea62fbffda38e477ef8e119608fc93793db95c3..dda53860397ee52f64209a8d08a7707c private void tickSpawningChunk(LevelChunk chunk, long timeInhabited, List spawnCategories, NaturalSpawner.SpawnState spawnState) { ChunkPos pos = chunk.getPos(); diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java -index ca9883277c0f036c94e861f7917ca42facd3c47b..8c98c2593eec14a8a378041e94cf52b8fbfedc30 100644 +index ca9883277c0f036c94e861f7917ca42facd3c47b..694f90490c44dc87127c9806f4d08028e6184798 100644 --- a/net/minecraft/server/level/ServerLevel.java +++ b/net/minecraft/server/level/ServerLevel.java @@ -197,7 +197,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe @@ -277,20 +248,27 @@ index ca9883277c0f036c94e861f7917ca42facd3c47b..8c98c2593eec14a8a378041e94cf52b8 this.chunkSource.getGeneratorState().ensureStructuresGenerated(); this.portalForcer = new PortalForcer(this); this.updateSkyBrightness(); -@@ -846,6 +864,13 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe +@@ -846,6 +864,12 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe this.dragonFight.tick(); } + // DivineMC start - Regionized Chunk Ticking + if (org.bxteam.divinemc.config.DivineConfig.AsyncCategory.enableRegionizedChunkTicking) { + this.tickBlockEntities(); -+ return; -+ } ++ } else { + // DivineMC end - Regionized Chunk Ticking + io.papermc.paper.entity.activation.ActivationRange.activateEntities(this); // Paper - EAR this.entityTickList .forEach( +@@ -871,6 +895,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe + } + ); + this.tickBlockEntities(); ++ } + } + + // Paper - rewrite chunk system @@ -1874,22 +1899,16 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe if (Shapes.joinIsNotEmpty(collisionShape, collisionShape1, BooleanOp.NOT_SAME)) { List list = new ObjectArrayList<>(); diff --git a/divinemc-server/minecraft-patches/features/0068-Optimize-Moonrise.patch b/divinemc-server/minecraft-patches/features/0068-Optimize-Moonrise.patch index feef04a..b42eae4 100644 --- a/divinemc-server/minecraft-patches/features/0068-Optimize-Moonrise.patch +++ b/divinemc-server/minecraft-patches/features/0068-Optimize-Moonrise.patch @@ -440,10 +440,10 @@ index d03d075d5c56b7d2beb5f0aafecbb69f5b3bbf5b..ce3b8f4161dde3e2758c5d33445da150 } diff --git a/net/minecraft/server/level/ServerChunkCache.java b/net/minecraft/server/level/ServerChunkCache.java -index dda53860397ee52f64209a8d08a7707cfa2f7592..8acdce5a4f5546d0fb5907cfc45731c40372e3ed 100644 +index 66072f0851be5ca75f9fbef88297625272e7a597..629d6badbb53264b868580ac0d2b4cb82eb619e8 100644 --- a/net/minecraft/server/level/ServerChunkCache.java +++ b/net/minecraft/server/level/ServerChunkCache.java -@@ -461,8 +461,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon +@@ -451,8 +451,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon public boolean isPositionTicking(long chunkPos) { // Paper start - rewrite chunk system @@ -454,7 +454,7 @@ index dda53860397ee52f64209a8d08a7707cfa2f7592..8acdce5a4f5546d0fb5907cfc45731c4 } diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java -index ba496e78740218a176d8e3117a21cbfc5173cfef..14790f65b00ede0c063c567f524674270ca58b5c 100644 +index 1ef8ac5985b9e3dd92e4741b1ee5eb2ab5c8b3fd..8415cde54fc30216f95ffe5bbf7eb9d5af2a22c8 100644 --- a/net/minecraft/server/level/ServerLevel.java +++ b/net/minecraft/server/level/ServerLevel.java @@ -186,6 +186,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe diff --git a/divinemc-server/src/main/java/org/bxteam/divinemc/async/rct/RegionizedChunkTicking.java b/divinemc-server/src/main/java/org/bxteam/divinemc/async/rct/RegionizedChunkTicking.java index 7045067..bfce2be 100644 --- a/divinemc-server/src/main/java/org/bxteam/divinemc/async/rct/RegionizedChunkTicking.java +++ b/divinemc-server/src/main/java/org/bxteam/divinemc/async/rct/RegionizedChunkTicking.java @@ -12,6 +12,14 @@ import it.unimi.dsi.fastutil.longs.LongIterator; import it.unimi.dsi.fastutil.longs.LongOpenHashSet; import it.unimi.dsi.fastutil.objects.ObjectArrayList; import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; +import java.util.function.Supplier; import net.minecraft.server.level.ServerChunkCache; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; @@ -26,17 +34,9 @@ import net.minecraft.world.level.storage.DimensionDataStorage; import net.minecraft.world.level.storage.LevelStorageSource; import org.bxteam.divinemc.config.DivineConfig; import org.bxteam.divinemc.util.NamedAgnosticThreadFactory; +import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionException; -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; -import java.util.function.Supplier; - public final class RegionizedChunkTicking extends ServerChunkCache { private static final Logger LOGGER = LogUtils.getLogger(); public static final Executor REGION_EXECUTOR = Executors.newFixedThreadPool(DivineConfig.AsyncCategory.regionizedChunkTickingExecutorThreadCount, @@ -47,15 +47,17 @@ public final class RegionizedChunkTicking extends ServerChunkCache { super(level, levelStorageAccess, fixerUpper, structureManager, dispatcher, generator, viewDistance, simulationDistance, sync, chunkStatusListener, overworldDataStorage); } - public void execute(CompletableFuture spawns, final LevelChunk[] raw) { + @Override + protected void iterateTickingChunksFaster(final @NotNull CompletableFuture spawns) { + final ServerLevel world = this.level; + final int randomTickSpeed = world.getGameRules().getInt(GameRules.RULE_RANDOMTICKING); + final LevelChunk[] raw = world.moonrise$getEntityTickingChunks().toArray(new LevelChunk[0]); + final TickPair tickPair = computePlayerRegionsParallel(); final RegionData[] regions = tickPair.regions(); - final int regionCount = regions.length; ActivationRange.activateEntities(level); // Paper - EAR - - final int randomTickSpeed = level.getGameRules().getInt(GameRules.RULE_RANDOMTICKING); - ObjectArrayList> ticked = new ObjectArrayList<>(regionCount); + ObjectArrayList> ticked = new ObjectArrayList<>(regions.length); for (final RegionData region : regions) { if (region == null || region.isEmpty()) { @@ -87,9 +89,7 @@ public final class RegionizedChunkTicking extends ServerChunkCache { level.tickChunk(chunk, randomTickSpeed); } for (Entity entity : region.entities()) { - if (!entity.moonrise$isUpdatingSectionStatus()) { - tickEntity(entity); - } + tickEntity(entity); } return regionChunksIDs; @@ -225,21 +225,19 @@ public final class RegionizedChunkTicking extends ServerChunkCache { } private void tickEntity(Entity entity) { - if (!entity.isRemoved()) { - if (!level.tickRateManager().isEntityFrozen(entity)) { - entity.checkDespawn(); - // Paper - rewrite chunk system - Entity vehicle = entity.getVehicle(); - if (vehicle != null) { - if (!vehicle.isRemoved() && vehicle.hasPassenger(entity)) { - return; - } - - entity.stopRiding(); + if (!entity.isRemoved() && !entity.moonrise$isUpdatingSectionStatus() && !level.tickRateManager().isEntityFrozen(entity)) { + entity.checkDespawn(); + // Paper - rewrite chunk system + Entity vehicle = entity.getVehicle(); + if (vehicle != null) { + if (!vehicle.isRemoved() && vehicle.hasPassenger(entity)) { + return; } - level.guardEntityTick(level::tickNonPassenger, entity); + entity.stopRiding(); } + + level.guardEntityTick(level::tickNonPassenger, entity); } } }