9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2026-01-06 15:51:31 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0159-Replace-brain-with-optimized-collection.patch
Dreeam 66887e3367 Update Leaf 1.21.6 (#385)
* Init 1.21.6

* Finish API patches

* Apply purpur patches & all paper server patches

* Apply some

* Apply all

* Updated Upstream (Paper/Purpur)

Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@6aafe29e 1.21.6 dev
PaperMC/Paper@ae7c74ec tweak mob goal keys, generate missing api
PaperMC/Paper@9ea1460b Revert "fix sponge-absorb deleting chest content (#12647)"

Purpur Changes:
PurpurMC/Purpur@eb0ba67d Updated Upstream (Paper)
PurpurMC/Purpur@7c6502dc Updated Upstream (Paper)
PurpurMC/Purpur@aa289e2c Updated Upstream (Paper)
PurpurMC/Purpur@b1d412fb Updated Upstream (Paper)
PurpurMC/Purpur@293e28a0 use empty registryaccess where context is not needed, closes #1676
PurpurMC/Purpur@452bb319 port PaperMC/Paper#12654, closes #1665
PurpurMC/Purpur@849bc79c register test subcommands used for debugging, closes #1675

* Fix build & diff

* Update vanilla attribute size

* [ci/skip] Forget to remove this

* Updated Upstream (Paper/Purpur)

Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@a24f9b20 1.21.6 dev
PaperMC/Paper@457a0a69 Bump version string and protocol version to 1.21.6 release
PaperMC/Paper@245b5473 Update base to 1.21.6
PaperMC/Paper@b28d77b3 Update Moonrise to 1.21.6
PaperMC/Paper@8318dcb7 Update DataConverter to 1.21.6-rc1

Purpur Changes:
PurpurMC/Purpur@b228e316 Updated Upstream (Paper)
PurpurMC/Purpur@d6562a57 make happy ghast takes-damage-from-water and always-drop-exp options work
PurpurMC/Purpur@08d989b6 inline import
PurpurMC/Purpur@61d7f559 Updated Upstream (Paper)
PurpurMC/Purpur@469a4de5 Merge branch 'ver/1.21.5' into ver/1.21.6

* Updated Upstream (Paper)

Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@d2b1158e Set correct world access on BSL snapshots

* Updated Upstream (Paper)

Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@023e6c29 Ensure waypoints are per world
PaperMC/Paper@40d5d31c Fix recursively converting JSON Text Components

* Update spigot mapping to remap reobf mapping to mojamp for plugins

* Updated Upstream (Paper/Purpur)

Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@1fcc4162 Implement FeatureHooks#isChunkSent for Moonrise chunk system
PaperMC/Paper@e4d7178a Implement WaypointTransmitter#isChunkVisible
PaperMC/Paper@8980ead7 Directly walk text component after converting from JSON
PaperMC/Paper@5613ed6d Provide env environment variable and copy spigots sys prop for overriding default repository
PaperMC/Paper@62b7f86d Avoid and discourage use of Maven Central as a CDN (#12689)
PaperMC/Paper@f5534cb7 [ci/skip] Run generators (#12691)
PaperMC/Paper@4781d28b Re-add Log4j Javadocs (#12693)
PaperMC/Paper@74fbcce5 Check type of Material in get/set stats (#12607)
PaperMC/Paper@4b3f967e Improve Fix MC-44654 (#12703)
PaperMC/Paper@a7dd2635 Enable spigot obfuscation support (#12695)
PaperMC/Paper@6a51c44e Cleanup Primed TNT Fix (#12704)
PaperMC/Paper@839c6a18 Fix #11169 (#12706)
PaperMC/Paper@c77d5f99 Fix MC-297591
PaperMC/Paper@219f86ee Implement chunk unload delay config option
PaperMC/Paper@e4eb69b8 Do not allow ticket level decreases to be processed asynchronously
PaperMC/Paper@71b0c768 Adds support for vanilla negative explosions (#12705)
PaperMC/Paper@3750927a [ci/skip] Fix PermissionManager#clearPermissions() docs bad wording (#12657)
PaperMC/Paper@d61a51e8 Add ItemType#getBurnDuration() (#12604)
PaperMC/Paper@29fc8532 Allow empty paths in namespaced keys (#12687)
PaperMC/Paper@4419afb9 fix: Safely handle nanosecond overflow in ClickCallback (#12686)
PaperMC/Paper@56528821 Add `isProxyEnabled` method to ServerConfiguration (#12664)
PaperMC/Paper@c0dda0ea Add `isForceDefaultGameMode` method (#12673)
PaperMC/Paper@e714de63 Fix excess slot updates
PaperMC/Paper@6d0821d2 [ci/skip] Fix docs for Spawner class and cleanup (#12710)
PaperMC/Paper@cceffe3d Release ItemType and BlockType (#12708)
PaperMC/Paper@186e9e33 Relocate CommandMap#registerServerAliases() call to after lifecycle events have been run (#12601)
PaperMC/Paper@5edcf6dd Cleanup/command dispatching (#12713)

Purpur Changes:
PurpurMC/Purpur@baa196f5 Updated Upstream (Paper)
PurpurMC/Purpur@fdd1e980 Updated Upstream (Paper)
PurpurMC/Purpur@439f15db Updated Upstream (Paper)
PurpurMC/Purpur@46a28b93 [ci/skip] update version in README
PurpurMC/Purpur@162bd288 Updated Upstream (Paper)
PurpurMC/Purpur@afcdf9bb Updated Upstream (Paper)

* fix random tick order

Signed-off-by: hayanesuru <hayanesuru@outlook.jp>

* update optimize random tick

* fix mob spawning over limit

* optimize mob spawning

* fix FastBitRadixSort memory leak

* optimize brain collections

* [ci/skip] update comments

* Revert "fix FastBitRadixSort memory leak"

This reverts commit bdea683525.

* optimize brain collections

* fix async mob spawning data race

* [ci/skip] cleanup

* remove hash lookup in optimize random tick

* Updated Upstream (Paper/Purpur)

Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@803baf0b Support hidden entities in Waypoints (#12715)
PaperMC/Paper@1814d8b4 build: publish to fill (#12717)
PaperMC/Paper@e454fef4 Add support for private constructors in plugin main classes (#12652)
PaperMC/Paper@d0e808f4 Move player to correct position in vehicle move packet handler
PaperMC/Paper@46b4b0b8 Improve keepalive ping system
PaperMC/Paper@38c1ddb5 Add and use FeatureHooks.getAllEntities
PaperMC/Paper@2f083acb Ensure player entity schedulers are ticked when they are dead
PaperMC/Paper@7c90c7c4 Optimise EntityScheduler ticking
PaperMC/Paper@aa6ee44a Re-add global region scheduler tick erroneously removed in last commit
PaperMC/Paper@d7510efc Fix #12722 (#12726)
PaperMC/Paper@0caf75f8 Fix #12721 (#12725)
PaperMC/Paper@bee28792 Adventure 4.23.0 (#12690)
PaperMC/Paper@692e93a9 Fix MC-299110
PaperMC/Paper@ea10fa4a Don't mutate the position of Items for MC-4 Fix (#12702)
PaperMC/Paper@aa6cd74c Remove unnecesary item check for ServerboundPlayerActionPacket RELEASE_USE_ITEM (#12668)
PaperMC/Paper@c9e89f49 Expose arrow velocity in EntityShootBowEvent for mobs (#12688)
PaperMC/Paper@7ec3174a Jump out of experimental phase
PaperMC/Paper@4e1a2555 Update try catch for command handling
PaperMC/Paper@e382e687 Fix 0 yield on explosion events
PaperMC/Paper@35b2c6ec Use dropped item for stats info (#12747)
PaperMC/Paper@bd79e20c [ci/skip] PluginManager#getPlugin and PluginManager#isPluginEnabled are case-insensitive (#12723)

Purpur Changes:
PurpurMC/Purpur@4a5974cf Updated Upstream (Paper)
PurpurMC/Purpur@3893bba0 this is important...
PurpurMC/Purpur@47e758fb Updated Upstream (Paper)

* Update PWT patch

* cleanup

* fix playermobcaps command

* fix Didn't fill chunk buffer in async chunk sending

* cleanup

* fix HurtByTargetGoal npe

* optimize collectSpawningChunks (#382)

* optimize collectSpawningChunks

* add bit set

* cleanup

* cleanup

* cleanup

* cleanup

* Updated Upstream (Paper/Purpur)

Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@57c202e0 Allow adventure nbt codec to parse all tags

Purpur Changes:
PurpurMC/Purpur@a4776d7b Updated Upstream (Paper)
PurpurMC/Purpur@dfd8a7ad Fix phantom going at high speed when ridden (#1680)

* Fix diff

---------

Signed-off-by: hayanesuru <hayanesuru@outlook.jp>
Co-authored-by: hayanesuru <hayanesuru@outlook.jp>
2025-06-30 05:17:33 +08:00

247 lines
14 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com>
Date: Sat, 26 Oct 2024 00:06:04 +0800
Subject: [PATCH] Replace brain with optimized collection
Co-authored-by: Taiyou06 <kaandindar21@gmail.com>
diff --git a/net/minecraft/world/entity/ai/Brain.java b/net/minecraft/world/entity/ai/Brain.java
index 29fdf94db0308031edfe7915fc587a2aa5a1a18a..1d533dc2db5df6f31f307bc94f9f8e5784b0ab23 100644
--- a/net/minecraft/world/entity/ai/Brain.java
+++ b/net/minecraft/world/entity/ai/Brain.java
@@ -45,14 +45,20 @@ public class Brain<E extends LivingEntity> {
static final Logger LOGGER = LogUtils.getLogger();
private final Supplier<Codec<Brain<E>>> codec;
private static final int SCHEDULE_UPDATE_DELAY = 20;
- private final Map<MemoryModuleType<?>, Optional<? extends ExpirableValue<?>>> memories = Maps.newHashMap();
- private final Map<SensorType<? extends Sensor<? super E>>, Sensor<? super E>> sensors = Maps.newLinkedHashMap();
- private final Map<Integer, Map<Activity, Set<BehaviorControl<? super E>>>> availableBehaviorsByPriority = Maps.newTreeMap();
+ // Leaf start - Replace brain maps with optimized collection
+ private final Map<MemoryModuleType<?>, Optional<? extends ExpirableValue<?>>> memories = new it.unimi.dsi.fastutil.objects.Reference2ReferenceOpenHashMap<>();
+ private final Map<SensorType<? extends Sensor<? super E>>, Sensor<? super E>> sensors = new it.unimi.dsi.fastutil.objects.Reference2ReferenceArrayMap<>();
+ private final Map<Integer, Map<Activity, Set<BehaviorControl<? super E>>>> availableBehaviorsByPriority = new it.unimi.dsi.fastutil.objects.Object2ObjectRBTreeMap<>();
+ public static final Map[] EMPTY_ARRAY = {};
+ private Map<Activity, Set<BehaviorControl<? super E>>>[] availableBehaviorsByPriorityArray = EMPTY_ARRAY;
+ // Leaf end - Replace brain maps with optimized collection
private Schedule schedule = Schedule.EMPTY;
- private final Map<Activity, Set<Pair<MemoryModuleType<?>, MemoryStatus>>> activityRequirements = Maps.newHashMap();
- private final Map<Activity, Set<MemoryModuleType<?>>> activityMemoriesToEraseWhenStopped = Maps.newHashMap();
- private Set<Activity> coreActivities = Sets.newHashSet();
- private final Set<Activity> activeActivities = Sets.newHashSet();
+ // Leaf start - Replace brain maps with optimized collection
+ private final Map<Activity, Set<Pair<MemoryModuleType<?>, MemoryStatus>>> activityRequirements = new org.dreeam.leaf.util.map.ActivityArrayMap<>();
+ private final Map<Activity, Set<MemoryModuleType<?>>> activityMemoriesToEraseWhenStopped = new org.dreeam.leaf.util.map.ActivityArrayMap<>();
+ private Set<Activity> coreActivities = new org.dreeam.leaf.util.map.ActivityBitSet();
+ private final Set<Activity> activeActivities = new org.dreeam.leaf.util.map.ActivityBitSet();
+ // Leaf end - Replace brain maps with optimized collection
private Activity defaultActivity = Activity.IDLE;
private long lastScheduleUpdate = -9999L;
@@ -162,6 +168,8 @@ public class Brain<E extends LivingEntity> {
for (Brain.MemoryValue<?> memoryValue : memoryValues) {
memoryValue.setMemoryInternal(this);
}
+
+ ((it.unimi.dsi.fastutil.objects.Reference2ReferenceOpenHashMap<MemoryModuleType<?>, Optional<? extends ExpirableValue<?>>>) this.memories).trim(); // Leaf
}
public <T> DataResult<T> serializeStart(DynamicOps<T> ops) {
@@ -178,6 +186,7 @@ public class Brain<E extends LivingEntity> {
public void clearMemories() {
this.memories.keySet().forEach(memoryModuleType -> this.memories.put((MemoryModuleType<?>)memoryModuleType, Optional.empty()));
+ this.availableBehaviorsByPriorityArray = EMPTY_ARRAY; // Leaf
}
public <U> void eraseMemory(MemoryModuleType<U> type) {
@@ -362,7 +371,7 @@ public class Brain<E extends LivingEntity> {
}
public void addActivity(Activity activity, ImmutableList<? extends Pair<Integer, ? extends BehaviorControl<? super E>>> tasks) {
- this.addActivityAndRemoveMemoriesWhenStopped(activity, tasks, ImmutableSet.of(), Sets.newHashSet());
+ this.addActivityAndRemoveMemoriesWhenStopped(activity, tasks, ImmutableSet.of(), new it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet<>());
}
public void addActivityWithConditions(
@@ -395,8 +404,8 @@ public class Brain<E extends LivingEntity> {
for (Pair<Integer, ? extends BehaviorControl<? super E>> pair : tasks) {
this.availableBehaviorsByPriority
- .computeIfAbsent(pair.getFirst(), integer -> Maps.newHashMap())
- .computeIfAbsent(activity, activity1 -> Sets.newLinkedHashSet())
+ .computeIfAbsent(pair.getFirst(), integer -> new org.dreeam.leaf.util.map.ActivityArrayMap<>()) // Leaf - Replace brain activity maps with optimized collection
+ .computeIfAbsent(activity, activity1 -> new org.dreeam.leaf.util.map.BehaviorControlArraySet<>()) // Leaf - Replace brain activity maps with optimized collection
.add((BehaviorControl<? super E>)pair.getSecond());
}
}
@@ -404,6 +413,7 @@ public class Brain<E extends LivingEntity> {
@VisibleForTesting
public void removeAllBehaviors() {
this.availableBehaviorsByPriority.clear();
+ this.availableBehaviorsByPriorityArray = EMPTY_ARRAY; // Leaf
}
public boolean isActive(Activity activity) {
@@ -437,14 +447,28 @@ public class Brain<E extends LivingEntity> {
}
private void forgetOutdatedMemories() {
+ boolean flag = false;
+ for (var entry : this.memories.values()) {
+ if (entry.isPresent()) {
+ ExpirableValue<?> expirableValue = entry.get();
+ if (expirableValue.hasExpired()) {
+ flag = true;
+ }
+ expirableValue.tick();
+ }
+ }
+ if (flag) {
+ eraseOutdatedMemories();
+ }
+ }
+
+ private void eraseOutdatedMemories() {
for (Entry<MemoryModuleType<?>, Optional<? extends ExpirableValue<?>>> entry : this.memories.entrySet()) {
if (entry.getValue().isPresent()) {
- ExpirableValue<?> expirableValue = (ExpirableValue<?>)entry.getValue().get();
+ ExpirableValue<?> expirableValue = entry.getValue().get();
if (expirableValue.hasExpired()) {
this.eraseMemory(entry.getKey());
}
-
- expirableValue.tick();
}
}
}
@@ -452,34 +476,92 @@ public class Brain<E extends LivingEntity> {
public void stopAll(ServerLevel level, E owner) {
long gameTime = owner.level().getGameTime();
- for (BehaviorControl<? super E> behaviorControl : this.getRunningBehaviors()) {
- behaviorControl.doStop(level, owner, gameTime);
+ // Leaf start
+ if (this.availableBehaviorsByPriorityArray.length != this.availableBehaviorsByPriority.size()) {
+ this.availableBehaviorsByPriorityArray = this.availableBehaviorsByPriority.values().toArray(EMPTY_ARRAY);
}
+ for (Map<Activity, Set<BehaviorControl<? super E>>> map : availableBehaviorsByPriorityArray) {
+ var map1 = (org.dreeam.leaf.util.map.ActivityArrayMap<Set<BehaviorControl<? super E>>>) map;
+ for (int index = 0; index <= org.dreeam.leaf.util.map.ActivityArrayMap.BITS; index++) {
+ if ((map1.bitset & (1 << index)) != 0) {
+ var behaviorControls = (org.dreeam.leaf.util.map.BehaviorControlArraySet<? super E>) map1.a[index];
+ var behaviorControlsRaw = behaviorControls.raw();
+ var behaviorControlsSize = behaviorControls.size();
+ for (int i = 0; i < behaviorControlsSize; i++) {
+ BehaviorControl<? super E> behaviorControl = (BehaviorControl<? super E>) behaviorControlsRaw[i];
+ if (behaviorControl.getStatus() == Behavior.Status.RUNNING) {
+ behaviorControl.doStop(level, owner, gameTime);
+ behaviorControls.dec();
+ }
+ }
+ }
+ }
+ }
+ // Leaf end
}
private void startEachNonRunningBehavior(ServerLevel level, E entity) {
long gameTime = level.getGameTime();
- for (Map<Activity, Set<BehaviorControl<? super E>>> map : this.availableBehaviorsByPriority.values()) {
- for (Entry<Activity, Set<BehaviorControl<? super E>>> entry : map.entrySet()) {
- Activity activity = entry.getKey();
- if (this.activeActivities.contains(activity)) {
- for (BehaviorControl<? super E> behaviorControl : entry.getValue()) {
+ // Leaf start
+ if (this.availableBehaviorsByPriorityArray.length != this.availableBehaviorsByPriority.size()) {
+ this.availableBehaviorsByPriorityArray = this.availableBehaviorsByPriority.values().toArray(EMPTY_ARRAY);
+ }
+ var aact = (org.dreeam.leaf.util.map.ActivityBitSet) this.activeActivities;
+ for (int index = 0; index <= org.dreeam.leaf.util.map.ActivityArrayMap.BITS; index++) {
+ if ((aact.bitset & (1 << index)) != 0) {
+ for (Map<Activity, Set<BehaviorControl<? super E>>> map : availableBehaviorsByPriorityArray) {
+ var map1 = ((org.dreeam.leaf.util.map.ActivityArrayMap<Set<BehaviorControl<? super E>>>) map);
+ var ele = map1.a[index];
+ if (ele == null) {
+ continue;
+ }
+ var behaviorControls = (org.dreeam.leaf.util.map.BehaviorControlArraySet<? super E>) ele;
+ var behaviorControlsRaw = behaviorControls.raw();
+ var behaviorControlsSize = behaviorControls.size();
+ for (int i = 0; i < behaviorControlsSize; i++) {
+ BehaviorControl<? super E> behaviorControl = (BehaviorControl<? super E>) behaviorControlsRaw[i];
if (behaviorControl.getStatus() == Behavior.Status.STOPPED) {
- behaviorControl.tryStart(level, entity, gameTime);
+ if (behaviorControl.tryStart(level, entity, gameTime)) {
+ behaviorControls.inc();
+ }
}
}
}
}
}
+ // Leaf end
}
private void tickEachRunningBehavior(ServerLevel level, E entity) {
long gameTime = level.getGameTime();
- for (BehaviorControl<? super E> behaviorControl : this.getRunningBehaviors()) {
- behaviorControl.tickOrStop(level, entity, gameTime);
+ // Leaf start
+ if (this.availableBehaviorsByPriorityArray.length != this.availableBehaviorsByPriority.size()) {
+ this.availableBehaviorsByPriorityArray = this.availableBehaviorsByPriority.values().toArray(new Map[0]);
+ }
+ for (Map<Activity, Set<BehaviorControl<? super E>>> map : availableBehaviorsByPriorityArray) {
+ var map1 = (org.dreeam.leaf.util.map.ActivityArrayMap<Set<BehaviorControl<? super E>>>) map;
+ for (int index = 0; index <= org.dreeam.leaf.util.map.ActivityArrayMap.BITS; index++) {
+ if ((map1.bitset & (1 << index)) != 0) {
+ var behaviorControls = (org.dreeam.leaf.util.map.BehaviorControlArraySet<? super E>) map1.a[index];
+ if (behaviorControls.running()) {
+ var behaviorControlsRaw = behaviorControls.raw();
+ var behaviorControlsSize = behaviorControls.size();
+ for (int i = 0; i < behaviorControlsSize; i++) {
+ BehaviorControl<? super E> behaviorControl = (BehaviorControl<? super E>) behaviorControlsRaw[i];
+ if (behaviorControl.getStatus() == Behavior.Status.RUNNING) {
+ behaviorControl.tickOrStop(level, entity, gameTime);
+ if (behaviorControl.getStatus() == Behavior.Status.STOPPED) {
+ behaviorControls.dec();
+ }
+ }
+ }
+ }
+ }
+ }
}
+ // Leaf end
}
private boolean activityRequirementsAreMet(Activity activity) {
diff --git a/net/minecraft/world/entity/schedule/Activity.java b/net/minecraft/world/entity/schedule/Activity.java
index 5a143bb6fabba3dc4e2272afb0be636d5722ea22..7753b2b2ab00c08dc15444a1caa08faac1d82f0c 100644
--- a/net/minecraft/world/entity/schedule/Activity.java
+++ b/net/minecraft/world/entity/schedule/Activity.java
@@ -32,10 +32,12 @@ public class Activity {
public static final Activity DIG = register("dig");
private final String name;
private final int hashCode;
+ public final int id; // Leaf
- private Activity(String name) {
+ private Activity(String name, int id) { // Leaf
this.name = name;
this.hashCode = name.hashCode();
+ this.id = id; // Leaf
}
public String getName() {
@@ -43,7 +45,7 @@ public class Activity {
}
private static Activity register(String key) {
- return Registry.register(BuiltInRegistries.ACTIVITY, key, new Activity(key));
+ return Registry.register(BuiltInRegistries.ACTIVITY, key, new Activity(key, BuiltInRegistries.ACTIVITY.size())); // Leaf
}
@Override