Skip to content

Commit

Permalink
Remove areChunksLoaded check to avoid double get chunk
Browse files Browse the repository at this point in the history
In `moonrise$areChunksLoaded`, it pre loop chunks in range and do fullchunk.get calls to check if the chunk is loaded. But get chunk calls are expensive, then I moved the check in the following loop to prevent double call it.

can be around 1 to 2 more faster in test (8192 stacked minecarts with 1min spark profiling).
  • Loading branch information
Dreeam-qwq committed Oct 22, 2024
1 parent 31026f4 commit a5ddca9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 83 deletions.
85 changes: 11 additions & 74 deletions patches/server/0063-Moonrise-Optimise-checkInsideBlocks.patch
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,15 @@ Original project: https://github.com/Tuinity/Moonrise

https://github.com/Tuinity/Moonrise/commit/3fa0ff67a7c165936c5fcef366eb3a14737ab77a

This is Dreeam's tiny modified version, should be around 1~2+ times faster.

Retrieve blocks more efficiently

diff --git a/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/level/ChunkSystemLevel.java b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/level/ChunkSystemLevel.java
index efcd9057f008f0b9cf0d22b2b21d1851205841e5..8daa4ca8968cafbd320936c706abe3a2161dd75c 100644
--- a/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/level/ChunkSystemLevel.java
+++ b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/level/ChunkSystemLevel.java
@@ -19,4 +19,6 @@ public interface ChunkSystemLevel {

public void moonrise$midTickTasks();

+ public boolean moonrise$areChunksLoaded(final int fromX, final int fromZ, final int toX, final int toZ); // Moonrise - Optimise checkInsideBlocks
+
}
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
index 9ef8919e4cdac61e2e4dd2fe96aed96cb1d5959e..f3190e4b548769f56f07bae06204bb18dbc88ece 100644
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
@@ -615,6 +615,23 @@ public class ServerLevel extends Level implements WorldGenLevel, ca.spottedleaf.
this.preciseTime = this.serverLevelData.getDayTime(); // Purpur
}

+ // Moonrise start - Optimise checkInsideBlocks
+ @Override
+ public final boolean moonrise$areChunksLoaded(final int fromX, final int fromZ, final int toX, final int toZ) {
+ final net.minecraft.server.level.ServerChunkCache chunkSource = ((ServerLevel) this).getChunkSource();
+
+ for (int currZ = fromZ; currZ <= toZ; ++currZ) {
+ for (int currX = fromX; currX <= toX; ++currX) {
+ if (!chunkSource.hasChunk(currX, currZ)) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+ }
+ // Moonrise end - Optimise checkInsideBlocks
+
// Paper start
@Override
public boolean hasChunk(int chunkX, int chunkZ) {
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index 39be345524b621e6ae2eec9af9d46da3291c671b..085925162f5099609c9b82ca0103d8dd5d2cb22d 100644
index 39be345524b621e6ae2eec9af9d46da3291c671b..db0133b8e5e4e26d3f5ca48f87d6d49f8cff0532 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -1718,6 +1718,83 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
@@ -1718,6 +1718,87 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
// CraftBukkit end

protected void checkInsideBlocks() {
Expand All @@ -79,17 +42,19 @@ index 39be345524b621e6ae2eec9af9d46da3291c671b..085925162f5099609c9b82ca0103d8dd
+
+ final Level world = this.level;
+
+ if (!((ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemLevel) world).moonrise$areChunksLoaded(minChunkX, minChunkZ, maxChunkX, maxChunkZ)) {
+ return;
+ }
+
+ final int minSection = ((ca.spottedleaf.moonrise.patches.collisions.world.CollisionLevel) world).moonrise$getMinSection();
+ final net.minecraft.world.level.chunk.ChunkSource chunkSource = world.getChunkSource();
+ final BlockPos.MutableBlockPos mutablePos = new BlockPos.MutableBlockPos();
+
+ for (int currChunkZ = minChunkZ; currChunkZ <= maxChunkZ; ++currChunkZ) {
+ for (int currChunkX = minChunkX; currChunkX <= maxChunkX; ++currChunkX) {
+ final net.minecraft.world.level.chunk.LevelChunkSection[] sections = chunkSource.getChunk(currChunkX, currChunkZ, net.minecraft.world.level.chunk.status.ChunkStatus.FULL, false).getSections();
+ final net.minecraft.world.level.chunk.ChunkAccess chunk = chunkSource.getChunk(currChunkX, currChunkZ, net.minecraft.world.level.chunk.status.ChunkStatus.FULL, false);
+
+ if (chunk == null) {
+ continue;
+ }
+
+ final net.minecraft.world.level.chunk.LevelChunkSection[] sections = chunk.getSections();
+
+ for (int currChunkY = minChunkY; currChunkY <= maxChunkY; ++currChunkY) {
+ final int sectionIdx = currChunkY - minSection;
Expand Down Expand Up @@ -139,31 +104,3 @@ index 39be345524b621e6ae2eec9af9d46da3291c671b..085925162f5099609c9b82ca0103d8dd
AABB axisalignedbb = this.getBoundingBox();
BlockPos blockposition = BlockPos.containing(axisalignedbb.minX + 1.0E-7D, axisalignedbb.minY + 1.0E-7D, axisalignedbb.minZ + 1.0E-7D);
BlockPos blockposition1 = BlockPos.containing(axisalignedbb.maxX - 1.0E-7D, axisalignedbb.maxY - 1.0E-7D, axisalignedbb.maxZ - 1.0E-7D);
diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java
index a40aec3a8adf39a94d62b776e845cfc193084bbb..7f7e1b56f8844bbc54cf0aec1a52c9766e105865 100644
--- a/src/main/java/net/minecraft/world/level/Level.java
+++ b/src/main/java/net/minecraft/world/level/Level.java
@@ -236,6 +236,23 @@ public abstract class Level implements LevelAccessor, AutoCloseable, ca.spottedl
return (CraftServer) Bukkit.getServer();
}

+ // Moonrise start - Optimise checkInsideBlocks
+ @Override
+ public boolean moonrise$areChunksLoaded(final int fromX, final int fromZ, final int toX, final int toZ) {
+ final net.minecraft.server.level.ServerChunkCache chunkSource = ((ServerLevel) this).getChunkSource();
+
+ for (int currZ = fromZ; currZ <= toZ; ++currZ) {
+ for (int currX = fromX; currX <= toX; ++currX) {
+ if (!chunkSource.hasChunk(currX, currZ)) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+ }
+ // Moonrise end - Optimise checkInsideBlocks
+
// Paper start - Use getChunkIfLoadedImmediately
@Override
public boolean hasChunk(int chunkX, int chunkZ) {
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ Original project: https://github.com/Tuinity/Moonrise

https://github.com/Tuinity/Moonrise/commit/f9c99d1e32614666913bc614d019dd86e2a0b2e5

This is Dreeam's tiny modified version, should be around 1~2+ times faster.

Avoid streams for retrieving blocks

diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index 085925162f5099609c9b82ca0103d8dd5d2cb22d..1ca8c17df3f0bf6cca8831145c3282834f20a6b5 100644
index db0133b8e5e4e26d3f5ca48f87d6d49f8cff0532..08a3714c530fb375ee729e91965c65efb9e6e3d2 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -1337,9 +1337,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
Expand All @@ -25,7 +27,7 @@ index 085925162f5099609c9b82ca0103d8dd5d2cb22d..1ca8c17df3f0bf6cca8831145c328283
if (this.remainingFireTicks <= 0) {
this.setRemainingFireTicks(-this.getFireImmuneTicks());
}
@@ -1363,6 +1361,90 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
@@ -1363,6 +1361,78 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
// Paper end - detailed watchdog information
}

Expand All @@ -49,16 +51,18 @@ index 085925162f5099609c9b82ca0103d8dd5d2cb22d..1ca8c17df3f0bf6cca8831145c328283
+
+ final Level world = this.level();
+
+ if (!((ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemLevel) world).moonrise$areChunksLoaded(minChunkX, minChunkZ, maxChunkX, maxChunkZ)) {
+ return true;
+ }
+
+ final int minSection = ca.spottedleaf.moonrise.common.util.WorldUtil.getMinSection(world);
+ final net.minecraft.world.level.chunk.ChunkSource chunkSource = world.getChunkSource();
+
+ for (int currChunkZ = minChunkZ; currChunkZ <= maxChunkZ; ++currChunkZ) {
+ for (int currChunkX = minChunkX; currChunkX <= maxChunkX; ++currChunkX) {
+ final net.minecraft.world.level.chunk.LevelChunkSection[] sections = chunkSource.getChunk(currChunkX, currChunkZ, net.minecraft.world.level.chunk.status.ChunkStatus.FULL, false).getSections();
+ final net.minecraft.world.level.chunk.ChunkAccess chunk = chunkSource.getChunk(currChunkX, currChunkZ, net.minecraft.world.level.chunk.status.ChunkStatus.FULL, false);
+
+ if (chunk == null) {
+ continue;
+ }
+
+ final net.minecraft.world.level.chunk.LevelChunkSection[] sections = chunk.getSections();
+
+ for (int currChunkY = minChunkY; currChunkY <= maxChunkY; ++currChunkY) {
+ final int sectionIdx = currChunkY - minSection;
Expand Down
4 changes: 2 additions & 2 deletions patches/server/0108-Multithreaded-Tracker.patch
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,10 @@ index 05125144ce0cb50fa6ac769fa025cda010c93f14..3b40fc420ec1a8aca4c66a77f54cf628

set.clear();
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
index d57e814fd353903ed6fcec59802acb9c3455bd96..7db2f7b6da0bd32c8d0655ba4ff2c15b5fb2cd87 100644
index f3c86193df3b7e1802f1e6fb91ba87506c834d79..9211869969aad355433129c519ddd6e73f8657df 100644
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
@@ -2415,7 +2415,7 @@ public class ServerLevel extends Level implements WorldGenLevel, ca.spottedleaf.
@@ -2398,7 +2398,7 @@ public class ServerLevel extends Level implements WorldGenLevel, ca.spottedleaf.

@Override
public LevelEntityGetter<Entity> getEntities() {
Expand Down

0 comments on commit a5ddca9

Please sign in to comment.