diff --git a/leaf-server/minecraft-patches/features/0089-Smart-sort-entities-in-NearestLivingEntitySensor.patch b/leaf-server/minecraft-patches/features/0089-Smart-sort-entities-in-NearestLivingEntitySensor.patch index 96454dc8..46b3d513 100644 --- a/leaf-server/minecraft-patches/features/0089-Smart-sort-entities-in-NearestLivingEntitySensor.patch +++ b/leaf-server/minecraft-patches/features/0089-Smart-sort-entities-in-NearestLivingEntitySensor.patch @@ -12,10 +12,10 @@ In non-strict test, this can give ~60-110% improvement (524ms on Paper, 204ms on under 625 villagers situation. diff --git a/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java b/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java -index b0c5e41fefc7c9adf1a61bd5b52861736657d37e..ef87b95d53e5ea2555778e6020ea07a11c474961 100644 +index b0c5e41fefc7c9adf1a61bd5b52861736657d37e..59dabdf7279c3e96a8d6ce40e5840d526487bf22 100644 --- a/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java +++ b/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java -@@ -13,17 +13,27 @@ import net.minecraft.world.entity.ai.memory.NearestVisibleLivingEntities; +@@ -13,17 +13,26 @@ import net.minecraft.world.entity.ai.memory.NearestVisibleLivingEntities; import net.minecraft.world.phys.AABB; public class NearestLivingEntitySensor extends Sensor { @@ -36,15 +36,12 @@ index b0c5e41fefc7c9adf1a61bd5b52861736657d37e..ef87b95d53e5ea2555778e6020ea07a1 - ); - entitiesOfClass.sort(Comparator.comparingDouble(entity::distanceToSqr)); + // Leaf start - Smart sort entities in NearestLivingEntitySensor -+ double rangeSqr = attributeValue * attributeValue; -+ List entities = level.getEntitiesOfClass(LivingEntity.class, aabb, e -> e != entity && e.isAlive() && entity.distanceToSqr(e) <= rangeSqr); -+ LivingEntity[] sorted = this.sorter.sort(entities, entity, LivingEntity.class); -+ List sortedList = java.util.Arrays.asList(sorted); ++ it.unimi.dsi.fastutil.objects.ObjectArrayList entitiesOfClass = new it.unimi.dsi.fastutil.objects.ObjectArrayList<>(); ++ ((ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemLevel) level).moonrise$getEntityLookup().getEntities(LivingEntity.class, entity, aabb, entitiesOfClass, LivingEntity::isAlive); ++ sorter.sort(entitiesOfClass.elements(), entitiesOfClass.size(), entity.position()); Brain brain = entity.getBrain(); -- brain.setMemory(MemoryModuleType.NEAREST_LIVING_ENTITIES, entitiesOfClass); -- brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES, new NearestVisibleLivingEntities(level, entity, entitiesOfClass)); -+ brain.setMemory(MemoryModuleType.NEAREST_LIVING_ENTITIES, sortedList); -+ brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES, new NearestVisibleLivingEntities(level, entity, sortedList)); + brain.setMemory(MemoryModuleType.NEAREST_LIVING_ENTITIES, entitiesOfClass); + brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES, new NearestVisibleLivingEntities(level, entity, entitiesOfClass)); + // Leaf end - Smart sort entities in NearestLivingEntitySensor } diff --git a/leaf-server/minecraft-patches/features/0162-Smart-sort-items-in-NearestItemSensor.patch b/leaf-server/minecraft-patches/features/0162-Smart-sort-items-in-NearestItemSensor.patch index 0907f553..302c2d1b 100644 --- a/leaf-server/minecraft-patches/features/0162-Smart-sort-items-in-NearestItemSensor.patch +++ b/leaf-server/minecraft-patches/features/0162-Smart-sort-items-in-NearestItemSensor.patch @@ -5,15 +5,16 @@ Subject: [PATCH] Smart sort items in NearestItemSensor diff --git a/net/minecraft/world/entity/ai/sensing/NearestItemSensor.java b/net/minecraft/world/entity/ai/sensing/NearestItemSensor.java -index 09fd13e2d958da8326276c4dadf25bf488aff5ac..651797720f7fc6ff9dc614f4de56053c304b1170 100644 +index 09fd13e2d958da8326276c4dadf25bf488aff5ac..a5ec595efe568cccee53b016531a56f721bc99f2 100644 --- a/net/minecraft/world/entity/ai/sensing/NearestItemSensor.java +++ b/net/minecraft/world/entity/ai/sensing/NearestItemSensor.java -@@ -16,6 +16,12 @@ public class NearestItemSensor extends Sensor { +@@ -16,6 +16,13 @@ public class NearestItemSensor extends Sensor { private static final long Y_RANGE = 16L; public static final int MAX_DISTANCE_TO_WANTED_ITEM = 32; + // Leaf start - Smart sort items in NearestItemSensor + private final org.dreeam.leaf.util.FastBitRadixSort itemSorter; ++ private static final double MAX_DIST_SQ = (double) MAX_DISTANCE_TO_WANTED_ITEM * MAX_DISTANCE_TO_WANTED_ITEM; + public NearestItemSensor() { + this.itemSorter = new org.dreeam.leaf.util.FastBitRadixSort(); + } @@ -21,16 +22,22 @@ index 09fd13e2d958da8326276c4dadf25bf488aff5ac..651797720f7fc6ff9dc614f4de56053c @Override public Set> requires() { return ImmutableSet.of(MemoryModuleType.NEAREST_VISIBLE_WANTED_ITEM); -@@ -25,10 +31,10 @@ public class NearestItemSensor extends Sensor { +@@ -24,8 +31,16 @@ public class NearestItemSensor extends Sensor { + @Override protected void doTick(ServerLevel level, Mob entity) { Brain brain = entity.getBrain(); - List entitiesOfClass = level.getEntitiesOfClass(ItemEntity.class, entity.getBoundingBox().inflate(32.0, 16.0, 32.0), itemEntity -> itemEntity.closerThan(entity, MAX_DISTANCE_TO_WANTED_ITEM) && entity.wantsToPickUp(level, itemEntity.getItem())); // Paper - Perf: Move predicate into getEntities +- List entitiesOfClass = level.getEntitiesOfClass(ItemEntity.class, entity.getBoundingBox().inflate(32.0, 16.0, 32.0), itemEntity -> itemEntity.closerThan(entity, MAX_DISTANCE_TO_WANTED_ITEM) && entity.wantsToPickUp(level, itemEntity.getItem())); // Paper - Perf: Move predicate into getEntities - entitiesOfClass.sort(Comparator.comparingDouble(entity::distanceToSqr)); -+ ItemEntity[] sortedItems = this.itemSorter.sort(entitiesOfClass, entity, ItemEntity.class); // Leaf - Smart sort items in NearestItemSensor ++ // Leaf start - Smart sort items in NearestItemSensor ++ net.minecraft.core.Position pos = entity.position(); ++ double x = pos.x(); ++ double y = pos.y(); ++ double z = pos.z(); ++ net.minecraft.world.phys.AABB boundingBox = entity.getBoundingBox().inflate(32.0, 16.0, 32.0); ++ it.unimi.dsi.fastutil.objects.ObjectArrayList entitiesOfClass = new it.unimi.dsi.fastutil.objects.ObjectArrayList<>(); ++ ((ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemLevel) level).moonrise$getEntityLookup().getEntities(ItemEntity.class, null, boundingBox, entitiesOfClass, (ItemEntity itemEntity) -> itemEntity.distanceToSqr(x, y, z) < MAX_DIST_SQ && entity.wantsToPickUp(level, itemEntity.getItem())); // Paper - Perf: Move predicate into getEntities ++ itemSorter.sort(entitiesOfClass.elements(), entitiesOfClass.size(), pos); ++ // Leaf end - Smart sort items in NearestItemSensor // Paper start - Perf: remove streams from hot code ItemEntity nearest = null; -- for (final ItemEntity itemEntity : entitiesOfClass) { -+ for (final ItemEntity itemEntity : sortedItems) { // Leaf - Smart sort items in NearestItemSensor - if (entity.hasLineOfSight(itemEntity)) { // Paper - Perf: Move predicate into getEntities - nearest = itemEntity; - break; + for (final ItemEntity itemEntity : entitiesOfClass) {