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 between ChunkPos < - > chunkKey This patch didn't cahce SectionPos or BlockPos to chunkKey, since it needs to consider the mutable blockpos siutation. TODO: Cache block pos and section pos, whether need? diff --git a/src/main/java/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java b/src/main/java/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java index 994456ea99d78aebe41398f72019d9f6842172ff..f4b4bcbd5347c3120d8a0272e078070c44b1aa66 100644 --- a/src/main/java/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java +++ b/src/main/java/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java @@ -95,7 +95,7 @@ public final class NearbyPlayers { } public TrackedChunk getChunk(final ChunkPos pos) { - return this.byChunk.get(CoordinateUtils.getChunkKey(pos)); + return this.byChunk.get(pos.chunkKey); // Leaf - Cache chunk key } public TrackedChunk getChunk(final BlockPos pos) { @@ -107,7 +107,7 @@ public final class NearbyPlayers { } public ReferenceList getPlayers(final ChunkPos pos, final NearbyMapType type) { - return this.directByChunk[type.ordinal()].get(CoordinateUtils.getChunkKey(pos)); // Moonrise - Add direct lookup by chunk for NearbyPlayers + return this.directByChunk[type.ordinal()].get(pos.chunkKey); // Moonrise - Add direct lookup by chunk for NearbyPlayers // Leaf - Cache chunk key } public ReferenceList getPlayersByChunk(final int chunkX, final int chunkZ, final NearbyMapType type) { 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 036c1a287db04c0191e5f84b027ea68d31447cbc..753c3e99e2f677ee1704b206a3196eb05c83f4ea 100644 --- a/src/main/java/ca/spottedleaf/moonrise/common/util/CoordinateUtils.java +++ b/src/main/java/ca/spottedleaf/moonrise/common/util/CoordinateUtils.java @@ -20,15 +20,15 @@ public final class CoordinateUtils { } 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 } public static long getChunkKey(final SectionPos pos) { - return ((long)pos.getZ() << 32) | (pos.getX() & 0xFFFFFFFFL); + return ((long)pos.getZ() << 32) | (pos.getX() & 0xFFFFFFFFL); // Leaf - Cache chunk key } public static long getChunkKey(final int x, final int z) { - return ((long)z << 32) | (x & 0xFFFFFFFFL); + return ((long)z << 32) | (x & 0xFFFFFFFFL); // Leaf - Cache chunk key } public static int getChunkX(final long chunkKey) { diff --git a/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/scheduling/ChunkHolderManager.java b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/scheduling/ChunkHolderManager.java index 58d3d1a47e9f2423c467bb329c2d5f4b58a8b5ef..c4b054cee46076efdcadf4cbb16aca7f8d9c7037 100644 --- a/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/scheduling/ChunkHolderManager.java +++ b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/scheduling/ChunkHolderManager.java @@ -485,7 +485,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.chunkKey, level, identifier); // Leaf - Cache chunk key } public boolean addTicketAtLevel(final TicketType type, final int chunkX, final int chunkZ, final int level, @@ -583,7 +583,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.chunkKey, level, identifier); // Leaf - Cache chunk key } public boolean removeTicketAtLevel(final TicketType type, final int chunkX, final int chunkZ, final int level, final T identifier) { @@ -1204,7 +1204,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.chunkKey, 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) { @@ -1216,7 +1216,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.chunkKey, 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/src/main/java/ca/spottedleaf/moonrise/patches/starlight/light/StarLightInterface.java b/src/main/java/ca/spottedleaf/moonrise/patches/starlight/light/StarLightInterface.java index c64ab41198a5e0c7cbcbe6452af11f82f5938862..c272a02b48e3672b738702b338c3237241b760fb 100644 --- a/src/main/java/ca/spottedleaf/moonrise/patches/starlight/light/StarLightInterface.java +++ b/src/main/java/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 PrioritisedExecutor.Priority priority) { - final ServerChunkTasks ret = this.chunkTasks.compute(CoordinateUtils.getChunkKey(pos), (final long keyInMap, ServerChunkTasks valueInMap) -> { + final ServerChunkTasks ret = this.chunkTasks.compute(pos.chunkKey, (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/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java index 6d8fb4fe9733bd1e83af7f8c148bdb54fa26a14b..b93823983183c4ae1467a6df4f0b1fcfe60c815c 100644 --- a/src/main/java/net/minecraft/server/level/ServerLevel.java +++ b/src/main/java/net/minecraft/server/level/ServerLevel.java @@ -2475,7 +2475,7 @@ public class ServerLevel extends Level implements WorldGenLevel, ca.spottedleaf. public boolean isNaturalSpawningAllowed(ChunkPos pos) { // 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(pos)); + final ca.spottedleaf.moonrise.patches.chunk_system.scheduling.NewChunkHolder chunkHolder = this.moonrise$getChunkTaskScheduler().chunkHolderManager.getChunkHolder(pos.chunkKey); // Leaf - Cache chunk key return chunkHolder != null && chunkHolder.isEntityTickingReady(); // Paper end - rewrite chunk system } diff --git a/src/main/java/net/minecraft/world/level/ChunkPos.java b/src/main/java/net/minecraft/world/level/ChunkPos.java index 4976627098381f70b10c7370529bb5000bc5626f..4e66ef1e77ad44027ded0084f160a5802d83c68d 100644 --- a/src/main/java/net/minecraft/world/level/ChunkPos.java +++ b/src/main/java/net/minecraft/world/level/ChunkPos.java @@ -21,6 +21,7 @@ public class ChunkPos { public final int x; public final int z; public final long longKey; // Paper + public final long chunkKey; // Leaf - Cache chunk key private static final int HASH_A = 1664525; private static final int HASH_C = 1013904223; private static final int HASH_Z_XOR = -559038737; @@ -29,18 +30,21 @@ public class ChunkPos { this.x = x; this.z = z; this.longKey = asLong(this.x, this.z); // Paper + this.chunkKey = ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkKey(this.x, this.z); // Leaf - Cache chunk key } 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.chunkKey = ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkKey(this.x, this.z); // Leaf - Cache chunk key } public ChunkPos(long pos) { this.x = (int)pos; this.z = (int)(pos >> 32); this.longKey = asLong(this.x, this.z); // Paper + this.chunkKey = ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkKey(this.x, this.z); // Leaf - Cache chunk key } public static ChunkPos minFromRegion(int x, int z) {