Optimize Brain a little

This commit is contained in:
Nassim Jahnke 2023-09-12 21:19:57 +10:00
parent 517f8fbbac
commit f6a1d2ecd7
No known key found for this signature in database
GPG key ID: EF6771C01F6EF02F
2 changed files with 146 additions and 0 deletions

View file

@ -132,6 +132,65 @@ index 2dc801061025888192c3bf2c4c38b928c16a0165..ca788f0dcec4a117b410fe8348969e05
}
public static <T> SortedArraySet<T> create(Comparator<T> 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<E extends LivingEntity> {
}
public static <E extends LivingEntity> Codec<Brain<E>> codec(final Collection<? extends MemoryModuleType<?>> memoryModules, final Collection<? extends SensorType<? extends Sensor<? super E>>> sensors) {
- final MutableObject<Codec<Brain<E>>> mutableObject = new MutableObject<>();
- mutableObject.setValue((new MapCodec<Brain<E>>() {
+ final MutableObject<Codec<Brain<E>>> mutableObject1 = new MutableObject<>(); // Paper - decompile fix
+ mutableObject1.setValue((new MapCodec<Brain<E>>() { // Paper - decompile fix
public <T> Stream<T> keys(DynamicOps<T> dynamicOps) {
return memoryModules.stream().flatMap((memoryType) -> {
return memoryType.getCodec().map((codec) -> {
@@ -81,7 +81,7 @@ public class Brain<E extends LivingEntity> {
mutableObject.setValue(mutableObject.getValue().apply2(ImmutableList.Builder::add, dataResult2));
});
ImmutableList<Brain.MemoryValue<?>> 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 <T, U> DataResult<Brain.MemoryValue<U>> captureRead(MemoryModuleType<U> memoryType, DynamicOps<T> ops, T value) {
@@ -103,7 +103,7 @@ public class Brain<E extends LivingEntity> {
return recordBuilder;
}
}).fieldOf("memories").codec());
- return mutableObject.getValue();
+ return mutableObject1.getValue(); // Paper - decompile fix
}
public Brain(Collection<? extends MemoryModuleType<?>> memories, Collection<? extends SensorType<? extends Sensor<? super E>>> sensors, ImmutableList<Brain.MemoryValue<?>> memoryEntries, Supplier<Codec<Brain<E>>> codecSupplier) {
@@ -181,14 +181,14 @@ public class Brain<E extends LivingEntity> {
if (optional == null) {
throw new IllegalStateException("Unregistered memory fetched: " + type);
} else {
- return optional.map(ExpirableValue::getValue);
+ return (Optional<U>) optional.map(ExpirableValue::getValue); // Paper - decompile fix
}
}
@Nullable
public <U> Optional<U> getMemoryInternal(MemoryModuleType<U> type) {
Optional<? extends ExpirableValue<?>> optional = this.memories.get(type);
- return optional == null ? null : optional.map(ExpirableValue::getValue);
+ return optional == null ? null : (Optional<U>) optional.map(ExpirableValue::getValue); // Paper - decompile fix
}
public <U> long getTimeUntilExpiry(MemoryModuleType<U> type) {
@@ -483,7 +483,7 @@ public class Brain<E extends LivingEntity> {
private final Optional<? extends ExpirableValue<U>> value;
static <U> Brain.MemoryValue<U> createUnchecked(MemoryModuleType<U> type, Optional<? extends ExpirableValue<?>> data) {
- return new Brain.MemoryValue<>(type, data);
+ return new Brain.MemoryValue<>(type, (Optional<? extends ExpirableValue<U>>) data); // Paper - decompile fix
}
MemoryValue(MemoryModuleType<U> type, Optional<? extends ExpirableValue<U>> 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

View file

@ -0,0 +1,87 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Nassim Jahnke <nassim@njahnke.dev>
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<E extends LivingEntity> {
private final Set<Activity> 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 <E extends LivingEntity> Brain.Provider<E> provider(Collection<? extends MemoryModuleType<?>> memoryModules, Collection<? extends SensorType<? extends Sensor<? super E>>> sensors) {
return new Brain.Provider<>(memoryModules, sensors);
@@ -144,6 +148,7 @@ public class Brain<E extends LivingEntity> {
}
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<E extends LivingEntity> {
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<E extends LivingEntity> {
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<E extends LivingEntity> {
}
public void setActiveActivityToFirstValid(List<Activity> 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<E extends LivingEntity> {
}
}
+ this.dirtyMemories = false; // Paper
}
public void setDefaultActivity(Activity activity) {
@@ -346,6 +355,7 @@ public class Brain<E extends LivingEntity> {
}
public void addActivityAndRemoveMemoriesWhenStopped(Activity activity, ImmutableList<? extends Pair<Integer, ? extends BehaviorControl<? super E>>> indexedTasks, Set<Pair<MemoryModuleType<?>, MemoryStatus>> requiredMemories, Set<MemoryModuleType<?>> 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<E extends LivingEntity> {
}
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<Activity, Set<BehaviorControl<? super E>>> map : this.availableBehaviorsByPriority.values()) {
@@ -524,3 +540,4 @@ public class Brain<E extends LivingEntity> {
}
}
}
+