mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-09 19:49:35 +01:00
Improve performance of PoiCompetitorScan by unrolling stream (#11871)
This commit is contained in:
parent
4106da712c
commit
6ab13521b7
1 changed files with 42 additions and 0 deletions
|
@ -0,0 +1,42 @@
|
||||||
|
--- a/net/minecraft/world/entity/ai/behavior/PoiCompetitorScan.java
|
||||||
|
+++ b/net/minecraft/world/entity/ai/behavior/PoiCompetitorScan.java
|
||||||
|
@@ -22,13 +_,32 @@
|
||||||
|
level.getPoiManager()
|
||||||
|
.getType(globalPos.pos())
|
||||||
|
.ifPresent(
|
||||||
|
- poi -> instance.<List<LivingEntity>>get(nearestLivingEntities)
|
||||||
|
- .stream()
|
||||||
|
- .filter(entity -> entity instanceof Villager && entity != villager)
|
||||||
|
- .map(entity -> (Villager)entity)
|
||||||
|
- .filter(LivingEntity::isAlive)
|
||||||
|
- .filter(v -> competesForSameJobsite(globalPos, poi, v))
|
||||||
|
- .reduce(villager, PoiCompetitorScan::selectWinner)
|
||||||
|
+ // Paper start - Improve performance of PoiCompetitorScan by unrolling stream
|
||||||
|
+ // The previous logic used Stream#reduce to simulate a form of single-iteration bubble sort
|
||||||
|
+ // in which the "winning" villager would maintain MemoryModuleType.JOB_SITE while all others
|
||||||
|
+ // would lose said memory module type by passing each "current winner" and incoming next
|
||||||
|
+ // villager to #selectWinner.
|
||||||
|
+ poi -> {
|
||||||
|
+ final List<LivingEntity> livingEntities = instance.get(nearestLivingEntities);
|
||||||
|
+
|
||||||
|
+ Villager winner = villager;
|
||||||
|
+ for (final LivingEntity other : livingEntities) {
|
||||||
|
+ if (other == villager) {
|
||||||
|
+ continue;
|
||||||
|
+ }
|
||||||
|
+ if (!(other instanceof final net.minecraft.world.entity.npc.Villager otherVillager)) {
|
||||||
|
+ continue;
|
||||||
|
+ }
|
||||||
|
+ if (!other.isAlive()) {
|
||||||
|
+ continue;
|
||||||
|
+ }
|
||||||
|
+ if (!competesForSameJobsite(globalPos, poi, otherVillager)) {
|
||||||
|
+ continue;
|
||||||
|
+ }
|
||||||
|
+ winner = selectWinner(winner, otherVillager);
|
||||||
|
+ }
|
||||||
|
+ }
|
||||||
|
+ // Paper end - Improve performance of PoiCompetitorScan by unrolling stream
|
||||||
|
);
|
||||||
|
return true;
|
||||||
|
}
|
Loading…
Reference in a new issue