Paper/patches/server/0550-Remove-streams-from-SensorNearest.patch

89 Zeilen
4.7 KiB
Diff

2021-06-15 04:59:31 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Bjarne Koll <lynxplay101@gmail.com>
Date: Wed, 3 Mar 2021 12:48:48 +0100
Subject: [PATCH] Remove streams from SensorNearest
The behavioural nearby sensors are validated every tick on the entities
that registered the respective sensors and are therefore a good subject
to performance improvements.
More specifically this commit replaces the Stream#filter usage with
ArrayList#removeIf as the removeIf method on an array list is heavily
optimized towards a single internal array re-allocation without any
further overhead on the removeIf call.
The only negative of this change is the rather agressive diff these
patches introduce as the methods are basically being reimplemented
compared to the previous stream-based implementation.
See: https://nipafx.dev/java-stream-performance/
diff --git a/src/main/java/net/minecraft/world/entity/ai/sensing/NearestItemSensor.java b/src/main/java/net/minecraft/world/entity/ai/sensing/NearestItemSensor.java
2023-09-22 19:31:02 +02:00
index e376ac99fa84b36112f36a91f2ed9981e78c39b7..dc8622a07f7694b1537ba71f1e9af83bb4fa030c 100644
2021-06-15 04:59:31 +02:00
--- a/src/main/java/net/minecraft/world/entity/ai/sensing/NearestItemSensor.java
+++ b/src/main/java/net/minecraft/world/entity/ai/sensing/NearestItemSensor.java
@@ -28,11 +28,15 @@ public class NearestItemSensor extends Sensor<Mob> {
return true;
});
list.sort(Comparator.comparingDouble(entity::distanceToSqr));
- Optional<ItemEntity> optional = list.stream().filter((itemEntity) -> {
- return entity.wantsToPickUp(itemEntity.getItem());
- }).filter((itemEntity) -> {
2022-06-08 09:40:34 +02:00
- return itemEntity.closerThan(entity, 32.0D);
2021-06-15 04:59:31 +02:00
- }).filter(entity::hasLineOfSight).findFirst();
- brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_WANTED_ITEM, optional);
+ // Paper start - remove streams in favour of lists
+ ItemEntity nearest = null;
+ for (ItemEntity entityItem : list) {
2022-06-08 09:40:34 +02:00
+ if (entity.wantsToPickUp(entityItem.getItem()) && entityItem.closerThan(entity, 32.0D) && entity.hasLineOfSight(entityItem)) {
2021-06-15 04:59:31 +02:00
+ nearest = entityItem;
+ break;
+ }
+ }
+ brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_WANTED_ITEM, Optional.ofNullable(nearest));
+ // Paper end
}
}
diff --git a/src/main/java/net/minecraft/world/entity/ai/sensing/PlayerSensor.java b/src/main/java/net/minecraft/world/entity/ai/sensing/PlayerSensor.java
2023-09-22 19:31:02 +02:00
index 5f2a8b42e3a5abbfcc7deb23c4298d57fbcdf45d..6f0734b1321fc53c6cce3c7c6342a6b3557e24ce 100644
2021-06-15 04:59:31 +02:00
--- a/src/main/java/net/minecraft/world/entity/ai/sensing/PlayerSensor.java
+++ b/src/main/java/net/minecraft/world/entity/ai/sensing/PlayerSensor.java
@@ -21,18 +21,25 @@ public class PlayerSensor extends Sensor<LivingEntity> {
@Override
protected void doTick(ServerLevel world, LivingEntity entity) {
2021-11-24 20:33:17 +01:00
- List<Player> list = world.players().stream().filter(EntitySelector.NO_SPECTATORS).filter((player) -> {
- return entity.closerThan(player, 16.0D);
2021-06-15 04:59:31 +02:00
- }).sorted(Comparator.comparingDouble(entity::distanceToSqr)).collect(Collectors.toList());
2021-11-24 21:15:19 +01:00
+ List<Player> players = new java.util.ArrayList<>(world.players());
2021-11-24 20:33:17 +01:00
+ players.removeIf(player -> !EntitySelector.NO_SPECTATORS.test(player) || !entity.closerThan(player, 16.0D));
+ players.sort(Comparator.comparingDouble(entity::distanceTo));
2021-06-15 04:59:31 +02:00
Brain<?> brain = entity.getBrain();
- brain.setMemory(MemoryModuleType.NEAREST_PLAYERS, list);
- List<Player> list2 = list.stream().filter((player) -> {
- return isEntityTargetable(entity, player);
- }).collect(Collectors.toList());
- brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_PLAYER, list2.isEmpty() ? null : list2.get(0));
- Optional<Player> optional = list2.stream().filter((player) -> {
- return isEntityAttackable(entity, player);
- }).findFirst();
- brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_ATTACKABLE_PLAYER, optional);
+
+ brain.setMemory(MemoryModuleType.NEAREST_PLAYERS, players);
+
+ Player nearest = null, nearestTargetable = null;
+ for (Player player : players) {
+ if (Sensor.isEntityTargetable(entity, player)) {
+ if (nearest == null) nearest = player;
+ if (Sensor.isEntityAttackable(entity, player)) {
+ nearestTargetable = player;
+ break; // Both variables are assigned, no reason to loop further
+ }
+ }
+ }
+ brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_PLAYER, nearest);
+ brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_ATTACKABLE_PLAYER, nearestTargetable);
+ // Paper end
}
}