9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-26 18:39:23 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0271-Optimise-getEntities.patch
Dreeam 9a4efaa230 Drop patch that causes performance regression
Originally vanilla logic is to use stream, and Mojang switched it to Guava's Collections2
since 1.21.4. It is much faster than using stream or manually adding to a new ArrayList.
Manually adding to a new ArrayList requires allocating a new object array. However, the Collections2
lazy handles filter condition on iteration, so much better.
2025-08-04 19:25:56 +08:00

84 lines
4.1 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Taiyou06 <kaandindar21@gmail.com>
Date: Sat, 12 Jul 2025 02:00:43 +0200
Subject: [PATCH] Optimise-getEntities
Co-authored by: Martijn Muijsers <martijnmuijsers@live.nl>
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 5ebc1b2cf186b512da5b62fedba16b612f2fa6ed..14dc37f0ed08b4eb4c7740ec68d23df0060c0955 100644
--- a/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java
+++ b/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java
@@ -579,6 +579,14 @@ public final class ChunkEntitySlices {
final BasicEntityList<Entity>[] entitiesBySection = this.entitiesBySection;
+ // 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<Entity> list = entitiesBySection[section - minSection];
@@ -587,11 +595,18 @@ 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) {
continue;
}
@@ -618,19 +633,32 @@ public final class ChunkEntitySlices {
final BasicEntityList<Entity>[] entitiesBySection = this.entitiesBySection;
+ // 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<Entity> 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) {
continue;
}