From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Taiyou06 Date: Sat, 12 Jul 2025 02:00:43 +0200 Subject: [PATCH] Optimise getEntities Co-authored by: Martijn Muijsers diff --git a/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java b/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java index 7f508c50e451a0689218cd36e6ac993f87092c04..a6c1f450f32fa688b102140963fd5bfb6b4e6ddf 100644 --- a/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java +++ b/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java @@ -546,6 +546,15 @@ public final class ChunkEntitySlices { final BasicEntityList[] entitiesBySection = this.entitiesBySection; + // Leaf start - Optimise getEntities + // Cache AABB fields to local variables to reduce field accesses inside the hot loop. + final double boxMinX = box.minX; + final double boxMinY = box.minY; + final double boxMinZ = box.minZ; + final double boxMaxX = box.maxX; + final double boxMaxY = box.maxY; + final double boxMaxZ = box.maxZ; + for (int section = min; section <= max; ++section) { final BasicEntityList list = entitiesBySection[section - minSection]; @@ -554,11 +563,19 @@ public final class ChunkEntitySlices { } final Entity[] storage = list.storage; + final int len = Math.min(storage.length, list.size()); - for (int i = 0, len = Math.min(storage.length, list.size()); i < len; ++i) { + for (int i = 0; i < len; ++i) { final Entity entity = storage[i]; - if (entity == null || entity == except || !entity.getBoundingBox().intersects(box)) { + if (entity == null || entity == except) { + continue; + } + + // Inline AABB#intersects to avoid a method call and use the cached AABB fields. + final AABB entityBB = entity.getBoundingBox(); + if (entityBB.maxX <= boxMinX || entityBB.minX >= boxMaxX || entityBB.maxY <= boxMinY || entityBB.minY >= boxMaxY || entityBB.maxZ <= boxMinZ || entityBB.minZ >= boxMaxZ) { + // Leaf end - Optimise getEntities continue; } @@ -585,19 +602,34 @@ public final class ChunkEntitySlices { final BasicEntityList[] entitiesBySection = this.entitiesBySection; + // Leaf start - Optimise getEntities + // Cache AABB fields to local variables to reduce field accesses inside the hot loop. + final double boxMinX = box.minX; + final double boxMinY = box.minY; + final double boxMinZ = box.minZ; + final double boxMaxX = box.maxX; + final double boxMaxY = box.maxY; + final double boxMaxZ = box.maxZ; + for (int section = min; section <= max; ++section) { final BasicEntityList list = entitiesBySection[section - minSection]; - if (list == null) { continue; } final Entity[] storage = list.storage; + final int len = Math.min(storage.length, list.size()); - for (int i = 0, len = Math.min(storage.length, list.size()); i < len; ++i) { + for (int i = 0; i < len; ++i) { final Entity entity = storage[i]; + if (entity == null || entity == except) { + continue; + } - if (entity == null || entity == except || !entity.getBoundingBox().intersects(box)) { + // Inline AABB#intersects to avoid a method call and use the cached AABB fields. + final AABB entityBB = entity.getBoundingBox(); + if (entityBB.maxX <= boxMinX || entityBB.minX >= boxMaxX || entityBB.maxY <= boxMinY || entityBB.minY >= boxMaxY || entityBB.maxZ <= boxMinZ || entityBB.minZ >= boxMaxZ) { + // Leaf end - Optimise getEntities continue; } diff --git a/net/minecraft/world/entity/LivingEntity.java b/net/minecraft/world/entity/LivingEntity.java index c63c2d1b6bd005eda12c8c1f7cec2a29856fed14..2107514e75b646d9af7471848252924206cbbb9b 100644 --- a/net/minecraft/world/entity/LivingEntity.java +++ b/net/minecraft/world/entity/LivingEntity.java @@ -2276,7 +2276,7 @@ public abstract class LivingEntity extends Entity implements Attackable, Waypoin @Override public boolean isAlive() { - return !this.isRemoved() && this.getHealth() > 0.0F && !this.dead; // Paper - Check this.dead + return !this.dead && !this.isRemoved() && this.getHealth() > 0.0F; // Paper - Check this.dead // Leaf - Optimise getEntities - check the cheapest first } public boolean isLookingAtMe(LivingEntity entity, double tolerance, boolean scaleByDistance, boolean visual, double... yValues) {