From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: hayanesuru Date: Fri, 6 Jun 2025 20:46:10 +0900 Subject: [PATCH] optimize random tick diff --git a/net/minecraft/server/level/ServerChunkCache.java b/net/minecraft/server/level/ServerChunkCache.java index 46e171ca454253c32e22c0c18587e9a7ba19f331..36e592ff42eba050829f9c4d055c3d49a78ba813 100644 --- a/net/minecraft/server/level/ServerChunkCache.java +++ b/net/minecraft/server/level/ServerChunkCache.java @@ -633,6 +633,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon } finally { list.clear(); } + this.level.randomTickSystem.tick(this.level); // Leaf - optimize random tick this.iterateTickingChunksFaster(); // Paper - chunk tick iteration optimisations if (_boolean) { diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java index 27da552e2542153a58d6177f592cf30d858c41a9..a180612fea46ec9f7dcfe1781e985dd2e98ed513 100644 --- a/net/minecraft/server/level/ServerLevel.java +++ b/net/minecraft/server/level/ServerLevel.java @@ -1114,6 +1114,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe private int currentIceAndSnowTick = 0; protected void resetIceAndSnowTick() { this.currentIceAndSnowTick = this.simpleRandom.nextInt(16); } // Gale - Airplane - optimize random calls in chunk ticking + public org.dreeam.leaf.world.RandomTickSystem randomTickSystem = new org.dreeam.leaf.world.RandomTickSystem(); // Leaf - optimize random tick public void tickChunk(LevelChunk chunk, int randomTickSpeed) { final net.minecraft.world.level.levelgen.BitRandomSource simpleRandom = this.simpleRandom; // Paper - optimise random ticking // Leaf - Faster random generator - upcasting ChunkPos pos = chunk.getPos(); @@ -1129,7 +1130,8 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe } // Paper - Option to disable ice and snow if (randomTickSpeed > 0) { - this.optimiseRandomTick(chunk, randomTickSpeed); // Paper - optimise random ticking + if (org.dreeam.leaf.config.modules.opt.OptimizeRandomTick.enabled) randomTickSystem.tickChunk(this.simpleRandom, chunk, randomTickSpeed); // Leaf - optimize random tick + else this.optimiseRandomTick(chunk, randomTickSpeed); // Paper - optimise random ticking // Leaf - optimize random tick } } diff --git a/net/minecraft/world/level/chunk/LevelChunk.java b/net/minecraft/world/level/chunk/LevelChunk.java index a3674ddb883eecb255279375a5e2eece7e016c0f..634bf0c15eb63aeb5ff06dd9b43ecbab627b0128 100644 --- a/net/minecraft/world/level/chunk/LevelChunk.java +++ b/net/minecraft/world/level/chunk/LevelChunk.java @@ -152,6 +152,48 @@ public class LevelChunk extends ChunkAccess implements ca.spottedleaf.moonrise.p } // Gale end - Airplane - optimize random calls in chunk ticking - instead of using a random every time the chunk is ticked, define when lightning strikes preemptively + // Leaf start - optimize random tick + private boolean leaf$tickingBlocksDirty = true; + private int leaf$tickingBlocksCount; + private int leaf$firstTickingSectionIndex = -1; + public final int leaf$tickingBlocksCount() { + if (!leaf$tickingBlocksDirty) { + return leaf$tickingBlocksCount; + } + leaf$tickingBlocksDirty = false; + int sum = 0; + leaf$firstTickingSectionIndex = -1; + for (int i = 0; i < sections.length; i++) { + LevelChunkSection section = sections[i]; + int size = section.moonrise$getTickingBlockList().size(); + if (size != 0 && leaf$firstTickingSectionIndex == -1) { + leaf$firstTickingSectionIndex = i; + } + sum += size; + } + leaf$tickingBlocksCount = sum; + return sum; + } + public final java.util.OptionalLong leaf$getTickingPos(int idx) { + if (leaf$firstTickingSectionIndex != -1) { + for (int i = leaf$firstTickingSectionIndex; i < sections.length; i++) { + LevelChunkSection section = sections[i]; + var l = section.moonrise$getTickingBlockList(); + int size = l.size(); + if (idx < size) { + short loc = l.getRaw(idx); + int x = (loc & 15) | (chunkPos.x << 4); + int y = (loc >>> 8) | ((getMinSectionY() + i) << 4); + int z = ((loc >>> 4) & 15) | (chunkPos.z << 4); + return java.util.OptionalLong.of(BlockPos.asLong(x, y, z)); + } + idx -= size; + } + } + leaf$tickingBlocksDirty = true; + return java.util.OptionalLong.empty(); + } + // Leaf end - optimize random tick public LevelChunk(Level level, ChunkPos pos) { this(level, pos, UpgradeData.EMPTY, new LevelChunkTicks<>(), new LevelChunkTicks<>(), 0L, null, null, null); } @@ -416,6 +458,11 @@ public class LevelChunk extends ChunkAccess implements ca.spottedleaf.moonrise.p if (blockState == state) { return null; } else { + // Leaf start - optimize random tick + if (blockState.isRandomlyTicking() != state.isRandomlyTicking()) { + leaf$tickingBlocksDirty = true; + } + // Leaf end - optimize random tick Block block = state.getBlock(); this.heightmaps.get(Heightmap.Types.MOTION_BLOCKING).update(i, y, i2, state); this.heightmaps.get(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES).update(i, y, i2, state);