From 754dfd1eb2ddad3b419f9638ded8872647b61f27 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Tue, 30 Nov 2021 00:36:30 -0800 Subject: [PATCH] re-inline Goal.Flag iteration --- patches/server/0006-MC-Utils.patch | 29 ++---------- ...oalSelector-Goal.Flag-Set-operations.patch | 44 ++++++++----------- .../server/0420-Implement-Mob-Goal-API.patch | 15 ------- 3 files changed, 23 insertions(+), 65 deletions(-) diff --git a/patches/server/0006-MC-Utils.patch b/patches/server/0006-MC-Utils.patch index 0a5bbfc2a2..135b52226f 100644 --- a/patches/server/0006-MC-Utils.patch +++ b/patches/server/0006-MC-Utils.patch @@ -1950,15 +1950,13 @@ index 0000000000000000000000000000000000000000..d0c77068e9a53d1b8bbad0f3f6b420d6 +} diff --git a/src/main/java/com/destroystokyo/paper/util/set/OptimizedSmallEnumSet.java b/src/main/java/com/destroystokyo/paper/util/set/OptimizedSmallEnumSet.java new file mode 100644 -index 0000000000000000000000000000000000000000..66f6423d2732d23809fe86418537e35d40d24373 +index 0000000000000000000000000000000000000000..b3329c6fcd6758a781a51f5ba8f5052ac1c77b49 --- /dev/null +++ b/src/main/java/com/destroystokyo/paper/util/set/OptimizedSmallEnumSet.java -@@ -0,0 +1,92 @@ +@@ -0,0 +1,71 @@ +package com.destroystokyo.paper.util.set; + +import java.util.Collection; -+import java.util.function.Consumer; -+import java.util.function.Predicate; + +/** + * @author Spottedleaf @@ -2023,27 +2021,8 @@ index 0000000000000000000000000000000000000000..66f6423d2732d23809fe86418537e35d + return (other.backingSet & this.backingSet) != 0; + } + -+ public void forEach(final E[] values, final Consumer action) { -+ long iterator = this.getBackingSet(); -+ int wrappedGoalSize = this.size(); -+ for (int i = 0; i < wrappedGoalSize; ++i) { -+ final E type = values[Long.numberOfTrailingZeros(iterator)]; -+ iterator ^= io.papermc.paper.util.IntegerUtil.getTrailingBit(iterator); -+ action.accept(type); -+ } -+ } -+ -+ public boolean anyMatch(final E[] values, final Predicate predicate) { -+ long iterator = this.getBackingSet(); -+ int wrappedGoalSize = this.size(); -+ for (int i = 0; i < wrappedGoalSize; ++i) { -+ final E type = values[Long.numberOfTrailingZeros(iterator)]; -+ iterator ^= io.papermc.paper.util.IntegerUtil.getTrailingBit(iterator); -+ if (predicate.test(type)) { -+ return true; -+ } -+ } -+ return false; ++ public boolean hasElement(final E element) { ++ return (this.backingSet & (1L << element.ordinal())) != 0; + } +} diff --git a/src/main/java/io/papermc/paper/chunk/SingleThreadChunkRegionManager.java b/src/main/java/io/papermc/paper/chunk/SingleThreadChunkRegionManager.java diff --git a/patches/server/0394-Optimize-GoalSelector-Goal.Flag-Set-operations.patch b/patches/server/0394-Optimize-GoalSelector-Goal.Flag-Set-operations.patch index 3182a81862..0f741cb320 100644 --- a/patches/server/0394-Optimize-GoalSelector-Goal.Flag-Set-operations.patch +++ b/patches/server/0394-Optimize-GoalSelector-Goal.Flag-Set-operations.patch @@ -47,7 +47,7 @@ index 6667ecc4b7eded4e20a415cef1e1b1179e6710b8..4379b9948f1eecfe6fd7dea98e298ad5 protected int adjustedTickDelay(int ticks) { 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 2bb32378b19a21c94ff3ec8ed32fc9d6f0ad0fdb..ad82395c46943e91e7f65d8d67824cc0c965362b 100644 +index 2bb32378b19a21c94ff3ec8ed32fc9d6f0ad0fdb..7fdc1cbd04a5bba9648272985f51c849b07b8223 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 @@ -30,10 +30,12 @@ public class GoalSelector { @@ -64,7 +64,7 @@ index 2bb32378b19a21c94ff3ec8ed32fc9d6f0ad0fdb..ad82395c46943e91e7f65d8d67824cc0 public GoalSelector(Supplier profiler) { this.profiler = profiler; -@@ -63,32 +65,33 @@ public class GoalSelector { +@@ -63,26 +65,32 @@ public class GoalSelector { } // Paper end public void removeGoal(Goal goal) { @@ -85,10 +85,10 @@ index 2bb32378b19a21c94ff3ec8ed32fc9d6f0ad0fdb..ad82395c46943e91e7f65d8d67824cc0 + WrappedGoal goalWrapped = iterator.next(); + if (goalWrapped.getGoal() != goal) { + continue; -+ } + } + if (goalWrapped.isRunning()) { + goalWrapped.stop(); - } ++ } + iterator.remove(); } + // Paper end - remove streams from pathfindergoalselector @@ -102,21 +102,16 @@ index 2bb32378b19a21c94ff3ec8ed32fc9d6f0ad0fdb..ad82395c46943e91e7f65d8d67824cc0 private static boolean goalCanBeReplacedForAllFlags(WrappedGoal goal, Map goalsByControl) { - for(Goal.Flag flag : goal.getFlags()) { + // Paper start -+ return !goal.getFlags().anyMatch(GOAL_FLAG_VALUES, flag -> { ++ 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)]; ++ flagIterator ^= io.papermc.paper.util.IntegerUtil.getTrailingBit(flagIterator); ++ // Paper end if (!goalsByControl.getOrDefault(flag, NO_GOAL).canBeReplacedBy(goal)) { -- return false; -+ return true; + return false; } -- } -- -- return true; -+ return false; -+ }); -+ // Paper end - } - - public void tick() { -@@ -96,7 +99,7 @@ public class GoalSelector { +@@ -96,7 +104,7 @@ public class GoalSelector { profilerFiller.push("goalCleanup"); for(WrappedGoal wrappedGoal : this.availableGoals) { @@ -125,7 +120,7 @@ index 2bb32378b19a21c94ff3ec8ed32fc9d6f0ad0fdb..ad82395c46943e91e7f65d8d67824cc0 wrappedGoal.stop(); } } -@@ -114,12 +117,14 @@ public class GoalSelector { +@@ -114,8 +122,14 @@ public class GoalSelector { profilerFiller.push("goalUpdate"); for(WrappedGoal wrappedGoal2 : this.availableGoals) { @@ -133,17 +128,16 @@ index 2bb32378b19a21c94ff3ec8ed32fc9d6f0ad0fdb..ad82395c46943e91e7f65d8d67824cc0 - for(Goal.Flag flag : wrappedGoal2.getFlags()) { + // Paper start + if (!wrappedGoal2.isRunning() && !goalContainsAnyFlags(wrappedGoal2, this.goalTypes) && goalCanBeReplacedForAllFlags(wrappedGoal2, this.lockedFlags) && wrappedGoal2.canUse()) { -+ wrappedGoal2.getFlags().forEach(GOAL_FLAG_VALUES, flag -> { ++ 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)]; ++ flagIterator ^= io.papermc.paper.util.IntegerUtil.getTrailingBit(flagIterator); + // Paper end WrappedGoal wrappedGoal3 = this.lockedFlags.getOrDefault(flag, NO_GOAL); wrappedGoal3.stop(); this.lockedFlags.put(flag, wrappedGoal2); -- } -+ }); // Paper - - wrappedGoal2.start(); - } -@@ -155,11 +160,11 @@ public class GoalSelector { +@@ -155,11 +169,11 @@ public class GoalSelector { } public void disableControlFlag(Goal.Flag control) { diff --git a/patches/server/0420-Implement-Mob-Goal-API.patch b/patches/server/0420-Implement-Mob-Goal-API.patch index 2e4f996024..a84ff862f1 100644 --- a/patches/server/0420-Implement-Mob-Goal-API.patch +++ b/patches/server/0420-Implement-Mob-Goal-API.patch @@ -734,21 +734,6 @@ index 0000000000000000000000000000000000000000..0d30e0b21b9024df939a9d070bd4a99b + return types; + } +} -diff --git a/src/main/java/com/destroystokyo/paper/util/set/OptimizedSmallEnumSet.java b/src/main/java/com/destroystokyo/paper/util/set/OptimizedSmallEnumSet.java -index 66f6423d2732d23809fe86418537e35d40d24373..964f68fc3cbdb658c13d5d0213abf2ee3ce9557d 100644 ---- a/src/main/java/com/destroystokyo/paper/util/set/OptimizedSmallEnumSet.java -+++ b/src/main/java/com/destroystokyo/paper/util/set/OptimizedSmallEnumSet.java -@@ -67,6 +67,10 @@ public final class OptimizedSmallEnumSet> { - return (other.backingSet & this.backingSet) != 0; - } - -+ public boolean hasElement(final E element) { -+ return (this.backingSet & (1L << element.ordinal())) != 0; -+ } -+ - public void forEach(final E[] values, final Consumer action) { - long iterator = this.getBackingSet(); - int wrappedGoalSize = this.size(); 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 4379b9948f1eecfe6fd7dea98e298ad5f761019a..3f081183521603824430709886a9cc313c28e7cb 100644 --- a/src/main/java/net/minecraft/world/entity/ai/goal/Goal.java