From 4d814b60d249cc07ab76a4c242f596db19bd9556 Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Mon, 28 Jul 2025 20:58:25 +0800 Subject: [PATCH] Cleanup --- .../0046-Reduce-array-allocations.patch | 2 + .../features/0185-Cache-chunk-key.patch | 47 ++++++++++++++++--- .../0017-Reduce-array-allocations.patch | 2 + .../features/0046-Cache-chunk-key.patch | 10 ++-- .../modules/misc/RemoveSpigotCheckBungee.java | 2 +- .../misc/RemoveVanillaUsernameCheck.java | 2 +- 6 files changed, 52 insertions(+), 13 deletions(-) diff --git a/leaf-server/minecraft-patches/features/0046-Reduce-array-allocations.patch b/leaf-server/minecraft-patches/features/0046-Reduce-array-allocations.patch index 313ddf85..e40ec5f5 100644 --- a/leaf-server/minecraft-patches/features/0046-Reduce-array-allocations.patch +++ b/leaf-server/minecraft-patches/features/0046-Reduce-array-allocations.patch @@ -16,6 +16,8 @@ So we can cache the values result to avoid useless allocations. Cached as the array since it does not create iterator on the enhanced for loop, But the list does, and may spend more time than iterating using the array. +One-time calls are excluded from this patch, since no need. + The JMH benchmark of this patch can be found in SunBox's `CachedEnumValuesForLoop` This patch is based on the following patch: diff --git a/leaf-server/minecraft-patches/features/0185-Cache-chunk-key.patch b/leaf-server/minecraft-patches/features/0185-Cache-chunk-key.patch index b0a9e05f..97d57b10 100644 --- a/leaf-server/minecraft-patches/features/0185-Cache-chunk-key.patch +++ b/leaf-server/minecraft-patches/features/0185-Cache-chunk-key.patch @@ -3,10 +3,10 @@ From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Sat, 2 Nov 2024 04:15:20 -0400 Subject: [PATCH] Cache chunk key -Cache convert process between ChunkPos < - > chunkKey -This patch didn't cahce SectionPos or BlockPos to chunkKey, since it needs to consider the mutable blockpos siutation. +Cache convert process in ChunkPos to the chunkKey, to avoid unnecessary casting and shift operations. +This patch didn't cahce SectionPos or BlockPos to chunkKey, since they are mutable after creation. -TODO: Cache block pos and section pos, whether need? +The JMH benchmark of this patch can be found in SunBox's `CacheChunkKey` diff --git a/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java b/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java index 2a2626e90836ae52a8a686b2040843c6644b6914..283e02b32cae0bc1b0cd71602795aa48987903db 100644 @@ -30,6 +30,28 @@ index 2a2626e90836ae52a8a686b2040843c6644b6914..283e02b32cae0bc1b0cd71602795aa48 } public ReferenceList getPlayersByChunk(final int chunkX, final int chunkZ, final NearbyMapType type) { +diff --git a/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/server/ServerEntityLookup.java b/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/server/ServerEntityLookup.java +index 47a600204ae1a1e7f166284dc26a1a7afc1dbecc..cddb42e1668c675e7d1528a56ec0ccdefd81d91c 100644 +--- a/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/server/ServerEntityLookup.java ++++ b/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/server/ServerEntityLookup.java +@@ -92,7 +92,7 @@ public final class ServerEntityLookup extends EntityLookup { + ((ChunkSystemServerLevel)this.serverWorld).moonrise$getNearbyPlayers().addPlayer(player); + } + if (entity instanceof ThrownEnderpearl enderpearl) { +- this.addEnderPearl(CoordinateUtils.getChunkKey(enderpearl.chunkPosition())); ++ this.addEnderPearl(enderpearl.chunkPosition().longKey); // Leaf - Cache chunk key + } + entity.registerScheduler(); // Paper - optimise Folia entity scheduler + } +@@ -103,7 +103,7 @@ public final class ServerEntityLookup extends EntityLookup { + ((ChunkSystemServerLevel)this.serverWorld).moonrise$getNearbyPlayers().removePlayer(player); + } + if (entity instanceof ThrownEnderpearl enderpearl) { +- this.removeEnderPearl(CoordinateUtils.getChunkKey(enderpearl.chunkPosition())); ++ this.removeEnderPearl(enderpearl.chunkPosition().longKey); // Leaf - Cache chunk key + } + } + diff --git a/ca/spottedleaf/moonrise/patches/chunk_system/scheduling/ChunkHolderManager.java b/ca/spottedleaf/moonrise/patches/chunk_system/scheduling/ChunkHolderManager.java index 6ce4a98e4d3b633e3c87944c23b6b3f0ff58f159..0f5966932c4211922eccac09ab164fcb69dad582 100644 --- a/ca/spottedleaf/moonrise/patches/chunk_system/scheduling/ChunkHolderManager.java @@ -84,7 +106,7 @@ index 51f4dd4f583dfbd16cb00f1cb4418d1044cecb1c..e1812910d7c3941dec3d4f1c90f4cf96 valueInMap = new ServerChunkTasks( keyInMap, ServerLightQueue.this.lightInterface, ServerLightQueue.this, priority diff --git a/net/minecraft/server/level/DistanceManager.java b/net/minecraft/server/level/DistanceManager.java -index fd3d0f6cb53bc8b6186f0d86575f21007b2c20ed..cddeeab73e7b981701a42c5aad6b47778d46b792 100644 +index 50bc5b940812432bc472e5b272582efb8bbfc7a7..d55677f66ec7e2d4f2a96556f874e719d98376ca 100644 --- a/net/minecraft/server/level/DistanceManager.java +++ b/net/minecraft/server/level/DistanceManager.java @@ -178,7 +178,7 @@ public abstract class DistanceManager implements ca.spottedleaf.moonrise.patches @@ -128,7 +150,7 @@ index b0ac6de9e0f15d234781fc43dd6fd1d5cd6c5ddf..7fb763e89e43698bfb2b9fcf62967053 // Paper end - rewrite chunk system } diff --git a/net/minecraft/world/level/ChunkPos.java b/net/minecraft/world/level/ChunkPos.java -index 6e2b2d258e47dcca30a5ad9f4f492598f2bc21fb..b48f429320db9f440f65b5032a952d9b050af323 100644 +index 6e2b2d258e47dcca30a5ad9f4f492598f2bc21fb..6947016aa06670f340b6fb51c74ed33de9bf591b 100644 --- a/net/minecraft/world/level/ChunkPos.java +++ b/net/minecraft/world/level/ChunkPos.java @@ -54,19 +54,19 @@ public class ChunkPos { @@ -164,7 +186,20 @@ index 6e2b2d258e47dcca30a5ad9f4f492598f2bc21fb..b48f429320db9f440f65b5032a952d9b public static long asLong(BlockPos pos) { - return asLong(SectionPos.blockToSectionCoord(pos.getX()), SectionPos.blockToSectionCoord(pos.getZ())); -+ return ((pos.getX() >> 4) & 4294967295L) | (((pos.getZ() >> 4) & 4294967295L) << 32); // Leaf - Cache chunk key - diff on change - inline ++ return ((pos.getX() >> 4) & 4294967295L) | (((pos.getZ() >> 4) & 4294967295L) << 32); // Leaf - Cache chunk key - inline } public static int getX(long chunkAsLong) { +diff --git a/net/minecraft/world/waypoints/WaypointTransmitter.java b/net/minecraft/world/waypoints/WaypointTransmitter.java +index 5d1c933dfa862d0733777d305563a89ea7827f07..c4db265b469cbd37530c3391f99d08ecac59d195 100644 +--- a/net/minecraft/world/waypoints/WaypointTransmitter.java ++++ b/net/minecraft/world/waypoints/WaypointTransmitter.java +@@ -34,7 +34,7 @@ public interface WaypointTransmitter extends Waypoint { + static boolean isChunkVisible(ChunkPos pos, ServerPlayer player) { + // Paper start - rewrite chunk system + final ca.spottedleaf.moonrise.patches.chunk_system.player.RegionizedPlayerChunkLoader.PlayerChunkLoaderData playerChunkLoader = ((ca.spottedleaf.moonrise.patches.chunk_system.player.ChunkSystemServerPlayer)player).moonrise$getChunkLoader(); +- return playerChunkLoader != null && playerChunkLoader.getSentChunksRaw().contains(ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkKey(pos)); ++ return playerChunkLoader != null && playerChunkLoader.getSentChunksRaw().contains(pos.longKey); // Leaf - Cache chunk key + // Paper end - rewrite chunk system + } + diff --git a/leaf-server/paper-patches/features/0017-Reduce-array-allocations.patch b/leaf-server/paper-patches/features/0017-Reduce-array-allocations.patch index 3cbcc56e..d03d8af8 100644 --- a/leaf-server/paper-patches/features/0017-Reduce-array-allocations.patch +++ b/leaf-server/paper-patches/features/0017-Reduce-array-allocations.patch @@ -16,6 +16,8 @@ So we can cache the values result to avoid useless allocations. Cached as the array since it does not create iterator on the enhanced for loop, But the list does, and may spend more time than iterating using the array. +One-time calls are excluded from this patch, since no need. + The JMH benchmark of this patch can be found in SunBox's `CachedEnumValuesForLoop` This patch is based on the following patch: diff --git a/leaf-server/paper-patches/features/0046-Cache-chunk-key.patch b/leaf-server/paper-patches/features/0046-Cache-chunk-key.patch index 0bffa125..f877bf13 100644 --- a/leaf-server/paper-patches/features/0046-Cache-chunk-key.patch +++ b/leaf-server/paper-patches/features/0046-Cache-chunk-key.patch @@ -3,13 +3,13 @@ From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Sat, 2 Nov 2024 04:15:20 -0400 Subject: [PATCH] Cache chunk key -Cache convert process between ChunkPos < - > chunkKey -This patch didn't cahce SectionPos or BlockPos to chunkKey, since it needs to consider the mutable blockpos siutation. +Cache convert process in ChunkPos to the chunkKey, to avoid unnecessary casting and shift operations. +This patch didn't cahce SectionPos or BlockPos to chunkKey, since they are mutable after creation. -TODO: Cache block pos and section pos, whether need? +The JMH benchmark of this patch can be found in SunBox's `CacheChunkKey` diff --git a/src/main/java/ca/spottedleaf/moonrise/common/util/CoordinateUtils.java b/src/main/java/ca/spottedleaf/moonrise/common/util/CoordinateUtils.java -index bb5b9c9cb0c73edce1dbe3758ee2db0fcc8f4e40..7e3ec4b74406e040ae79d63845fc57bfa3d78a80 100644 +index bb5b9c9cb0c73edce1dbe3758ee2db0fcc8f4e40..e9ed38430e15d51692004dbf2a1110abc77b31d2 100644 --- a/src/main/java/ca/spottedleaf/moonrise/common/util/CoordinateUtils.java +++ b/src/main/java/ca/spottedleaf/moonrise/common/util/CoordinateUtils.java @@ -21,7 +21,7 @@ public final class CoordinateUtils { @@ -17,7 +17,7 @@ index bb5b9c9cb0c73edce1dbe3758ee2db0fcc8f4e40..7e3ec4b74406e040ae79d63845fc57bf public static long getChunkKey(final ChunkPos pos) { - return ((long)pos.z << 32) | (pos.x & 0xFFFFFFFFL); -+ return ((long)pos.z << 32) | (pos.x & 0xFFFFFFFFL); // Leaf - Cache chunk key ++ return ((long)pos.z << 32) | (pos.x & 0xFFFFFFFFL); // Leaf - Cache chunk key - diff on change } public static long getChunkKey(final SectionPos pos) { diff --git a/leaf-server/src/main/java/org/dreeam/leaf/config/modules/misc/RemoveSpigotCheckBungee.java b/leaf-server/src/main/java/org/dreeam/leaf/config/modules/misc/RemoveSpigotCheckBungee.java index 42bfa036..d252a7c3 100644 --- a/leaf-server/src/main/java/org/dreeam/leaf/config/modules/misc/RemoveSpigotCheckBungee.java +++ b/leaf-server/src/main/java/org/dreeam/leaf/config/modules/misc/RemoveSpigotCheckBungee.java @@ -9,7 +9,7 @@ public class RemoveSpigotCheckBungee extends ConfigModules { return EnumConfigCategory.MISC.getBaseKeyName() + ".remove-spigot-check-bungee-config"; } - 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/config/modules/misc/RemoveVanillaUsernameCheck.java b/leaf-server/src/main/java/org/dreeam/leaf/config/modules/misc/RemoveVanillaUsernameCheck.java index dcb0d23c..d035246a 100644 --- a/leaf-server/src/main/java/org/dreeam/leaf/config/modules/misc/RemoveVanillaUsernameCheck.java +++ b/leaf-server/src/main/java/org/dreeam/leaf/config/modules/misc/RemoveVanillaUsernameCheck.java @@ -9,7 +9,7 @@ public class RemoveVanillaUsernameCheck extends ConfigModules { return EnumConfigCategory.MISC.getBaseKeyName() + ".remove-vanilla-username-check"; } - public static boolean enabled = true; + public static boolean enabled = false; @Override public void onLoaded() {