From f6a1d2ecd7978131a26350495387565ea900a457 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Tue, 12 Sep 2023 21:19:57 +1000 Subject: Optimize Brain a little --- patches/server/0006-MC-Dev-fixes.patch | 59 ++++++++++++++++ patches/server/1032-Optimize-brain-ticking.patch | 87 ++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 patches/server/1032-Optimize-brain-ticking.patch diff --git a/patches/server/0006-MC-Dev-fixes.patch b/patches/server/0006-MC-Dev-fixes.patch index becf3158af..3395dbfa27 100644 --- a/patches/server/0006-MC-Dev-fixes.patch +++ b/patches/server/0006-MC-Dev-fixes.patch @@ -132,6 +132,65 @@ index 2dc801061025888192c3bf2c4c38b928c16a0165..ca788f0dcec4a117b410fe8348969e05 } public static SortedArraySet create(Comparator comparator) { +diff --git a/src/main/java/net/minecraft/world/entity/ai/Brain.java b/src/main/java/net/minecraft/world/entity/ai/Brain.java +index ef6e968ed2708272eab407a983928382a2f2049c..f34b3548990d4398123352c439b598d47a904a70 100644 +--- a/src/main/java/net/minecraft/world/entity/ai/Brain.java ++++ b/src/main/java/net/minecraft/world/entity/ai/Brain.java +@@ -59,8 +59,8 @@ public class Brain { + } + + public static Codec> codec(final Collection> memoryModules, final Collection>> sensors) { +- final MutableObject>> mutableObject = new MutableObject<>(); +- mutableObject.setValue((new MapCodec>() { ++ final MutableObject>> mutableObject1 = new MutableObject<>(); // Paper - decompile fix ++ mutableObject1.setValue((new MapCodec>() { // Paper - decompile fix + public Stream keys(DynamicOps dynamicOps) { + return memoryModules.stream().flatMap((memoryType) -> { + return memoryType.getCodec().map((codec) -> { +@@ -81,7 +81,7 @@ public class Brain { + mutableObject.setValue(mutableObject.getValue().apply2(ImmutableList.Builder::add, dataResult2)); + }); + ImmutableList> immutableList = mutableObject.getValue().resultOrPartial(Brain.LOGGER::error).map(ImmutableList.Builder::build).orElseGet(ImmutableList::of); +- return DataResult.success(new Brain<>(memoryModules, sensors, immutableList, mutableObject::getValue)); ++ return DataResult.success(new Brain<>(memoryModules, sensors, immutableList, mutableObject1::getValue)); // Paper - decompile fix + } + + private DataResult> captureRead(MemoryModuleType memoryType, DynamicOps ops, T value) { +@@ -103,7 +103,7 @@ public class Brain { + return recordBuilder; + } + }).fieldOf("memories").codec()); +- return mutableObject.getValue(); ++ return mutableObject1.getValue(); // Paper - decompile fix + } + + public Brain(Collection> memories, Collection>> sensors, ImmutableList> memoryEntries, Supplier>> codecSupplier) { +@@ -181,14 +181,14 @@ public class Brain { + if (optional == null) { + throw new IllegalStateException("Unregistered memory fetched: " + type); + } else { +- return optional.map(ExpirableValue::getValue); ++ return (Optional) optional.map(ExpirableValue::getValue); // Paper - decompile fix + } + } + + @Nullable + public Optional getMemoryInternal(MemoryModuleType type) { + Optional> optional = this.memories.get(type); +- return optional == null ? null : optional.map(ExpirableValue::getValue); ++ return optional == null ? null : (Optional) optional.map(ExpirableValue::getValue); // Paper - decompile fix + } + + public long getTimeUntilExpiry(MemoryModuleType type) { +@@ -483,7 +483,7 @@ public class Brain { + private final Optional> value; + + static Brain.MemoryValue createUnchecked(MemoryModuleType type, Optional> data) { +- return new Brain.MemoryValue<>(type, data); ++ return new Brain.MemoryValue<>(type, (Optional>) data); // Paper - decompile fix + } + + MemoryValue(MemoryModuleType type, Optional> data) { diff --git a/src/main/java/net/minecraft/world/entity/animal/camel/Camel.java b/src/main/java/net/minecraft/world/entity/animal/camel/Camel.java index d6e25c7f5dc6c219e2590aa8b1ffd51a2120d50e..40cdff9eaa1e78e02060d970e477d96f960cfed3 100644 --- a/src/main/java/net/minecraft/world/entity/animal/camel/Camel.java diff --git a/patches/server/1032-Optimize-brain-ticking.patch b/patches/server/1032-Optimize-brain-ticking.patch new file mode 100644 index 0000000000..51ef4196fb --- /dev/null +++ b/patches/server/1032-Optimize-brain-ticking.patch @@ -0,0 +1,87 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Nassim Jahnke +Date: Tue, 12 Sep 2023 21:18:04 +1000 +Subject: [PATCH] Optimize brain ticking + + +diff --git a/src/main/java/net/minecraft/world/entity/ai/Brain.java b/src/main/java/net/minecraft/world/entity/ai/Brain.java +index f34b3548990d4398123352c439b598d47a904a70..2005cd4ee368c0638f63297a5a0cfaaf86135633 100644 +--- a/src/main/java/net/minecraft/world/entity/ai/Brain.java ++++ b/src/main/java/net/minecraft/world/entity/ai/Brain.java +@@ -53,6 +53,10 @@ public class Brain { + private final Set activeActivities = Sets.newHashSet(); + private Activity defaultActivity = Activity.IDLE; + private long lastScheduleUpdate = -9999L; ++ // Paper start ++ private boolean dirtyMemories = true; ++ private boolean dirtyActivities = true; ++ // Paper end + + public static Brain.Provider provider(Collection> memoryModules, Collection>> sensors) { + return new Brain.Provider<>(memoryModules, sensors); +@@ -144,6 +148,7 @@ public class Brain { + } + + public void clearMemories() { ++ this.dirtyMemories = true; // Paper + this.memories.keySet().forEach((type) -> { + this.memories.put(type, Optional.empty()); + }); +@@ -170,6 +175,7 @@ public class Brain { + if (memory.isPresent() && this.isEmptyCollection(memory.get().getValue())) { + this.eraseMemory(type); + } else { ++ this.dirtyMemories = true; // Paper + this.memories.put(type, memory); + } + } +@@ -281,6 +287,7 @@ public class Brain { + + private void setActiveActivity(Activity except) { + if (!this.isActive(except)) { ++ this.dirtyActivities = true; + this.eraseMemoriesForOtherActivitesThan(except); + this.activeActivities.clear(); + this.activeActivities.addAll(this.coreActivities); +@@ -314,6 +321,7 @@ public class Brain { + } + + public void setActiveActivityToFirstValid(List activities) { ++ if (!this.dirtyMemories) return; // Paper - this assumes that the list is always going to be the same + for(Activity activity : activities) { + if (this.activityRequirementsAreMet(activity)) { + this.setActiveActivity(activity); +@@ -321,6 +329,7 @@ public class Brain { + } + } + ++ this.dirtyMemories = false; // Paper + } + + public void setDefaultActivity(Activity activity) { +@@ -346,6 +355,7 @@ public class Brain { + } + + public void addActivityAndRemoveMemoriesWhenStopped(Activity activity, ImmutableList>> indexedTasks, Set, MemoryStatus>> requiredMemories, Set> forgettingMemories) { ++ this.dirtyMemories = true; // Paper + this.activityRequirements.put(activity, requiredMemories); + if (!forgettingMemories.isEmpty()) { + this.activityMemoriesToEraseWhenStopped.put(activity, forgettingMemories); +@@ -421,6 +431,12 @@ public class Brain { + } + + private void startEachNonRunningBehavior(ServerLevel world, E entity) { ++ // Paper start - Dynamic brain activation ++ if (!this.dirtyActivities) { ++ return; ++ } ++ this.dirtyActivities = false; ++ // Paper end - Dynamic brain activation + long l = world.getGameTime(); + + for(Map>> map : this.availableBehaviorsByPriority.values()) { +@@ -524,3 +540,4 @@ public class Brain { + } + } + } ++ -- cgit v1.2.3