From d112c777b5cd9ff893ee7bcd87c60c8c8e9dc38b Mon Sep 17 00:00:00 2001 From: Taiyou06 Date: Sat, 22 Mar 2025 12:02:36 +0100 Subject: [PATCH] micro optimizations for random tick (Author: @Altiami) --- ...-Micro-optimizations-for-random-tick.patch | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 leaf-server/minecraft-patches/features/0153-Micro-optimizations-for-random-tick.patch diff --git a/leaf-server/minecraft-patches/features/0153-Micro-optimizations-for-random-tick.patch b/leaf-server/minecraft-patches/features/0153-Micro-optimizations-for-random-tick.patch new file mode 100644 index 00000000..c677532c --- /dev/null +++ b/leaf-server/minecraft-patches/features/0153-Micro-optimizations-for-random-tick.patch @@ -0,0 +1,72 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Taiyou06 +Date: Sat, 22 Mar 2025 03:02:52 +0100 +Subject: [PATCH] Micro-optimizations for random tick + + +diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java +index b5a61261083ddab70582c1a1d5cac0b9ced9b652..ab8cc4e2c74d4b214fd24f5ea415a8dc8f24bb57 100644 +--- a/net/minecraft/server/level/ServerLevel.java ++++ b/net/minecraft/server/level/ServerLevel.java +@@ -924,7 +924,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe + // Paper start - optimise random ticking + private void optimiseRandomTick(final LevelChunk chunk, final int tickSpeed) { + final LevelChunkSection[] sections = chunk.getSections(); +- final int minSection = ca.spottedleaf.moonrise.common.util.WorldUtil.getMinSection((ServerLevel)(Object)this); ++ final int minSection = ca.spottedleaf.moonrise.common.util.WorldUtil.getMinSection(this); // Leaf - no redundant cast + final net.minecraft.world.level.levelgen.BitRandomSource simpleRandom = this.simpleRandom; // Leaf - Faster random generator - upcasting + final boolean doubleTickFluids = !ca.spottedleaf.moonrise.common.PlatformHooks.get().configFixMC224294(); + +@@ -933,41 +933,41 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe + final int offsetZ = cpos.z << 4; + + for (int sectionIndex = 0, sectionsLen = sections.length; sectionIndex < sectionsLen; sectionIndex++) { +- final int offsetY = (sectionIndex + minSection) << 4; ++ // Leaf start - continue early + final LevelChunkSection section = sections[sectionIndex]; +- final net.minecraft.world.level.chunk.PalettedContainer states = section.states; + if (!section.isRandomlyTickingBlocks()) { + continue; + } ++ final int offsetY = (sectionIndex + minSection) << 4; ++ final net.minecraft.world.level.chunk.PalettedContainer states = section.states; ++ // Leaf end + +- final ca.spottedleaf.moonrise.common.list.ShortList tickList = ((ca.spottedleaf.moonrise.patches.block_counting.BlockCountingChunkSection)section).moonrise$getTickingBlockList(); ++ final ca.spottedleaf.moonrise.common.list.ShortList tickList = section.moonrise$getTickingBlockList(); // Leaf - no redundant cast + + for (int i = 0; i < tickSpeed; ++i) { +- final int tickingBlocks = tickList.size(); + final int index = simpleRandom.nextInt() & ((16 * 16 * 16) - 1); + +- if (index >= tickingBlocks) { ++ if (index >= tickList.size()) { // Leaf - inline one-time value + // most of the time we fall here + continue; + } + +- final int location = (int)tickList.getRaw(index) & 0xFFFF; ++ final int location = tickList.getRaw(index); // Leaf - no unnecessary operations + final BlockState state = states.get(location); + + // do not use a mutable pos, as some random tick implementations store the input without calling immutable()! +- final BlockPos pos = new BlockPos((location & 15) | offsetX, ((location >>> (4 + 4)) & 15) | offsetY, ((location >>> 4) & 15) | offsetZ); ++ final BlockPos pos = new BlockPos((location & 15) | offsetX, (location >>> (4 + 4)) | offsetY, ((location >>> 4) & 15) | offsetZ); // Leaf - no redundant mask + +- state.randomTick((ServerLevel)(Object)this, pos, simpleRandom); ++ state.randomTick(this, pos, simpleRandom); // Leaf - no redundant cast + if (doubleTickFluids) { + final FluidState fluidState = state.getFluidState(); + if (fluidState.isRandomlyTicking()) { +- fluidState.randomTick((ServerLevel)(Object)this, pos, simpleRandom); ++ fluidState.randomTick(this, pos, simpleRandom); // Leaf - no redundant cast + } + } + } + } +- +- return; ++ // Leaf - no redundant return + } + // Paper end - optimise random ticking +