2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2022-09-26 10:02:51 +02:00
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
2021-06-11 14:02:28 +02:00
Date: Mon, 6 Apr 2020 17:53:29 -0700
2021-11-24 12:06:34 +01:00
Subject: [PATCH] Optimize GoalSelector Goal.Flag Set operations
2021-06-11 14:02:28 +02:00
2021-11-24 12:06:34 +01:00
Optimise the stream.anyMatch statement to move to a bitset
2021-06-11 14:02:28 +02:00
where we can replace the call with a single bitwise operation.
diff --git a/src/main/java/net/minecraft/world/entity/ai/goal/Goal.java b/src/main/java/net/minecraft/world/entity/ai/goal/Goal.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/world/entity/ai/goal/Goal.java
+++ b/src/main/java/net/minecraft/world/entity/ai/goal/Goal.java
2021-11-24 12:06:34 +01:00
@@ -0,0 +0,0 @@ import java.util.EnumSet;
import net.minecraft.util.Mth;
2021-06-11 14:02:28 +02:00
public abstract class Goal {
- private final EnumSet<Goal.Flag> flags = EnumSet.noneOf(Goal.Flag.class);
+ private final EnumSet<Goal.Flag> flags = EnumSet.noneOf(Goal.Flag.class); // Paper unused, but dummy to prevent plugins from crashing as hard. Theyll need to support paper in a special case if this is super important, but really doesn't seem like it would be.
2024-07-17 19:24:53 +02:00
+ private final ca.spottedleaf.moonrise.common.set.OptimizedSmallEnumSet<net.minecraft.world.entity.ai.goal.Goal.Flag> goalTypes = new ca.spottedleaf.moonrise.common.set.OptimizedSmallEnumSet<>(Goal.Flag.class); // Paper - remove streams from pathfindergoalselector
2024-01-23 12:06:27 +01:00
+
+ // Paper start - remove streams from pathfindergoalselector; make sure types are not empty
+ public Goal() {
+ if (this.goalTypes.size() == 0) {
2024-07-17 19:24:53 +02:00
+ this.goalTypes.addUnchecked(Flag.UNKNOWN_BEHAVIOR);
2024-01-23 12:06:27 +01:00
+ }
+ }
+ // Paper end - remove streams from pathfindergoalselector
2021-06-11 14:02:28 +02:00
2021-06-14 03:06:38 +02:00
public abstract boolean canUse();
2021-06-11 14:02:28 +02:00
@@ -0,0 +0,0 @@ public abstract class Goal {
2021-06-14 03:06:38 +02:00
}
2021-06-11 14:02:28 +02:00
public void setFlags(EnumSet<Goal.Flag> controls) {
- this.flags.clear();
- this.flags.addAll(controls);
+ // Paper start - remove streams from pathfindergoalselector
+ this.goalTypes.clear();
2024-07-17 19:24:53 +02:00
+ this.goalTypes.addAllUnchecked(controls);
2024-01-23 12:06:27 +01:00
+ if (this.goalTypes.size() == 0) {
2024-07-17 19:24:53 +02:00
+ this.goalTypes.addUnchecked(Flag.UNKNOWN_BEHAVIOR);
2024-01-23 12:06:27 +01:00
+ }
2021-06-11 14:02:28 +02:00
+ // Paper end - remove streams from pathfindergoalselector
}
2021-06-14 03:06:38 +02:00
@Override
@@ -0,0 +0,0 @@ public abstract class Goal {
2021-06-11 14:02:28 +02:00
return this.getClass().getSimpleName();
}
- public EnumSet<Goal.Flag> getFlags() {
- return this.flags;
+ // Paper start - remove streams from pathfindergoalselector
2024-07-17 19:24:53 +02:00
+ public ca.spottedleaf.moonrise.common.set.OptimizedSmallEnumSet<Goal.Flag> getFlags() {
2021-06-11 14:02:28 +02:00
+ return this.goalTypes;
+ // Paper end - remove streams from pathfindergoalselector
}
2021-11-24 12:06:34 +01:00
protected int adjustedTickDelay(int ticks) {
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/world/entity/ai/goal/GoalSelector.java b/src/main/java/net/minecraft/world/entity/ai/goal/GoalSelector.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/world/entity/ai/goal/GoalSelector.java
+++ b/src/main/java/net/minecraft/world/entity/ai/goal/GoalSelector.java
@@ -0,0 +0,0 @@ public class GoalSelector {
2021-06-14 03:06:38 +02:00
private final Map<Goal.Flag, WrappedGoal> lockedFlags = new EnumMap<>(Goal.Flag.class);
2024-04-25 11:42:10 +02:00
private final Set<WrappedGoal> availableGoals = new ObjectLinkedOpenHashSet<>();
2021-06-11 14:02:28 +02:00
private final Supplier<ProfilerFiller> profiler;
- private final EnumSet<Goal.Flag> disabledFlags = EnumSet.noneOf(Goal.Flag.class);
2024-04-25 11:42:10 +02:00
+ private static final Goal.Flag[] GOAL_FLAG_VALUES = Goal.Flag.values(); // Paper - remove streams from pathfindergoalselector
2024-07-17 19:24:53 +02:00
+ private final ca.spottedleaf.moonrise.common.set.OptimizedSmallEnumSet<net.minecraft.world.entity.ai.goal.Goal.Flag> goalTypes = new ca.spottedleaf.moonrise.common.set.OptimizedSmallEnumSet<>(Goal.Flag.class); // Paper - remove streams from pathfindergoalselector
2021-06-14 03:06:38 +02:00
private int curRate;
2021-06-11 14:02:28 +02:00
2021-06-14 03:06:38 +02:00
public GoalSelector(Supplier<ProfilerFiller> profiler) {
2021-06-11 14:02:28 +02:00
@@ -0,0 +0,0 @@ public class GoalSelector {
2024-04-25 11:42:10 +02:00
this.availableGoals.removeIf(wrappedGoalx -> wrappedGoalx.getGoal() == goal);
2021-06-14 03:06:38 +02:00
}
2024-04-25 11:42:10 +02:00
2023-03-23 22:57:03 +01:00
- private static boolean goalContainsAnyFlags(WrappedGoal goal, EnumSet<Goal.Flag> controls) {
2024-04-12 21:14:06 +02:00
- for (Goal.Flag flag : goal.getFlags()) {
2023-03-23 22:57:03 +01:00
- if (controls.contains(flag)) {
- return true;
2024-04-25 11:42:10 +02:00
- }
- }
-
2021-11-24 12:06:34 +01:00
- return false;
2024-04-25 11:42:10 +02:00
+ // Paper start
2024-07-17 19:24:53 +02:00
+ private static boolean goalContainsAnyFlags(WrappedGoal goal, ca.spottedleaf.moonrise.common.set.OptimizedSmallEnumSet<Goal.Flag> controls) {
2024-04-25 11:42:10 +02:00
+ return goal.getFlags().hasCommonElements(controls);
2021-11-24 12:06:34 +01:00
}
private static boolean goalCanBeReplacedForAllFlags(WrappedGoal goal, Map<Goal.Flag, WrappedGoal> goalsByControl) {
2024-04-12 21:14:06 +02:00
- for (Goal.Flag flag : goal.getFlags()) {
2021-11-30 09:36:30 +01:00
+ long flagIterator = goal.getFlags().getBackingSet();
+ int wrappedGoalSize = goal.getFlags().size();
+ for (int i = 0; i < wrappedGoalSize; ++i) {
+ final Goal.Flag flag = GOAL_FLAG_VALUES[Long.numberOfTrailingZeros(flagIterator)];
2024-07-17 19:24:53 +02:00
+ flagIterator ^= ca.spottedleaf.concurrentutil.util.IntegerUtil.getTrailingBit(flagIterator);
2021-11-30 09:36:30 +01:00
+ // Paper end
2021-11-24 12:06:34 +01:00
if (!goalsByControl.getOrDefault(flag, NO_GOAL).canBeReplacedBy(goal)) {
2021-11-30 09:36:30 +01:00
return false;
2021-11-24 12:06:34 +01:00
}
@@ -0,0 +0,0 @@ public class GoalSelector {
2021-06-14 03:06:38 +02:00
profilerFiller.push("goalCleanup");
2024-04-12 21:14:06 +02:00
for (WrappedGoal wrappedGoal : this.availableGoals) {
2021-11-24 12:06:34 +01:00
- if (wrappedGoal.isRunning() && (goalContainsAnyFlags(wrappedGoal, this.disabledFlags) || !wrappedGoal.canContinueToUse())) {
2024-04-12 21:14:06 +02:00
+ if (wrappedGoal.isRunning() && (goalContainsAnyFlags(wrappedGoal, this.goalTypes) || !wrappedGoal.canContinueToUse())) { // Paper - Perf: optimize goal types by removing streams
2021-11-24 12:06:34 +01:00
wrappedGoal.stop();
}
}
@@ -0,0 +0,0 @@ public class GoalSelector {
2021-06-14 03:06:38 +02:00
profilerFiller.push("goalUpdate");
2021-06-11 14:02:28 +02:00
2024-04-12 21:14:06 +02:00
for (WrappedGoal wrappedGoal2 : this.availableGoals) {
- if (!wrappedGoal2.isRunning()
- && !goalContainsAnyFlags(wrappedGoal2, this.disabledFlags)
- && goalCanBeReplacedForAllFlags(wrappedGoal2, this.lockedFlags)
- && wrappedGoal2.canUse()) {
- for (Goal.Flag flag : wrappedGoal2.getFlags()) {
2021-11-24 12:06:34 +01:00
+ // Paper start
+ if (!wrappedGoal2.isRunning() && !goalContainsAnyFlags(wrappedGoal2, this.goalTypes) && goalCanBeReplacedForAllFlags(wrappedGoal2, this.lockedFlags) && wrappedGoal2.canUse()) {
2021-11-30 09:36:30 +01:00
+ long flagIterator = wrappedGoal2.getFlags().getBackingSet();
+ int wrappedGoalSize = wrappedGoal2.getFlags().size();
+ for (int i = 0; i < wrappedGoalSize; ++i) {
+ final Goal.Flag flag = GOAL_FLAG_VALUES[Long.numberOfTrailingZeros(flagIterator)];
2024-07-17 19:24:53 +02:00
+ flagIterator ^= ca.spottedleaf.concurrentutil.util.IntegerUtil.getTrailingBit(flagIterator);
2021-11-24 12:06:34 +01:00
+ // Paper end
WrappedGoal wrappedGoal3 = this.lockedFlags.getOrDefault(flag, NO_GOAL);
wrappedGoal3.stop();
this.lockedFlags.put(flag, wrappedGoal2);
2021-06-11 14:02:28 +02:00
@@ -0,0 +0,0 @@ public class GoalSelector {
}
public void disableControlFlag(Goal.Flag control) {
- this.disabledFlags.add(control);
2024-07-17 19:24:53 +02:00
+ this.goalTypes.addUnchecked(control); // Paper - remove streams from pathfindergoalselector
2021-06-11 14:02:28 +02:00
}
public void enableControlFlag(Goal.Flag control) {
- this.disabledFlags.remove(control);
2024-07-17 19:24:53 +02:00
+ this.goalTypes.removeUnchecked(control); // Paper - remove streams from pathfindergoalselector
2021-06-11 14:02:28 +02:00
}
public void setControlFlag(Goal.Flag control, boolean enabled) {
diff --git a/src/main/java/net/minecraft/world/entity/ai/goal/WrappedGoal.java b/src/main/java/net/minecraft/world/entity/ai/goal/WrappedGoal.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/world/entity/ai/goal/WrappedGoal.java
+++ b/src/main/java/net/minecraft/world/entity/ai/goal/WrappedGoal.java
@@ -0,0 +0,0 @@ public class WrappedGoal extends Goal {
}
2021-11-24 12:06:34 +01:00
@Override
2021-06-11 14:02:28 +02:00
- public EnumSet<Goal.Flag> getFlags() {
+ // Paper start - remove streams from pathfindergoalselector
2024-07-17 19:24:53 +02:00
+ public ca.spottedleaf.moonrise.common.set.OptimizedSmallEnumSet<Goal.Flag> getFlags() {
2021-09-09 19:12:22 +02:00
return this.goal.getFlags();
2021-06-11 14:02:28 +02:00
+ // Paper end - remove streams from pathfindergoalselector
}
2021-06-14 03:06:38 +02:00
public boolean isRunning() {