From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 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 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. 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 356ec20eff799aa12ed6b4f9e54e8721c50a0068..499cad369242f9ad724b3251538d62d8dc8d2ec8 100644 --- a/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java +++ b/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java @@ -127,7 +127,7 @@ public final class NearbyPlayers { } public TrackedChunk getChunk(final ChunkPos pos) { - return this.byChunk.get(CoordinateUtils.getChunkKey(pos)); + return this.byChunk.get(pos.longKey); // Leaf - Cache chunk key } public TrackedChunk getChunk(final BlockPos pos) { @@ -143,7 +143,7 @@ public final class NearbyPlayers { } public ReferenceList getPlayers(final ChunkPos pos, final NearbyMapType type) { - return this.directByChunk[type.ordinal()].get(CoordinateUtils.getChunkKey(pos)); + return this.directByChunk[type.ordinal()].get(pos.longKey); // Leaf - Cache chunk key } 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 +++ b/ca/spottedleaf/moonrise/patches/chunk_system/scheduling/ChunkHolderManager.java @@ -542,7 +542,7 @@ public final class ChunkHolderManager { public boolean addTicketAtLevel(final TicketType type, final ChunkPos chunkPos, final int level, final T identifier) { - return this.addTicketAtLevel(type, CoordinateUtils.getChunkKey(chunkPos), level, identifier); + return this.addTicketAtLevel(type, chunkPos.longKey, level, identifier); // Leaf - Cache chunk key } public boolean addTicketAtLevel(final TicketType type, final int chunkX, final int chunkZ, final int level, @@ -689,7 +689,7 @@ public final class ChunkHolderManager { } public boolean removeTicketAtLevel(final TicketType type, final ChunkPos chunkPos, final int level, final T identifier) { - return this.removeTicketAtLevel(type, CoordinateUtils.getChunkKey(chunkPos), level, identifier); + return this.removeTicketAtLevel(type, chunkPos.longKey, level, identifier); // Leaf - Cache chunk key } public boolean removeTicketAtLevel(final TicketType type, final int chunkX, final int chunkZ, final int level, final T identifier) { @@ -1330,7 +1330,7 @@ public final class ChunkHolderManager { } public static TicketOperation addOp(final ChunkPos chunk, final TicketType type, final int ticketLevel, final T identifier) { - return addOp(CoordinateUtils.getChunkKey(chunk), type, ticketLevel, identifier); + return addOp(chunk.longKey, type, ticketLevel, identifier); // Leaf - Cache chunk key } public static TicketOperation addOp(final int chunkX, final int chunkZ, final TicketType type, final int ticketLevel, final T identifier) { @@ -1342,7 +1342,7 @@ public final class ChunkHolderManager { } public static TicketOperation removeOp(final ChunkPos chunk, final TicketType type, final int ticketLevel, final T identifier) { - return removeOp(CoordinateUtils.getChunkKey(chunk), type, ticketLevel, identifier); + return removeOp(chunk.longKey, type, ticketLevel, identifier); // Leaf - Cache chunk key } public static TicketOperation removeOp(final int chunkX, final int chunkZ, final TicketType type, final int ticketLevel, final T identifier) { diff --git a/ca/spottedleaf/moonrise/patches/starlight/light/StarLightInterface.java b/ca/spottedleaf/moonrise/patches/starlight/light/StarLightInterface.java index 51f4dd4f583dfbd16cb00f1cb4418d1044cecb1c..e1812910d7c3941dec3d4f1c90f4cf966a631de3 100644 --- a/ca/spottedleaf/moonrise/patches/starlight/light/StarLightInterface.java +++ b/ca/spottedleaf/moonrise/patches/starlight/light/StarLightInterface.java @@ -817,7 +817,7 @@ public final class StarLightInterface { } public ServerChunkTasks queueChunkLightTask(final ChunkPos pos, final BooleanSupplier lightTask, final Priority priority) { - final ServerChunkTasks ret = this.chunkTasks.compute(CoordinateUtils.getChunkKey(pos), (final long keyInMap, ServerChunkTasks valueInMap) -> { + final ServerChunkTasks ret = this.chunkTasks.compute(pos.longKey, (final long keyInMap, ServerChunkTasks valueInMap) -> { // Leaf - Cache chunk key if (valueInMap == null) { 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 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 for (int i = 0; i < size; ++i) { final LevelChunk chunk = raw[i]; - action.accept(ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkKey(chunk.getPos())); + action.accept(chunk.coordinateKey); // Leaf - Cache chunk key } // Paper end - rewrite chunk system } diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java index b0ac6de9e0f15d234781fc43dd6fd1d5cd6c5ddf..7fb763e89e43698bfb2b9fcf6296705384e8624a 100644 --- a/net/minecraft/server/level/ServerLevel.java +++ b/net/minecraft/server/level/ServerLevel.java @@ -508,7 +508,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe @Override public final void moonrise$markChunkForPlayerTicking(final LevelChunk chunk) { final ChunkPos pos = chunk.getPos(); - if (!this.playerTickingRequests.containsKey(ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkKey(pos))) { + if (!this.playerTickingRequests.containsKey(pos.longKey)) { // Leaf - Cache chunk key return; } @@ -2612,7 +2612,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe public boolean areEntitiesActuallyLoadedAndTicking(ChunkPos chunkPos) { // Paper start - rewrite chunk system - final ca.spottedleaf.moonrise.patches.chunk_system.scheduling.NewChunkHolder chunkHolder = this.moonrise$getChunkTaskScheduler().chunkHolderManager.getChunkHolder(ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkKey(chunkPos)); + final ca.spottedleaf.moonrise.patches.chunk_system.scheduling.NewChunkHolder chunkHolder = this.moonrise$getChunkTaskScheduler().chunkHolderManager.getChunkHolder(chunkPos.longKey); // Leaf - Cache chunk key return chunkHolder != null && chunkHolder.isEntityTickingReady(); // Paper end - rewrite chunk system } @@ -2627,7 +2627,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe public boolean canSpawnEntitiesInChunk(ChunkPos chunkPos) { // Paper start - rewrite chunk system - final ca.spottedleaf.moonrise.patches.chunk_system.scheduling.NewChunkHolder chunkHolder = this.moonrise$getChunkTaskScheduler().chunkHolderManager.getChunkHolder(ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkKey(chunkPos)); + final ca.spottedleaf.moonrise.patches.chunk_system.scheduling.NewChunkHolder chunkHolder = this.moonrise$getChunkTaskScheduler().chunkHolderManager.getChunkHolder(chunkPos.longKey); // Leaf - Cache chunk key return chunkHolder != null && chunkHolder.isEntityTickingReady() && this.getWorldBorder().isWithinBounds(chunkPos); // Paper end - rewrite chunk system } diff --git a/net/minecraft/world/level/ChunkPos.java b/net/minecraft/world/level/ChunkPos.java 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 { public ChunkPos(int x, int y) { this.x = x; this.z = y; - this.longKey = asLong(this.x, this.z); // Paper + this.longKey = asLong(this.x, this.z); // Paper // Leaf - Cache chunk key - diff on change } public ChunkPos(BlockPos pos) { this.x = SectionPos.blockToSectionCoord(pos.getX()); this.z = SectionPos.blockToSectionCoord(pos.getZ()); - this.longKey = asLong(this.x, this.z); // Paper + this.longKey = asLong(this.x, this.z); // Paper // Leaf - Cache chunk key - diff on change } public ChunkPos(long packedPos) { this.x = (int)packedPos; this.z = (int)(packedPos >> 32); - this.longKey = asLong(this.x, this.z); // Paper + this.longKey = asLong(this.x, this.z); // Paper // Leaf - Cache chunk key - diff on change } public static ChunkPos minFromRegion(int chunkX, int chunkZ) { @@ -82,11 +82,11 @@ public class ChunkPos { } public static long asLong(int x, int z) { - return x & 4294967295L | (z & 4294967295L) << 32; + return x & 4294967295L | (z & 4294967295L) << 32; // Leaf - Cache chunk key - diff on change } 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 - 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 }