From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Taiyou06 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 e78cef4c0ee6cff7ec2f27ced12c96be67dc7a0e..e3d0186e681168840836690acd402b774bc464b5 100644 --- a/net/minecraft/world/entity/ai/Brain.java +++ b/net/minecraft/world/entity/ai/Brain.java @@ -268,23 +268,52 @@ public class Brain { return this.activeActivities; } + // Leaf start - Alternative Brain Behaviour + private ObjectArrayList> runningBehaviorsCache; + private long lastRunningBehaviorCheck = -1; + // Leaf end - Alternative Brain Behaviour + @Deprecated @VisibleForDebug public List> getRunningBehaviors() { - List> 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>> activityMap : availableBehaviorsByPriority.values()) { + for (Set> behaviors : activityMap.values()) { + if (behaviors.isEmpty()) continue; - for (Map>> map : this.availableBehaviorsByPriority.values()) { - for (Set> set : map.values()) { - for (BehaviorControl behaviorControl : set) { - if (behaviorControl.getStatus() == Behavior.Status.RUNNING) { - list.add(behaviorControl); + for (BehaviorControl 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);