From 4cb40ef9a47b34cd596029aa2eb52f1ea51b78e9 Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Sat, 2 Nov 2024 22:23:33 -0400 Subject: [PATCH] Cache chunk key --- patches/server/0136-Cache-chunk-key.patch | 214 ++++++++++++++++++++++ 1 file changed, 214 insertions(+) create mode 100644 patches/server/0136-Cache-chunk-key.patch diff --git a/patches/server/0136-Cache-chunk-key.patch b/patches/server/0136-Cache-chunk-key.patch new file mode 100644 index 000000000..ce764dae6 --- /dev/null +++ b/patches/server/0136-Cache-chunk-key.patch @@ -0,0 +1,214 @@ +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 + + +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..c79fd545a5c5789f74ff1b0ee76daa0c321363d4 100644 +--- a/src/main/java/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java ++++ b/src/main/java/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java +@@ -95,19 +95,19 @@ 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) { +- return this.byChunk.get(CoordinateUtils.getChunkKey(pos)); ++ return this.byChunk.get(pos.getChunkKey()); // Leaf - Cache chunk key + } + + public ReferenceList getPlayers(final BlockPos 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.getChunkKey()); // Moonrise - Add direct lookup by chunk for NearbyPlayers // Leaf - Cache chunk key + } + + 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..68a9e15e0e73351d0b693cf52150de1a0036ca9e 100644 +--- a/src/main/java/ca/spottedleaf/moonrise/common/util/CoordinateUtils.java ++++ b/src/main/java/ca/spottedleaf/moonrise/common/util/CoordinateUtils.java +@@ -12,7 +12,7 @@ public final class CoordinateUtils { + // the chunk keys are compatible with vanilla + + public static long getChunkKey(final BlockPos pos) { +- return ((long)(pos.getZ() >> 4) << 32) | ((pos.getX() >> 4) & 0xFFFFFFFFL); ++ return ((long)(pos.getZ() >> 4) << 32) | ((pos.getX() >> 4) & 0xFFFFFFFFL); // Leaf - Cache chunk key + } + + public static long getChunkKey(final Entity entity) { +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..9dd2794bf444850e23d5079ea9bf8e29f8e97c54 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 +@@ -678,7 +678,7 @@ public final class StarLightInterface { + + @Override + public synchronized ClientChunkTasks queueBlockChange(final BlockPos pos) { +- final ClientChunkTasks tasks = this.getOrCreate(CoordinateUtils.getChunkKey(pos)); ++ final ClientChunkTasks tasks = this.getOrCreate(pos.getChunkKey()); // Leaf - Cache chunk key + tasks.addChangedPosition(pos); + return tasks; + } +@@ -782,7 +782,7 @@ public final class StarLightInterface { + + @Override + public ServerChunkTasks queueBlockChange(final BlockPos pos) { +- final ServerChunkTasks ret = this.chunkTasks.compute(CoordinateUtils.getChunkKey(pos), (final long keyInMap, ServerChunkTasks valueInMap) -> { ++ final ServerChunkTasks ret = this.chunkTasks.compute(pos.getChunkKey(), (final long keyInMap, ServerChunkTasks valueInMap) -> { // Leaf - Cache chunk key + if (valueInMap == null) { + valueInMap = new ServerChunkTasks( + keyInMap, ServerLightQueue.this.lightInterface, ServerLightQueue.this +@@ -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/core/Vec3i.java b/src/main/java/net/minecraft/core/Vec3i.java +index 7b0fc7135bc107103dcaed6dc0707b1829928fae..0e5474ec9b38ea8cb4be5e25a54c2bf03b27851c 100644 +--- a/src/main/java/net/minecraft/core/Vec3i.java ++++ b/src/main/java/net/minecraft/core/Vec3i.java +@@ -19,6 +19,7 @@ public class Vec3i implements Comparable { + protected int x; // Paper - Perf: Manually inline methods in BlockPosition; protected + protected int y; // Paper - Perf: Manually inline methods in BlockPosition; protected + protected int z; // Paper - Perf: Manually inline methods in BlockPosition; protected ++ protected long chunkKey; // Leaf - Cache chunk key + + public static Codec offsetCodec(int maxAbsValue) { + return CODEC.validate( +@@ -38,6 +39,7 @@ public class Vec3i implements Comparable { + this.x = x; + this.y = y; + this.z = z; ++ this.chunkKey = ((long)(this.z >> 4) << 32) | ((this.x >> 4) & 0xFFFFFFFFL); // Leaf - Cache chunk key + } + + @Override +@@ -71,6 +73,12 @@ public class Vec3i implements Comparable { + return this.z; + } + ++ // Leaf start - Cache chunk key ++ public final long getChunkKey() { ++ return this.chunkKey; ++ } ++ // Leaf end - Cache chunk key ++ + protected Vec3i setX(int x) { + this.x = x; + return this; +diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java +index cfe2849adfe1e7021e1d20f9dd9c1c9a9d245f6e..fb6fa088c21727726951bd5dcd3d498614e5a9b1 100644 +--- a/src/main/java/net/minecraft/server/level/ServerLevel.java ++++ b/src/main/java/net/minecraft/server/level/ServerLevel.java +@@ -2461,21 +2461,21 @@ public class ServerLevel extends Level implements WorldGenLevel, ca.spottedleaf. + + public boolean isPositionEntityTicking(BlockPos 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.getChunkKey()); // Leaf - Cache chunk key + return chunkHolder != null && chunkHolder.isEntityTickingReady(); + // Paper end - rewrite chunk system + } + + public boolean isNaturalSpawningAllowed(BlockPos 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.getChunkKey()); // Leaf - Cache chunk key + return chunkHolder != null && chunkHolder.isEntityTickingReady(); + // Paper end - rewrite chunk system + } + + 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 fa58eeec2b652f0fa251eedf11cfabde5fd3198b..c378b47034d846f425c9e519c450925911d36730 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 = ((long)this.z << 32) | (this.x & 0xFFFFFFFFL); // 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 = ((long)this.z << 32) | (this.x & 0xFFFFFFFFL); // 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 = ((long)this.z << 32) | (this.x & 0xFFFFFFFFL); // Leaf - Cache chunk key + } + + public static ChunkPos minFromRegion(int x, int z) {