From d868052fdb89bdc52c4d2202418156f1dbb4611b Mon Sep 17 00:00:00 2001 From: wling-art Date: Thu, 22 May 2025 02:38:11 +0800 Subject: [PATCH 01/10] Reduce optimiseRandomTick new BlockPos instance create --- ...andomTick-new-BlockPos-instance-crea.patch | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 leaf-server/minecraft-patches/features/0181-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch diff --git a/leaf-server/minecraft-patches/features/0181-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch b/leaf-server/minecraft-patches/features/0181-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch new file mode 100644 index 000000000..ba86ad4c1 --- /dev/null +++ b/leaf-server/minecraft-patches/features/0181-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch @@ -0,0 +1,51 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: wling-art +Date: Thu, 22 May 2025 02:28:50 +0800 +Subject: [PATCH] Reduce optimiseRandomTick new BlockPos instance create + + +diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java +index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..25fdf36a02723e7b6e61496ee4fc7a65d1cfbec2 100644 +--- a/net/minecraft/server/level/ServerLevel.java ++++ b/net/minecraft/server/level/ServerLevel.java +@@ -946,6 +946,11 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe + this.players.stream().filter(LivingEntity::isSleeping).collect(Collectors.toList()).forEach(player -> player.stopSleepInBed(false, false)); + } + ++ // Leaf start - Reduce optimiseRandomTick new BlockPos instance create ++ private static final ThreadLocal POS_CACHE = ++ ThreadLocal.withInitial(BlockPos.MutableBlockPos::new); ++ // Leaf end - Reduce optimiseRandomTick new BlockPos instance create ++ + // Paper start - optimise random ticking + private void optimiseRandomTick(final LevelChunk chunk, final int tickSpeed) { + final LevelChunkSection[] sections = chunk.getSections(); +@@ -956,6 +961,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe + final ChunkPos cpos = chunk.getPos(); + final int offsetX = cpos.x << 4; + final int offsetZ = cpos.z << 4; ++ final BlockPos.MutableBlockPos mpos = POS_CACHE.get(); // Leaf - Reduce optimiseRandomTick new BlockPos instance create + + for (int sectionIndex = 0, sectionsLen = sections.length; sectionIndex < sectionsLen; sectionIndex++) { + // Leaf start - Micro optimizations for random tick +@@ -980,14 +986,16 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe + final int location = tickList.getRaw(index); // Leaf - Micro optimizations for random tick - 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)) | offsetY, ((location >>> 4) & 15) | offsetZ); // Leaf - Micro optimizations for random tick - no redundant mask ++ // Leaf start - Reduce optimiseRandomTick new BlockPos instance create ++ mpos.set((location & 15) | offsetX, (location >>> 8) | offsetY, ((location >>> 4) & 15) | offsetZ); // Leaf - Reduce optimiseRandomTick new BlockPos instance create ++ BlockPos immutablePos = mpos.immutable(); ++ state.randomTick(this, immutablePos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast ++ // Leaf end - Reduce optimiseRandomTick new BlockPos instance create + +- state.randomTick(this, pos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast + if (doubleTickFluids) { + final FluidState fluidState = state.getFluidState(); + if (fluidState.isRandomlyTicking()) { +- fluidState.randomTick(this, pos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast ++ fluidState.randomTick(this, immutablePos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast // Leaf - Reduce optimiseRandomTick new BlockPos instance create + } + } + } From 823c4541cdea6854ae7319f027320b5024f3278d Mon Sep 17 00:00:00 2001 From: wling-art Date: Thu, 22 May 2025 04:29:59 +0800 Subject: [PATCH 02/10] remove immutable --- ...ptimiseRandomTick-new-BlockPos-instance-crea.patch | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/leaf-server/minecraft-patches/features/0181-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch b/leaf-server/minecraft-patches/features/0181-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch index ba86ad4c1..ee06d8c11 100644 --- a/leaf-server/minecraft-patches/features/0181-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch +++ b/leaf-server/minecraft-patches/features/0181-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch @@ -1,11 +1,11 @@ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: wling-art -Date: Thu, 22 May 2025 02:28:50 +0800 +Date: Thu, 22 May 2025 04:28:33 +0800 Subject: [PATCH] Reduce optimiseRandomTick new BlockPos instance create diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java -index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..25fdf36a02723e7b6e61496ee4fc7a65d1cfbec2 100644 +index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..39dd32a90acab4a7aceb8976c4d2149f5e6ac96e 100644 --- a/net/minecraft/server/level/ServerLevel.java +++ b/net/minecraft/server/level/ServerLevel.java @@ -946,6 +946,11 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe @@ -28,7 +28,7 @@ index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..25fdf36a02723e7b6e61496ee4fc7a65 for (int sectionIndex = 0, sectionsLen = sections.length; sectionIndex < sectionsLen; sectionIndex++) { // Leaf start - Micro optimizations for random tick -@@ -980,14 +986,16 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe +@@ -980,14 +986,15 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe final int location = tickList.getRaw(index); // Leaf - Micro optimizations for random tick - no unnecessary operations final BlockState state = states.get(location); @@ -36,8 +36,7 @@ index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..25fdf36a02723e7b6e61496ee4fc7a65 - final BlockPos pos = new BlockPos((location & 15) | offsetX, (location >>> (4 + 4)) | offsetY, ((location >>> 4) & 15) | offsetZ); // Leaf - Micro optimizations for random tick - no redundant mask + // Leaf start - Reduce optimiseRandomTick new BlockPos instance create + mpos.set((location & 15) | offsetX, (location >>> 8) | offsetY, ((location >>> 4) & 15) | offsetZ); // Leaf - Reduce optimiseRandomTick new BlockPos instance create -+ BlockPos immutablePos = mpos.immutable(); -+ state.randomTick(this, immutablePos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast ++ state.randomTick(this, mpos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast + // Leaf end - Reduce optimiseRandomTick new BlockPos instance create - state.randomTick(this, pos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast @@ -45,7 +44,7 @@ index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..25fdf36a02723e7b6e61496ee4fc7a65 final FluidState fluidState = state.getFluidState(); if (fluidState.isRandomlyTicking()) { - fluidState.randomTick(this, pos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast -+ fluidState.randomTick(this, immutablePos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast // Leaf - Reduce optimiseRandomTick new BlockPos instance create ++ fluidState.randomTick(this, mpos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast // Leaf - Reduce optimiseRandomTick new BlockPos instance create } } } From 8ac4c59fe07309a949f96fd6daab551849e54ad2 Mon Sep 17 00:00:00 2001 From: wling-art Date: Thu, 22 May 2025 05:31:49 +0800 Subject: [PATCH 03/10] add config option: use-mutable-blockpos --- ...andomTick-new-BlockPos-instance-crea.patch | 15 +++++------ .../config/modules/opt/MutableBlockPos.java | 25 +++++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 leaf-server/src/main/java/org/dreeam/leaf/config/modules/opt/MutableBlockPos.java diff --git a/leaf-server/minecraft-patches/features/0181-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch b/leaf-server/minecraft-patches/features/0181-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch index ee06d8c11..0c6afd334 100644 --- a/leaf-server/minecraft-patches/features/0181-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch +++ b/leaf-server/minecraft-patches/features/0181-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch @@ -1,11 +1,11 @@ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: wling-art -Date: Thu, 22 May 2025 04:28:33 +0800 +Date: Thu, 22 May 2025 05:24:52 +0800 Subject: [PATCH] Reduce optimiseRandomTick new BlockPos instance create diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java -index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..39dd32a90acab4a7aceb8976c4d2149f5e6ac96e 100644 +index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..900e193a10e51587cdf9a0f3e4507765f3d72b0e 100644 --- a/net/minecraft/server/level/ServerLevel.java +++ b/net/minecraft/server/level/ServerLevel.java @@ -946,6 +946,11 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe @@ -24,19 +24,20 @@ index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..39dd32a90acab4a7aceb8976c4d2149f final ChunkPos cpos = chunk.getPos(); final int offsetX = cpos.x << 4; final int offsetZ = cpos.z << 4; -+ final BlockPos.MutableBlockPos mpos = POS_CACHE.get(); // Leaf - Reduce optimiseRandomTick new BlockPos instance create ++ final BlockPos.MutableBlockPos cachedMutablePos = POS_CACHE.get(); // Leaf - Reduce optimiseRandomTick new BlockPos instance create for (int sectionIndex = 0, sectionsLen = sections.length; sectionIndex < sectionsLen; sectionIndex++) { // Leaf start - Micro optimizations for random tick -@@ -980,14 +986,15 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe +@@ -980,14 +986,16 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe final int location = tickList.getRaw(index); // Leaf - Micro optimizations for random tick - 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)) | offsetY, ((location >>> 4) & 15) | offsetZ); // Leaf - Micro optimizations for random tick - no redundant mask + // Leaf start - Reduce optimiseRandomTick new BlockPos instance create -+ mpos.set((location & 15) | offsetX, (location >>> 8) | offsetY, ((location >>> 4) & 15) | offsetZ); // Leaf - Reduce optimiseRandomTick new BlockPos instance create -+ state.randomTick(this, mpos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast ++ final BlockPos pos = cachedMutablePos.set((location & 15) | offsetX, (location >>> 8) | offsetY, ((location >>> 4) & 15) | offsetZ); ++ final BlockPos finalPos = org.dreeam.leaf.config.modules.opt.MutableBlockPos.enabled ? pos : pos.immutable(); ++ state.randomTick(this, finalPos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast + // Leaf end - Reduce optimiseRandomTick new BlockPos instance create - state.randomTick(this, pos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast @@ -44,7 +45,7 @@ index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..39dd32a90acab4a7aceb8976c4d2149f final FluidState fluidState = state.getFluidState(); if (fluidState.isRandomlyTicking()) { - fluidState.randomTick(this, pos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast -+ fluidState.randomTick(this, mpos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast // Leaf - Reduce optimiseRandomTick new BlockPos instance create ++ fluidState.randomTick(this, finalPos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast // Leaf - Reduce optimiseRandomTick new BlockPos instance create } } } diff --git a/leaf-server/src/main/java/org/dreeam/leaf/config/modules/opt/MutableBlockPos.java b/leaf-server/src/main/java/org/dreeam/leaf/config/modules/opt/MutableBlockPos.java new file mode 100644 index 000000000..e79e7cd2a --- /dev/null +++ b/leaf-server/src/main/java/org/dreeam/leaf/config/modules/opt/MutableBlockPos.java @@ -0,0 +1,25 @@ +package org.dreeam.leaf.config.modules.opt; + +import org.dreeam.leaf.config.ConfigModules; +import org.dreeam.leaf.config.EnumConfigCategory; + +public class MutableBlockPos extends ConfigModules { + + public String getBasePath() { + return EnumConfigCategory.PERF.getBaseKeyName(); + } + + public static boolean enabled = true; + + @Override + public void onLoaded() { + enabled = config.getBoolean(getBasePath() + ".use-mutable-blockpos", enabled, + config.pickStringRegionBased( + """ + Reuse BlockPos to reduce memory allocation and improve tick performance. + May conflict with certain plugins or operations. Disable if position issues occur.""", + """ + 复用 BlockPos 减少内存分配,提升 tick 性能. + 可能与某些插件或操作冲突,如出现位置异常请关闭.""")); + } +} From ad1686e2bd349444488832d553431cc1c520327c Mon Sep 17 00:00:00 2001 From: wling-art Date: Fri, 23 May 2025 15:15:56 +0800 Subject: [PATCH 04/10] Update after rebase --- ...ndomTick-new-BlockPos-instance-crea.patch} | 0 .../0227-optimize-getEntityStatus.patch | 43 ------------------- 2 files changed, 43 deletions(-) rename leaf-server/minecraft-patches/features/{0181-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch => 0182-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch} (100%) delete mode 100644 leaf-server/minecraft-patches/features/0227-optimize-getEntityStatus.patch diff --git a/leaf-server/minecraft-patches/features/0181-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch b/leaf-server/minecraft-patches/features/0182-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch similarity index 100% rename from leaf-server/minecraft-patches/features/0181-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch rename to leaf-server/minecraft-patches/features/0182-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch diff --git a/leaf-server/minecraft-patches/features/0227-optimize-getEntityStatus.patch b/leaf-server/minecraft-patches/features/0227-optimize-getEntityStatus.patch deleted file mode 100644 index 683dc0b1f..000000000 --- a/leaf-server/minecraft-patches/features/0227-optimize-getEntityStatus.patch +++ /dev/null @@ -1,43 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: hayanesuru -Date: Fri, 23 May 2025 15:57:42 +0900 -Subject: [PATCH] optimize getEntityStatus - - -diff --git a/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/EntityLookup.java b/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/EntityLookup.java -index 2d24d03bbdb5ee0d862cbfff2219f58afffafe12..703bf9c2a56b262e2719a1787584de537b8f12e0 100644 ---- a/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/EntityLookup.java -+++ b/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/EntityLookup.java -@@ -93,8 +93,14 @@ public abstract class EntityLookup implements LevelEntityGetter { - if (entity == null) { - return null; - } -- final Visibility visibility = EntityLookup.getEntityStatus(entity); -- return visibility.isAccessible() ? entity : null; -+ // Leaf start - optimize getEntityStatus -+ final FullChunkStatus entityStatus = ((ChunkSystemEntity) entity).moonrise$getChunkStatus(); -+ return switch (entityStatus) { -+ case INACCESSIBLE -> null; -+ case FULL, BLOCK_TICKING, ENTITY_TICKING -> entity; -+ case null -> null; -+ }; -+ // Leaf end - optimize getEntityStatus - } - - @Override -@@ -398,7 +404,14 @@ public abstract class EntityLookup implements LevelEntityGetter { - return Visibility.TICKING; - } - final FullChunkStatus entityStatus = ((ChunkSystemEntity)entity).moonrise$getChunkStatus(); -- return Visibility.fromFullChunkStatus(entityStatus == null ? FullChunkStatus.INACCESSIBLE : entityStatus); -+ // Leaf start - optimize getEntityStatus -+ return switch (entityStatus) { -+ case INACCESSIBLE -> Visibility.HIDDEN; -+ case FULL, BLOCK_TICKING -> Visibility.TRACKED; -+ case ENTITY_TICKING -> Visibility.TICKING; -+ case null -> Visibility.HIDDEN; -+ }; -+ // Leaf end - optimize getEntityStatus - } - - protected boolean addEntity(final Entity entity, final boolean fromDisk, final boolean event) { From 911d3a5901c01c4c7b96811e930b937fb1edd51e Mon Sep 17 00:00:00 2001 From: wling-art Date: Fri, 23 May 2025 16:39:47 +0800 Subject: [PATCH 05/10] Immutable copy to prevent concurrent modification --- ...andomTick-new-BlockPos-instance-crea.patch | 62 ++++++++++++++++--- 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/leaf-server/minecraft-patches/features/0182-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch b/leaf-server/minecraft-patches/features/0182-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch index 0c6afd334..36392c2ae 100644 --- a/leaf-server/minecraft-patches/features/0182-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch +++ b/leaf-server/minecraft-patches/features/0182-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch @@ -1,11 +1,33 @@ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: wling-art -Date: Thu, 22 May 2025 05:24:52 +0800 +Date: Thu, 22 May 2025 02:28:50 +0800 Subject: [PATCH] Reduce optimiseRandomTick new BlockPos instance create +diff --git a/io/papermc/paper/redstone/RedstoneWireTurbo.java b/io/papermc/paper/redstone/RedstoneWireTurbo.java +index ff747a1ecdf3c888bca0d69de4f85dcd810b6139..8e8a68274bb168ffd28be312ef38b72170c626d3 100644 +--- a/io/papermc/paper/redstone/RedstoneWireTurbo.java ++++ b/io/papermc/paper/redstone/RedstoneWireTurbo.java +@@ -717,7 +717,7 @@ public final class RedstoneWireTurbo { + upd.parent = pos; + upd.visited = true; + identifyNode(worldIn, upd); +- nodeCache.put(pos, upd); ++ nodeCache.put(pos.immutable(), upd); // Leaf - Immutable blockPos to prevent issues with mutability + } + upd.currentState = newState; + +@@ -795,7 +795,7 @@ public final class RedstoneWireTurbo { + upd.currentState = newState; + upd.type = UpdateNode.Type.REDSTONE; + upd.visited = true; +- nodeCache.put(pos, upd); ++ nodeCache.put(pos.immutable(), upd); // Leaf - Immutable blockpos to prevent concurrent modification + propagateChanges(worldIn, upd, 0); + + // Perform the walk over all directly reachable redstone wire blocks, propagating wire value diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java -index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..900e193a10e51587cdf9a0f3e4507765f3d72b0e 100644 +index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..25fdf36a02723e7b6e61496ee4fc7a65d1cfbec2 100644 --- a/net/minecraft/server/level/ServerLevel.java +++ b/net/minecraft/server/level/ServerLevel.java @@ -946,6 +946,11 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe @@ -24,7 +46,7 @@ index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..900e193a10e51587cdf9a0f3e4507765 final ChunkPos cpos = chunk.getPos(); final int offsetX = cpos.x << 4; final int offsetZ = cpos.z << 4; -+ final BlockPos.MutableBlockPos cachedMutablePos = POS_CACHE.get(); // Leaf - Reduce optimiseRandomTick new BlockPos instance create ++ final BlockPos.MutableBlockPos mpos = POS_CACHE.get(); // Leaf - Reduce optimiseRandomTick new BlockPos instance create for (int sectionIndex = 0, sectionsLen = sections.length; sectionIndex < sectionsLen; sectionIndex++) { // Leaf start - Micro optimizations for random tick @@ -35,9 +57,9 @@ index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..900e193a10e51587cdf9a0f3e4507765 - // 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)) | offsetY, ((location >>> 4) & 15) | offsetZ); // Leaf - Micro optimizations for random tick - no redundant mask + // Leaf start - Reduce optimiseRandomTick new BlockPos instance create -+ final BlockPos pos = cachedMutablePos.set((location & 15) | offsetX, (location >>> 8) | offsetY, ((location >>> 4) & 15) | offsetZ); -+ final BlockPos finalPos = org.dreeam.leaf.config.modules.opt.MutableBlockPos.enabled ? pos : pos.immutable(); -+ state.randomTick(this, finalPos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast ++ mpos.set((location & 15) | offsetX, (location >>> 8) | offsetY, ((location >>> 4) & 15) | offsetZ); // Leaf - Reduce optimiseRandomTick new BlockPos instance create ++ BlockPos immutablePos = mpos.immutable(); ++ state.randomTick(this, immutablePos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast + // Leaf end - Reduce optimiseRandomTick new BlockPos instance create - state.randomTick(this, pos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast @@ -45,7 +67,33 @@ index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..900e193a10e51587cdf9a0f3e4507765 final FluidState fluidState = state.getFluidState(); if (fluidState.isRandomlyTicking()) { - fluidState.randomTick(this, pos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast -+ fluidState.randomTick(this, finalPos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast // Leaf - Reduce optimiseRandomTick new BlockPos instance create ++ fluidState.randomTick(this, immutablePos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast // Leaf - Reduce optimiseRandomTick new BlockPos instance create } } } +diff --git a/net/minecraft/world/level/block/SculkSpreader.java b/net/minecraft/world/level/block/SculkSpreader.java +index 54971f1d77a7850988bf5525ad4b383a3f0f2c14..3971f6e2603bf2940909e5703481d1e874fedb62 100644 +--- a/net/minecraft/world/level/block/SculkSpreader.java ++++ b/net/minecraft/world/level/block/SculkSpreader.java +@@ -165,7 +165,7 @@ public class SculkSpreader { + if (chargeCursor.charge <= 0) { + level.levelEvent(3006, chargeCursor.getPos(), 0); + } else { +- BlockPos pos1 = chargeCursor.getPos(); ++ BlockPos pos1 = chargeCursor.getPos().immutable(); // Leaf - Immutable blockPos to prevent issues with mutability + map1.computeInt(pos1, (cursorPos, charge) -> (charge == null ? 0 : charge) + chargeCursor.charge); + SculkSpreader.ChargeCursor chargeCursor1 = map.get(pos1); + if (chargeCursor1 == null) { +diff --git a/net/minecraft/world/level/redstone/ExperimentalRedstoneWireEvaluator.java b/net/minecraft/world/level/redstone/ExperimentalRedstoneWireEvaluator.java +index 1fc3aaa3732ddbae5bb24f1985fc9fb5ffd5e554..6ee46b750986d81a07872e7ab25ac025b2b7ccff 100644 +--- a/net/minecraft/world/level/redstone/ExperimentalRedstoneWireEvaluator.java ++++ b/net/minecraft/world/level/redstone/ExperimentalRedstoneWireEvaluator.java +@@ -103,7 +103,7 @@ public class ExperimentalRedstoneWireEvaluator extends RedstoneWireEvaluator { + BlockState blockState = level.getBlockState(pos); + if (blockState.is(this.wireBlock)) { + this.setPower(pos, blockState.getValue(RedStoneWireBlock.POWER), orientation); +- this.wiresToTurnOff.add(pos); ++ this.wiresToTurnOff.add(pos.immutable()); // Leaf - Immutable copy to prevent concurrent modification + } else { + this.propagateChangeToNeighbors(level, pos, 0, orientation, true); + } From 5e4c7c11034db59ad00bc232a424d366c9e29ee0 Mon Sep 17 00:00:00 2001 From: wling-art Date: Sat, 24 May 2025 12:23:13 +0800 Subject: [PATCH 06/10] Reduce optimiseRandomTick new BlockPos instance create --- ...timiseRandomTick-new-BlockPos-instance-crea.patch | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/leaf-server/minecraft-patches/features/0182-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch b/leaf-server/minecraft-patches/features/0182-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch index 36392c2ae..ae13c029b 100644 --- a/leaf-server/minecraft-patches/features/0182-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch +++ b/leaf-server/minecraft-patches/features/0182-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch @@ -27,7 +27,7 @@ index ff747a1ecdf3c888bca0d69de4f85dcd810b6139..8e8a68274bb168ffd28be312ef38b721 // Perform the walk over all directly reachable redstone wire blocks, propagating wire value diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java -index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..25fdf36a02723e7b6e61496ee4fc7a65d1cfbec2 100644 +index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..900e193a10e51587cdf9a0f3e4507765f3d72b0e 100644 --- a/net/minecraft/server/level/ServerLevel.java +++ b/net/minecraft/server/level/ServerLevel.java @@ -946,6 +946,11 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe @@ -46,7 +46,7 @@ index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..25fdf36a02723e7b6e61496ee4fc7a65 final ChunkPos cpos = chunk.getPos(); final int offsetX = cpos.x << 4; final int offsetZ = cpos.z << 4; -+ final BlockPos.MutableBlockPos mpos = POS_CACHE.get(); // Leaf - Reduce optimiseRandomTick new BlockPos instance create ++ final BlockPos.MutableBlockPos cachedMutablePos = POS_CACHE.get(); // Leaf - Reduce optimiseRandomTick new BlockPos instance create for (int sectionIndex = 0, sectionsLen = sections.length; sectionIndex < sectionsLen; sectionIndex++) { // Leaf start - Micro optimizations for random tick @@ -57,9 +57,9 @@ index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..25fdf36a02723e7b6e61496ee4fc7a65 - // 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)) | offsetY, ((location >>> 4) & 15) | offsetZ); // Leaf - Micro optimizations for random tick - no redundant mask + // Leaf start - Reduce optimiseRandomTick new BlockPos instance create -+ mpos.set((location & 15) | offsetX, (location >>> 8) | offsetY, ((location >>> 4) & 15) | offsetZ); // Leaf - Reduce optimiseRandomTick new BlockPos instance create -+ BlockPos immutablePos = mpos.immutable(); -+ state.randomTick(this, immutablePos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast ++ final BlockPos pos = cachedMutablePos.set((location & 15) | offsetX, (location >>> 8) | offsetY, ((location >>> 4) & 15) | offsetZ); ++ final BlockPos finalPos = org.dreeam.leaf.config.modules.opt.MutableBlockPos.enabled ? pos : pos.immutable(); ++ state.randomTick(this, finalPos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast + // Leaf end - Reduce optimiseRandomTick new BlockPos instance create - state.randomTick(this, pos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast @@ -67,7 +67,7 @@ index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..25fdf36a02723e7b6e61496ee4fc7a65 final FluidState fluidState = state.getFluidState(); if (fluidState.isRandomlyTicking()) { - fluidState.randomTick(this, pos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast -+ fluidState.randomTick(this, immutablePos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast // Leaf - Reduce optimiseRandomTick new BlockPos instance create ++ fluidState.randomTick(this, finalPos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast // Leaf - Reduce optimiseRandomTick new BlockPos instance create } } } From 2b07570541ab316451b913b499a6a93c3faca7f6 Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Sun, 18 Jan 2026 23:43:21 -0500 Subject: [PATCH 07/10] Fix rebase --- .../0227-optimize-getEntityStatus.patch | 43 +++++++++++++++++++ ...ndomTick-new-BlockPos-instance-crea.patch} | 22 +++++----- 2 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 leaf-server/minecraft-patches/features/0227-optimize-getEntityStatus.patch rename leaf-server/minecraft-patches/features/{0182-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch => 0295-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch} (85%) diff --git a/leaf-server/minecraft-patches/features/0227-optimize-getEntityStatus.patch b/leaf-server/minecraft-patches/features/0227-optimize-getEntityStatus.patch new file mode 100644 index 000000000..683dc0b1f --- /dev/null +++ b/leaf-server/minecraft-patches/features/0227-optimize-getEntityStatus.patch @@ -0,0 +1,43 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: hayanesuru +Date: Fri, 23 May 2025 15:57:42 +0900 +Subject: [PATCH] optimize getEntityStatus + + +diff --git a/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/EntityLookup.java b/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/EntityLookup.java +index 2d24d03bbdb5ee0d862cbfff2219f58afffafe12..703bf9c2a56b262e2719a1787584de537b8f12e0 100644 +--- a/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/EntityLookup.java ++++ b/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/EntityLookup.java +@@ -93,8 +93,14 @@ public abstract class EntityLookup implements LevelEntityGetter { + if (entity == null) { + return null; + } +- final Visibility visibility = EntityLookup.getEntityStatus(entity); +- return visibility.isAccessible() ? entity : null; ++ // Leaf start - optimize getEntityStatus ++ final FullChunkStatus entityStatus = ((ChunkSystemEntity) entity).moonrise$getChunkStatus(); ++ return switch (entityStatus) { ++ case INACCESSIBLE -> null; ++ case FULL, BLOCK_TICKING, ENTITY_TICKING -> entity; ++ case null -> null; ++ }; ++ // Leaf end - optimize getEntityStatus + } + + @Override +@@ -398,7 +404,14 @@ public abstract class EntityLookup implements LevelEntityGetter { + return Visibility.TICKING; + } + final FullChunkStatus entityStatus = ((ChunkSystemEntity)entity).moonrise$getChunkStatus(); +- return Visibility.fromFullChunkStatus(entityStatus == null ? FullChunkStatus.INACCESSIBLE : entityStatus); ++ // Leaf start - optimize getEntityStatus ++ return switch (entityStatus) { ++ case INACCESSIBLE -> Visibility.HIDDEN; ++ case FULL, BLOCK_TICKING -> Visibility.TRACKED; ++ case ENTITY_TICKING -> Visibility.TICKING; ++ case null -> Visibility.HIDDEN; ++ }; ++ // Leaf end - optimize getEntityStatus + } + + protected boolean addEntity(final Entity entity, final boolean fromDisk, final boolean event) { diff --git a/leaf-server/minecraft-patches/features/0182-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch b/leaf-server/minecraft-patches/features/0295-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch similarity index 85% rename from leaf-server/minecraft-patches/features/0182-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch rename to leaf-server/minecraft-patches/features/0295-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch index ae13c029b..87191e337 100644 --- a/leaf-server/minecraft-patches/features/0182-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch +++ b/leaf-server/minecraft-patches/features/0295-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch @@ -5,7 +5,7 @@ Subject: [PATCH] Reduce optimiseRandomTick new BlockPos instance create diff --git a/io/papermc/paper/redstone/RedstoneWireTurbo.java b/io/papermc/paper/redstone/RedstoneWireTurbo.java -index ff747a1ecdf3c888bca0d69de4f85dcd810b6139..8e8a68274bb168ffd28be312ef38b72170c626d3 100644 +index 0838b3d60e94a280a6fdd1aef413078a0ec71a1c..621564d35ce232f6168c7c4b02c786e6f5896130 100644 --- a/io/papermc/paper/redstone/RedstoneWireTurbo.java +++ b/io/papermc/paper/redstone/RedstoneWireTurbo.java @@ -717,7 +717,7 @@ public final class RedstoneWireTurbo { @@ -27,10 +27,10 @@ index ff747a1ecdf3c888bca0d69de4f85dcd810b6139..8e8a68274bb168ffd28be312ef38b721 // Perform the walk over all directly reachable redstone wire blocks, propagating wire value diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java -index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..900e193a10e51587cdf9a0f3e4507765f3d72b0e 100644 +index cf2a365bcbb1a278e5553c16ffef6d9ae81f4d41..6eb0c2d40dd98ce23d122c49393749fbd4efaff9 100644 --- a/net/minecraft/server/level/ServerLevel.java +++ b/net/minecraft/server/level/ServerLevel.java -@@ -946,6 +946,11 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe +@@ -1126,6 +1126,11 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe this.players.stream().filter(LivingEntity::isSleeping).collect(Collectors.toList()).forEach(player -> player.stopSleepInBed(false, false)); } @@ -42,7 +42,7 @@ index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..900e193a10e51587cdf9a0f3e4507765 // Paper start - optimise random ticking private void optimiseRandomTick(final LevelChunk chunk, final int tickSpeed) { final LevelChunkSection[] sections = chunk.getSections(); -@@ -956,6 +961,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe +@@ -1136,6 +1141,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe final ChunkPos cpos = chunk.getPos(); final int offsetX = cpos.x << 4; final int offsetZ = cpos.z << 4; @@ -50,7 +50,7 @@ index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..900e193a10e51587cdf9a0f3e4507765 for (int sectionIndex = 0, sectionsLen = sections.length; sectionIndex < sectionsLen; sectionIndex++) { // Leaf start - Micro optimizations for random tick -@@ -980,14 +986,16 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe +@@ -1160,14 +1166,16 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe final int location = tickList.getRaw(index); // Leaf - Micro optimizations for random tick - no unnecessary operations final BlockState state = states.get(location); @@ -72,23 +72,23 @@ index 0d8b71bbe5835187d5dfc1a301b6a01b33237bc1..900e193a10e51587cdf9a0f3e4507765 } } diff --git a/net/minecraft/world/level/block/SculkSpreader.java b/net/minecraft/world/level/block/SculkSpreader.java -index 54971f1d77a7850988bf5525ad4b383a3f0f2c14..3971f6e2603bf2940909e5703481d1e874fedb62 100644 +index 050f7595bda2e735ec112f70d6462042900b8c2c..781a942d15be4898386ac4521027bf6dec760992 100644 --- a/net/minecraft/world/level/block/SculkSpreader.java +++ b/net/minecraft/world/level/block/SculkSpreader.java -@@ -165,7 +165,7 @@ public class SculkSpreader { +@@ -156,7 +156,7 @@ public class SculkSpreader { if (chargeCursor.charge <= 0) { - level.levelEvent(3006, chargeCursor.getPos(), 0); + level.levelEvent(LevelEvent.PARTICLES_SCULK_CHARGE, chargeCursor.getPos(), 0); } else { - BlockPos pos1 = chargeCursor.getPos(); + BlockPos pos1 = chargeCursor.getPos().immutable(); // Leaf - Immutable blockPos to prevent issues with mutability - map1.computeInt(pos1, (cursorPos, charge) -> (charge == null ? 0 : charge) + chargeCursor.charge); + map1.computeInt(pos1, (blockPos, integer) -> (integer == null ? 0 : integer) + chargeCursor.charge); SculkSpreader.ChargeCursor chargeCursor1 = map.get(pos1); if (chargeCursor1 == null) { diff --git a/net/minecraft/world/level/redstone/ExperimentalRedstoneWireEvaluator.java b/net/minecraft/world/level/redstone/ExperimentalRedstoneWireEvaluator.java -index 1fc3aaa3732ddbae5bb24f1985fc9fb5ffd5e554..6ee46b750986d81a07872e7ab25ac025b2b7ccff 100644 +index 453eb859b78fe6df909ef3bc478d6fdd848a96d1..3fd136254dacceef88f3ee7007f2af00f70bace4 100644 --- a/net/minecraft/world/level/redstone/ExperimentalRedstoneWireEvaluator.java +++ b/net/minecraft/world/level/redstone/ExperimentalRedstoneWireEvaluator.java -@@ -103,7 +103,7 @@ public class ExperimentalRedstoneWireEvaluator extends RedstoneWireEvaluator { +@@ -113,7 +113,7 @@ public class ExperimentalRedstoneWireEvaluator extends RedstoneWireEvaluator { BlockState blockState = level.getBlockState(pos); if (blockState.is(this.wireBlock)) { this.setPower(pos, blockState.getValue(RedStoneWireBlock.POWER), orientation); From 2193f45f7147ccc0444501fdd4da3ff9a91ac9bc Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Mon, 19 Jan 2026 00:09:59 -0500 Subject: [PATCH 08/10] Update to latest leaf --- ...andomTick-new-BlockPos-instance-crea.patch | 27 +++++++++---------- .../config/modules/opt/MutableBlockPos.java | 10 ++++--- .../dreeam/leaf/world/RandomTickSystem.java | 12 +++++---- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/leaf-server/minecraft-patches/features/0295-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch b/leaf-server/minecraft-patches/features/0295-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch index 87191e337..6907d196f 100644 --- a/leaf-server/minecraft-patches/features/0295-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch +++ b/leaf-server/minecraft-patches/features/0295-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch @@ -5,7 +5,7 @@ Subject: [PATCH] Reduce optimiseRandomTick new BlockPos instance create diff --git a/io/papermc/paper/redstone/RedstoneWireTurbo.java b/io/papermc/paper/redstone/RedstoneWireTurbo.java -index 0838b3d60e94a280a6fdd1aef413078a0ec71a1c..621564d35ce232f6168c7c4b02c786e6f5896130 100644 +index 0838b3d60e94a280a6fdd1aef413078a0ec71a1c..bdc5f13321362d5ecd99562b48f9950ad72bbfba 100644 --- a/io/papermc/paper/redstone/RedstoneWireTurbo.java +++ b/io/papermc/paper/redstone/RedstoneWireTurbo.java @@ -717,7 +717,7 @@ public final class RedstoneWireTurbo { @@ -13,7 +13,7 @@ index 0838b3d60e94a280a6fdd1aef413078a0ec71a1c..621564d35ce232f6168c7c4b02c786e6 upd.visited = true; identifyNode(worldIn, upd); - nodeCache.put(pos, upd); -+ nodeCache.put(pos.immutable(), upd); // Leaf - Immutable blockPos to prevent issues with mutability ++ nodeCache.put(pos.immutable(), upd); // Leaf - Reduce optimiseRandomTick new BlockPos instance create - immutable } upd.currentState = newState; @@ -22,27 +22,24 @@ index 0838b3d60e94a280a6fdd1aef413078a0ec71a1c..621564d35ce232f6168c7c4b02c786e6 upd.type = UpdateNode.Type.REDSTONE; upd.visited = true; - nodeCache.put(pos, upd); -+ nodeCache.put(pos.immutable(), upd); // Leaf - Immutable blockpos to prevent concurrent modification ++ nodeCache.put(pos.immutable(), upd); // Leaf - Reduce optimiseRandomTick new BlockPos instance create - immutable propagateChanges(worldIn, upd, 0); // Perform the walk over all directly reachable redstone wire blocks, propagating wire value diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java -index cf2a365bcbb1a278e5553c16ffef6d9ae81f4d41..6eb0c2d40dd98ce23d122c49393749fbd4efaff9 100644 +index cf2a365bcbb1a278e5553c16ffef6d9ae81f4d41..02d2849798a5ff724942cfbe8a69f19970eb9d8d 100644 --- a/net/minecraft/server/level/ServerLevel.java +++ b/net/minecraft/server/level/ServerLevel.java -@@ -1126,6 +1126,11 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe +@@ -1126,6 +1126,8 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe this.players.stream().filter(LivingEntity::isSleeping).collect(Collectors.toList()).forEach(player -> player.stopSleepInBed(false, false)); } -+ // Leaf start - Reduce optimiseRandomTick new BlockPos instance create -+ private static final ThreadLocal POS_CACHE = -+ ThreadLocal.withInitial(BlockPos.MutableBlockPos::new); -+ // Leaf end - Reduce optimiseRandomTick new BlockPos instance create ++ public static final ThreadLocal POS_CACHE = ThreadLocal.withInitial(BlockPos.MutableBlockPos::new); // Leaf - Reduce optimiseRandomTick new BlockPos instance create + // Paper start - optimise random ticking private void optimiseRandomTick(final LevelChunk chunk, final int tickSpeed) { final LevelChunkSection[] sections = chunk.getSections(); -@@ -1136,6 +1141,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe +@@ -1136,6 +1138,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe final ChunkPos cpos = chunk.getPos(); final int offsetX = cpos.x << 4; final int offsetZ = cpos.z << 4; @@ -50,7 +47,7 @@ index cf2a365bcbb1a278e5553c16ffef6d9ae81f4d41..6eb0c2d40dd98ce23d122c49393749fb for (int sectionIndex = 0, sectionsLen = sections.length; sectionIndex < sectionsLen; sectionIndex++) { // Leaf start - Micro optimizations for random tick -@@ -1160,14 +1166,16 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe +@@ -1160,14 +1163,16 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe final int location = tickList.getRaw(index); // Leaf - Micro optimizations for random tick - no unnecessary operations final BlockState state = states.get(location); @@ -72,7 +69,7 @@ index cf2a365bcbb1a278e5553c16ffef6d9ae81f4d41..6eb0c2d40dd98ce23d122c49393749fb } } diff --git a/net/minecraft/world/level/block/SculkSpreader.java b/net/minecraft/world/level/block/SculkSpreader.java -index 050f7595bda2e735ec112f70d6462042900b8c2c..781a942d15be4898386ac4521027bf6dec760992 100644 +index 050f7595bda2e735ec112f70d6462042900b8c2c..3485a08070b1f021ee0dfb9b7456d0227599027b 100644 --- a/net/minecraft/world/level/block/SculkSpreader.java +++ b/net/minecraft/world/level/block/SculkSpreader.java @@ -156,7 +156,7 @@ public class SculkSpreader { @@ -80,12 +77,12 @@ index 050f7595bda2e735ec112f70d6462042900b8c2c..781a942d15be4898386ac4521027bf6d level.levelEvent(LevelEvent.PARTICLES_SCULK_CHARGE, chargeCursor.getPos(), 0); } else { - BlockPos pos1 = chargeCursor.getPos(); -+ BlockPos pos1 = chargeCursor.getPos().immutable(); // Leaf - Immutable blockPos to prevent issues with mutability ++ BlockPos pos1 = chargeCursor.getPos().immutable(); // Leaf - Reduce optimiseRandomTick new BlockPos instance create - immutable map1.computeInt(pos1, (blockPos, integer) -> (integer == null ? 0 : integer) + chargeCursor.charge); SculkSpreader.ChargeCursor chargeCursor1 = map.get(pos1); if (chargeCursor1 == null) { diff --git a/net/minecraft/world/level/redstone/ExperimentalRedstoneWireEvaluator.java b/net/minecraft/world/level/redstone/ExperimentalRedstoneWireEvaluator.java -index 453eb859b78fe6df909ef3bc478d6fdd848a96d1..3fd136254dacceef88f3ee7007f2af00f70bace4 100644 +index 453eb859b78fe6df909ef3bc478d6fdd848a96d1..a678bc008827ea128c484efd8126737bcfca93c3 100644 --- a/net/minecraft/world/level/redstone/ExperimentalRedstoneWireEvaluator.java +++ b/net/minecraft/world/level/redstone/ExperimentalRedstoneWireEvaluator.java @@ -113,7 +113,7 @@ public class ExperimentalRedstoneWireEvaluator extends RedstoneWireEvaluator { @@ -93,7 +90,7 @@ index 453eb859b78fe6df909ef3bc478d6fdd848a96d1..3fd136254dacceef88f3ee7007f2af00 if (blockState.is(this.wireBlock)) { this.setPower(pos, blockState.getValue(RedStoneWireBlock.POWER), orientation); - this.wiresToTurnOff.add(pos); -+ this.wiresToTurnOff.add(pos.immutable()); // Leaf - Immutable copy to prevent concurrent modification ++ this.wiresToTurnOff.add(pos.immutable()); // Leaf - Reduce optimiseRandomTick new BlockPos instance create - immutable } else { this.propagateChangeToNeighbors(level, pos, 0, orientation, true); } diff --git a/leaf-server/src/main/java/org/dreeam/leaf/config/modules/opt/MutableBlockPos.java b/leaf-server/src/main/java/org/dreeam/leaf/config/modules/opt/MutableBlockPos.java index e79e7cd2a..703986781 100644 --- a/leaf-server/src/main/java/org/dreeam/leaf/config/modules/opt/MutableBlockPos.java +++ b/leaf-server/src/main/java/org/dreeam/leaf/config/modules/opt/MutableBlockPos.java @@ -2,6 +2,7 @@ import org.dreeam.leaf.config.ConfigModules; import org.dreeam.leaf.config.EnumConfigCategory; +import org.dreeam.leaf.config.annotations.Experimental; public class MutableBlockPos extends ConfigModules { @@ -9,17 +10,20 @@ public String getBasePath() { return EnumConfigCategory.PERF.getBaseKeyName(); } + @Experimental public static boolean enabled = true; @Override public void onLoaded() { - enabled = config.getBoolean(getBasePath() + ".use-mutable-blockpos", enabled, + enabled = config.getBoolean(getBasePath() + ".reuse-random-ticking-blockpos", enabled, config.pickStringRegionBased( """ - Reuse BlockPos to reduce memory allocation and improve tick performance. + Experimental feature. + Reuse BlockPos to reduce memory allocation slightly and improve performance on random ticking. May conflict with certain plugins or operations. Disable if position issues occur.""", """ - 复用 BlockPos 减少内存分配,提升 tick 性能. + 实验性功能 + 复用 BlockPos 以略微减少内存分配,提升 random ticking 的性能. 可能与某些插件或操作冲突,如出现位置异常请关闭.""")); } } diff --git a/leaf-server/src/main/java/org/dreeam/leaf/world/RandomTickSystem.java b/leaf-server/src/main/java/org/dreeam/leaf/world/RandomTickSystem.java index 60768c22b..bf7ffae4f 100644 --- a/leaf-server/src/main/java/org/dreeam/leaf/world/RandomTickSystem.java +++ b/leaf-server/src/main/java/org/dreeam/leaf/world/RandomTickSystem.java @@ -41,6 +41,7 @@ public void tick(ServerLevel world) { final LevelChunk[] raw = entityTickingChunks.getRawDataUnchecked(); final int size = entityTickingChunks.size(); final boolean disableIceAndSnow = world.paperConfig().environment.disableIceAndSnow; + final BlockPos.MutableBlockPos cachedMutablePos = ServerLevel.POS_CACHE.get(); if (randomTickSpeed <= 0) { return; } @@ -56,7 +57,7 @@ public void tick(ServerLevel world) { for (int k = 0, len = queue.size(); k < len; ++k) { final long packed = q[k]; final LevelChunk chunk = raw[(int) (packed >>> SECTION_BITS)]; - tickBlock(world, chunk, (int) (packed & SECTION_MASK), random, minY); + tickBlock(world, chunk, cachedMutablePos, (int) (packed & SECTION_MASK), random, minY); } } @@ -222,21 +223,22 @@ private static void iceSnow(ServerLevel world, int size, int randomTickSpeed, Bi } } - private static void tickBlock(ServerLevel world, LevelChunk chunk, int sectionIdx, BitRandomSource random, int minSection) { + private static void tickBlock(ServerLevel world, LevelChunk chunk, BlockPos.MutableBlockPos cachedMutablePos, int sectionIdx, BitRandomSource random, int minSection) { LevelChunkSection section = chunk.getSection(sectionIdx); ShortList list = section.moonrise$getTickingBlockList(); int size = list.size(); if (size == 0) return; short location = list.getRaw(boundedNextInt(random, size)); BlockState state = section.states.get(location); - final BlockPos pos = new BlockPos((location & 15) | (chunk.locX << 4), (location >>> 8) | (minSection + (sectionIdx << 4)), ((location >>> 4) & 15) | (chunk.locZ << 4)); - state.randomTick(world, pos, random); + final BlockPos pos = cachedMutablePos.set((location & 15) | (chunk.locX << 4), (location >>> 8) | (minSection + (sectionIdx << 4)), ((location >>> 4) & 15) | (chunk.locZ << 4)); + final BlockPos finalPos = org.dreeam.leaf.config.modules.opt.MutableBlockPos.enabled ? pos : pos.immutable(); + state.randomTick(world, finalPos, random); final boolean doubleTickFluids = !ca.spottedleaf.moonrise.common.PlatformHooks.get().configFixMC224294(); if (doubleTickFluids) { final FluidState fluidState = state.getFluidState(); if (fluidState.isRandomlyTicking()) { - fluidState.randomTick(world, pos, random); + fluidState.randomTick(world, finalPos, random); } } } From 6f6228314bf6ab83364a2c1ab028443e196f7dba Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Wed, 21 Jan 2026 12:03:04 -0500 Subject: [PATCH 09/10] Cleanup --- ...seRandomTick-new-BlockPos-instance-crea.patch | 16 ++++------------ .../leaf/config/modules/opt/MutableBlockPos.java | 2 +- .../org/dreeam/leaf/world/RandomTickSystem.java | 7 +++---- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/leaf-server/minecraft-patches/features/0295-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch b/leaf-server/minecraft-patches/features/0295-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch index 6907d196f..8ae8655dc 100644 --- a/leaf-server/minecraft-patches/features/0295-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch +++ b/leaf-server/minecraft-patches/features/0295-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch @@ -27,34 +27,26 @@ index 0838b3d60e94a280a6fdd1aef413078a0ec71a1c..bdc5f13321362d5ecd99562b48f9950a // Perform the walk over all directly reachable redstone wire blocks, propagating wire value diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java -index cf2a365bcbb1a278e5553c16ffef6d9ae81f4d41..02d2849798a5ff724942cfbe8a69f19970eb9d8d 100644 +index cf2a365bcbb1a278e5553c16ffef6d9ae81f4d41..592be2e7f28c9ee270d51aeea4615bbbe55e80f0 100644 --- a/net/minecraft/server/level/ServerLevel.java +++ b/net/minecraft/server/level/ServerLevel.java @@ -1126,6 +1126,8 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe this.players.stream().filter(LivingEntity::isSleeping).collect(Collectors.toList()).forEach(player -> player.stopSleepInBed(false, false)); } -+ public static final ThreadLocal POS_CACHE = ThreadLocal.withInitial(BlockPos.MutableBlockPos::new); // Leaf - Reduce optimiseRandomTick new BlockPos instance create ++ public static final BlockPos.MutableBlockPos POS_CACHE = new BlockPos.MutableBlockPos(); // Leaf - Reduce optimiseRandomTick new BlockPos instance create + // Paper start - optimise random ticking private void optimiseRandomTick(final LevelChunk chunk, final int tickSpeed) { final LevelChunkSection[] sections = chunk.getSections(); -@@ -1136,6 +1138,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe - final ChunkPos cpos = chunk.getPos(); - final int offsetX = cpos.x << 4; - final int offsetZ = cpos.z << 4; -+ final BlockPos.MutableBlockPos cachedMutablePos = POS_CACHE.get(); // Leaf - Reduce optimiseRandomTick new BlockPos instance create - - for (int sectionIndex = 0, sectionsLen = sections.length; sectionIndex < sectionsLen; sectionIndex++) { - // Leaf start - Micro optimizations for random tick -@@ -1160,14 +1163,16 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe +@@ -1160,14 +1162,16 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe final int location = tickList.getRaw(index); // Leaf - Micro optimizations for random tick - 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)) | offsetY, ((location >>> 4) & 15) | offsetZ); // Leaf - Micro optimizations for random tick - no redundant mask + // Leaf start - Reduce optimiseRandomTick new BlockPos instance create -+ final BlockPos pos = cachedMutablePos.set((location & 15) | offsetX, (location >>> 8) | offsetY, ((location >>> 4) & 15) | offsetZ); ++ final BlockPos pos = POS_CACHE.set((location & 15) | offsetX, (location >>> 8) | offsetY, ((location >>> 4) & 15) | offsetZ); + final BlockPos finalPos = org.dreeam.leaf.config.modules.opt.MutableBlockPos.enabled ? pos : pos.immutable(); + state.randomTick(this, finalPos, simpleRandom); // Leaf - Micro optimizations for random tick - no redundant cast + // Leaf end - Reduce optimiseRandomTick new BlockPos instance create diff --git a/leaf-server/src/main/java/org/dreeam/leaf/config/modules/opt/MutableBlockPos.java b/leaf-server/src/main/java/org/dreeam/leaf/config/modules/opt/MutableBlockPos.java index 703986781..52adfee8b 100644 --- a/leaf-server/src/main/java/org/dreeam/leaf/config/modules/opt/MutableBlockPos.java +++ b/leaf-server/src/main/java/org/dreeam/leaf/config/modules/opt/MutableBlockPos.java @@ -11,7 +11,7 @@ public String getBasePath() { } @Experimental - public static boolean enabled = true; + public static boolean enabled = false; @Override public void onLoaded() { diff --git a/leaf-server/src/main/java/org/dreeam/leaf/world/RandomTickSystem.java b/leaf-server/src/main/java/org/dreeam/leaf/world/RandomTickSystem.java index bf7ffae4f..444f01060 100644 --- a/leaf-server/src/main/java/org/dreeam/leaf/world/RandomTickSystem.java +++ b/leaf-server/src/main/java/org/dreeam/leaf/world/RandomTickSystem.java @@ -41,7 +41,6 @@ public void tick(ServerLevel world) { final LevelChunk[] raw = entityTickingChunks.getRawDataUnchecked(); final int size = entityTickingChunks.size(); final boolean disableIceAndSnow = world.paperConfig().environment.disableIceAndSnow; - final BlockPos.MutableBlockPos cachedMutablePos = ServerLevel.POS_CACHE.get(); if (randomTickSpeed <= 0) { return; } @@ -57,7 +56,7 @@ public void tick(ServerLevel world) { for (int k = 0, len = queue.size(); k < len; ++k) { final long packed = q[k]; final LevelChunk chunk = raw[(int) (packed >>> SECTION_BITS)]; - tickBlock(world, chunk, cachedMutablePos, (int) (packed & SECTION_MASK), random, minY); + tickBlock(world, chunk, (int) (packed & SECTION_MASK), random, minY); } } @@ -223,14 +222,14 @@ private static void iceSnow(ServerLevel world, int size, int randomTickSpeed, Bi } } - private static void tickBlock(ServerLevel world, LevelChunk chunk, BlockPos.MutableBlockPos cachedMutablePos, int sectionIdx, BitRandomSource random, int minSection) { + private static void tickBlock(ServerLevel world, LevelChunk chunk, int sectionIdx, BitRandomSource random, int minSection) { LevelChunkSection section = chunk.getSection(sectionIdx); ShortList list = section.moonrise$getTickingBlockList(); int size = list.size(); if (size == 0) return; short location = list.getRaw(boundedNextInt(random, size)); BlockState state = section.states.get(location); - final BlockPos pos = cachedMutablePos.set((location & 15) | (chunk.locX << 4), (location >>> 8) | (minSection + (sectionIdx << 4)), ((location >>> 4) & 15) | (chunk.locZ << 4)); + final BlockPos pos = ServerLevel.POS_CACHE.set((location & 15) | (chunk.locX << 4), (location >>> 8) | (minSection + (sectionIdx << 4)), ((location >>> 4) & 15) | (chunk.locZ << 4)); final BlockPos finalPos = org.dreeam.leaf.config.modules.opt.MutableBlockPos.enabled ? pos : pos.immutable(); state.randomTick(world, finalPos, random); From af366946c09b516f46f1cfac88d89af85b68023d Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Sun, 25 Jan 2026 21:32:36 -0500 Subject: [PATCH 10/10] [ci skip] Cleanup --- ...5-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/leaf-server/minecraft-patches/features/0295-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch b/leaf-server/minecraft-patches/features/0295-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch index 8ae8655dc..3daf44462 100644 --- a/leaf-server/minecraft-patches/features/0295-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch +++ b/leaf-server/minecraft-patches/features/0295-Reduce-optimiseRandomTick-new-BlockPos-instance-crea.patch @@ -1,5 +1,5 @@ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: wling-art +From: MrlingXD <90316914+wling-art@users.noreply.github.com> Date: Thu, 22 May 2025 02:28:50 +0800 Subject: [PATCH] Reduce optimiseRandomTick new BlockPos instance create