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 51b4a043c09eef84bf926ea02bf588e9ce900788..8ca23e74011b70a8524fb62d1acd4344c3e71eb8 100644 --- a/net/minecraft/server/level/ServerChunkCache.java +++ b/net/minecraft/server/level/ServerChunkCache.java @@ -648,7 +648,13 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon list.clear(); } - this.iterateTickingChunksFaster(); // Paper - chunk tick iteration optimisations + // Leaf start - optimize random tick + if (org.dreeam.leaf.config.modules.opt.OptimizeRandomTick.enabled) { + this.level.randomTickSystem.tick(this.level); + } else { + this.iterateTickingChunksFaster(); // Paper - chunk tick iteration optimisations + } + // Leaf end - optimize random tick if (_boolean) { this.level.tickCustomSpawners(this.spawnEnemies, this.spawnFriendlies); } diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java index 27da552e2542153a58d6177f592cf30d858c41a9..35fb0770eb385e3837cb29711905c41b899bac8f 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(); diff --git a/net/minecraft/world/level/chunk/LevelChunk.java b/net/minecraft/world/level/chunk/LevelChunk.java index a3674ddb883eecb255279375a5e2eece7e016c0f..193009af4e477660ce36edda2658f09983941592 100644 --- a/net/minecraft/world/level/chunk/LevelChunk.java +++ b/net/minecraft/world/level/chunk/LevelChunk.java @@ -152,6 +152,61 @@ 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 = 0; + private int leaf$firstTickingSectionIndex = -1; + private int leaf$mostTickingSectionIndex = -1; + public final int leaf$tickingBlocksCount() { + if (!leaf$tickingBlocksDirty) { + return leaf$tickingBlocksCount; + } + leaf$tickingBlocksDirty = false; + int sum = 0; + leaf$firstTickingSectionIndex = -1; + leaf$mostTickingSectionIndex = -1; + int most = 0; + for (int i = 0; i < sections.length; i++) { + var list = sections[i].moonrise$getTickingBlockList(); + int size = list.size(); + if (size != 0 && leaf$firstTickingSectionIndex == -1) { + leaf$firstTickingSectionIndex = i; + } + if (size > most) { + leaf$mostTickingSectionIndex = i; + } + sum += size; + } + leaf$tickingBlocksCount = sum; + return sum; + } + public final java.util.OptionalLong leaf$getTickingPos(int idx) { + if (leaf$firstTickingSectionIndex != -1) { + int most = leaf$mostTickingSectionIndex; + var mostList = sections[most].moonrise$getTickingBlockList(); + int mostSectionSize = mostList.size(); + if (idx < mostSectionSize) { + short loc = mostList.getRaw(idx); + return java.util.OptionalLong.of(BlockPos.asLong((loc & 15) | (locX << 4), (loc >>> 8) | ((getMinSectionY() + most) << 4), ((loc >>> 4) & 15) | (locZ << 4))); + } + idx -= mostSectionSize; + for (int i = leaf$firstTickingSectionIndex; i < sections.length; i++) { + if (i == most) { + continue; + } + var list = sections[i].moonrise$getTickingBlockList(); + int size = list.size(); + if (idx < size) { + short loc = list.getRaw(idx); + return java.util.OptionalLong.of(BlockPos.asLong((loc & 15) | (locX << 4), (loc >>> 8) | ((getMinSectionY() + i) << 4), ((loc >>> 4) & 15) | (locZ << 4))); + } + 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 +471,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);