9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2026-01-06 15:51:31 +00:00

remove tick control on getRunningBehaviors

This commit is contained in:
Taiyou06
2025-06-07 23:11:35 +02:00
parent 08d67817b8
commit da48e6e0fb
73 changed files with 4 additions and 97 deletions

View File

@@ -1,72 +0,0 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Taiyou06 <kaandindar21@gmail.com>
Date: Fri, 14 Feb 2025 14:58:59 +0100
Subject: [PATCH] Alternative Brain Behaviour
In the test, this can give ~54.87% improvement (~25712ms -> ~11604ms),
under 1024 villagers situation.
diff --git a/net/minecraft/world/entity/ai/Brain.java b/net/minecraft/world/entity/ai/Brain.java
index e27284f9897923f67985e3d60c3438bd00cc4a51..0ff7564e0e848bd38e82f9089bfd7249fa649dc5 100644
--- a/net/minecraft/world/entity/ai/Brain.java
+++ b/net/minecraft/world/entity/ai/Brain.java
@@ -268,23 +268,52 @@ public class Brain<E extends LivingEntity> {
return this.activeActivities;
}
+ // Leaf start - Alternative Brain Behaviour
+ private ObjectArrayList<BehaviorControl<? super E>> runningBehaviorsCache;
+ private long lastRunningBehaviorCheck = -1;
+ // Leaf end - Alternative Brain Behaviour
+
@Deprecated
@VisibleForDebug
public List<BehaviorControl<? super E>> getRunningBehaviors() {
- List<BehaviorControl<? super E>> list = new ObjectArrayList<>();
+ // Leaf start - Alternative Brain Behaviour
+ long currentTick = getCurrentTick();
+
+ // Use cached result if within update interval
+ if (runningBehaviorsCache != null && (currentTick - lastRunningBehaviorCheck) < org.dreeam.leaf.config.modules.opt.BrainRunningBehaviorCacheUpdate.interval) {
+ return runningBehaviorsCache;
+ }
+
+ // Initialize or reuse cache list
+ if (runningBehaviorsCache == null) {
+ runningBehaviorsCache = new ObjectArrayList<>(32);
+ } else {
+ runningBehaviorsCache.clear();
+ }
+
+ for (Map<Activity, Set<BehaviorControl<? super E>>> activityMap : availableBehaviorsByPriority.values()) {
+ for (Set<BehaviorControl<? super E>> behaviors : activityMap.values()) {
+ if (behaviors.isEmpty()) continue;
- for (Map<Activity, Set<BehaviorControl<? super E>>> map : this.availableBehaviorsByPriority.values()) {
- for (Set<BehaviorControl<? super E>> set : map.values()) {
- for (BehaviorControl<? super E> behaviorControl : set) {
- if (behaviorControl.getStatus() == Behavior.Status.RUNNING) {
- list.add(behaviorControl);
+ for (BehaviorControl<? super E> behavior : behaviors) {
+ if (behavior.getStatus() == Behavior.Status.RUNNING) {
+ runningBehaviorsCache.add(behavior);
}
}
}
}
- return list;
+ lastRunningBehaviorCheck = currentTick;
+
+ return runningBehaviorsCache;
+ }
+
+ // Helper method to get current tick
+ private long getCurrentTick() {
+ // This should be implemented to return the current game tick
+ return System.nanoTime() / 50_000_000; // Approximate tick time of 50ms
}
+ // Leaf end - Alternative Brain Behaviour
public void useDefaultActivity() {
this.setActiveActivity(this.defaultActivity);

View File

@@ -5,7 +5,7 @@ Subject: [PATCH] Cache potential behaviors in Brain
diff --git a/net/minecraft/world/entity/ai/Brain.java b/net/minecraft/world/entity/ai/Brain.java
index 457962b2113726c3cae7fed9c1926e8537834c3c..68eadc20d0bb80cac81dd3b6127c46ea484d39b0 100644
index 97dad57ba873c0f6404a490e358739dbaf11bc55..34b66ee67927bc0796d6c5f069393618abca9d74 100644
--- a/net/minecraft/world/entity/ai/Brain.java
+++ b/net/minecraft/world/entity/ai/Brain.java
@@ -60,6 +60,7 @@ public class Brain<E extends LivingEntity> {
@@ -24,7 +24,7 @@ index 457962b2113726c3cae7fed9c1926e8537834c3c..68eadc20d0bb80cac81dd3b6127c46ea
}
public <T> DataResult<T> serializeStart(DynamicOps<T> ops) {
@@ -343,6 +345,7 @@ public class Brain<E extends LivingEntity> {
@@ -314,6 +316,7 @@ public class Brain<E extends LivingEntity> {
this.activeActivities.clear();
this.activeActivities.addAll(this.coreActivities);
this.activeActivities.add(activity);
@@ -32,7 +32,7 @@ index 457962b2113726c3cae7fed9c1926e8537834c3c..68eadc20d0bb80cac81dd3b6127c46ea
}
}
@@ -423,11 +426,13 @@ public class Brain<E extends LivingEntity> {
@@ -394,11 +397,13 @@ public class Brain<E extends LivingEntity> {
.computeIfAbsent(activity, activity1 -> new it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet<>()) // Leaf - Replace brain activity maps with optimized collection
.add((BehaviorControl<? super E>)pair.getSecond());
}
@@ -46,7 +46,7 @@ index 457962b2113726c3cae7fed9c1926e8537834c3c..68eadc20d0bb80cac81dd3b6127c46ea
}
public boolean isActive(Activity activity) {
@@ -481,30 +486,40 @@ public class Brain<E extends LivingEntity> {
@@ -452,30 +457,40 @@ public class Brain<E extends LivingEntity> {
}
}

View File

@@ -1,21 +0,0 @@
package org.dreeam.leaf.config.modules.opt;
import org.dreeam.leaf.config.ConfigModules;
import org.dreeam.leaf.config.EnumConfigCategory;
public class BrainRunningBehaviorCacheUpdate extends ConfigModules {
public String getBasePath() {
return EnumConfigCategory.PERF.getBaseKeyName();
}
public static int interval = 5;
@Override
public void onLoaded() {
interval = config.getInt(getBasePath() + ".entity-running-behavior-cache-update-interval", interval,
config.pickStringRegionBased(
"How often entity update current brain running behavior list.",
"生物更新现有 Brain Behavior 列表缓存的间隔."));
}
}