From ad27520a6e6ffa6c523e8dbb5a3f0a650351016f Mon Sep 17 00:00:00 2001 From: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com> Date: Sat, 26 Oct 2024 02:42:45 +0800 Subject: [PATCH] Optimize Brain --- patches/server/0118-Optimize-Brain.patch | 111 ++++++++++++++++++ .../0119-Remove-stream-in-BehaviorUtils.patch | 35 ++++++ 2 files changed, 146 insertions(+) create mode 100644 patches/server/0118-Optimize-Brain.patch create mode 100644 patches/server/0119-Remove-stream-in-BehaviorUtils.patch diff --git a/patches/server/0118-Optimize-Brain.patch b/patches/server/0118-Optimize-Brain.patch new file mode 100644 index 00000000..2580632a --- /dev/null +++ b/patches/server/0118-Optimize-Brain.patch @@ -0,0 +1,111 @@ +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] Optimize Brain + + +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 afbb027021acfbe25d534a84f1750e420bbde6e0..2e0859cd62fd064d43aaf3ac24269d159a6bd49e 100644 +--- a/src/main/java/net/minecraft/world/entity/ai/Brain.java ++++ b/src/main/java/net/minecraft/world/entity/ai/Brain.java +@@ -45,14 +45,18 @@ public class Brain { + static final Logger LOGGER = LogUtils.getLogger(); + private final Supplier>> codec; + private static final int SCHEDULE_UPDATE_DELAY = 20; +- private final Map, Optional>> memories = Maps.newHashMap(); +- private final Map>, Sensor> sensors = Maps.newLinkedHashMap(); +- private final Map>>> availableBehaviorsByPriority = Maps.newTreeMap(); ++ // Leaf start - Optimize Brain ++ private final Map, Optional>> memories = new it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<>(); ++ private final Map>, Sensor> sensors = new it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap<>(); ++ private final Map>>> availableBehaviorsByPriority = new it.unimi.dsi.fastutil.objects.Object2ObjectRBTreeMap<>(); ++ // Leaf end - Optimize Brain + private Schedule schedule = Schedule.EMPTY; +- private final Map, MemoryStatus>>> activityRequirements = Maps.newHashMap(); +- private final Map>> activityMemoriesToEraseWhenStopped = Maps.newHashMap(); +- private Set coreActivities = Sets.newHashSet(); +- private final Set activeActivities = Sets.newHashSet(); ++ // Leaf start - Optimize Brain ++ private final Map, MemoryStatus>>> activityRequirements = new it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<>(); ++ private final Map>> activityMemoriesToEraseWhenStopped = new it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<>(); ++ private Set coreActivities = new it.unimi.dsi.fastutil.objects.ObjectOpenHashSet<>(); ++ private final Set activeActivities = new it.unimi.dsi.fastutil.objects.ObjectOpenHashSet<>(); ++ // Leaf end - Optimize Brain + private Activity defaultActivity = Activity.IDLE; + private long lastScheduleUpdate = -9999L; + +@@ -69,17 +73,21 @@ public class Brain { + mutableObject.setValue( + (new MapCodec>() { + public Stream keys(DynamicOps dynamicOps) { +- return memoryModules.stream() +- .flatMap( +- memoryType -> memoryType.getCodec() +- .map(codec -> BuiltInRegistries.MEMORY_MODULE_TYPE.getKey((MemoryModuleType)memoryType)) +- .stream() +- ) +- .map(id -> dynamicOps.createString(id.toString())); ++ // Leaf start - Optimize Brain ++ List results = new java.util.ArrayList<>(); ++ for (MemoryModuleType memoryType : memoryModules) { ++ Optional codecOptional = memoryType.getCodec(); ++ if (codecOptional.isPresent()) { ++ net.minecraft.resources.ResourceLocation id = BuiltInRegistries.MEMORY_MODULE_TYPE.getKey(memoryType); ++ results.add(id.toString()); ++ } ++ } ++ return results.stream().map(dynamicOps::createString); ++ // Leaf end - Optimize Brain + } + + public DataResult> decode(DynamicOps dynamicOps, MapLike mapLike) { +- MutableObject>>> mutableObject = new MutableObject<>( ++ MutableObject>>> mutableObject2 = new MutableObject<>( // Leaf - Decompile fix + DataResult.success(ImmutableList.builder()) + ); + mapLike.entries() +@@ -91,10 +99,10 @@ public class Brain { + DataResult> dataResult2 = dataResult.flatMap( + memoryType -> this.captureRead((MemoryModuleType)memoryType, dynamicOps, (T)pair.getSecond()) + ); +- mutableObject.setValue(mutableObject.getValue().apply2(Builder::add, dataResult2)); ++ mutableObject2.setValue(mutableObject2.getValue().apply2(Builder::add, dataResult2)); // Leaf - Decompile fix + } + ); +- ImmutableList> immutableList = mutableObject.getValue() ++ ImmutableList> immutableList = mutableObject2.getValue() // Leaf - Decompile fix + .resultOrPartial(Brain.LOGGER::error) + .map(Builder::build) + .orElseGet(ImmutableList::of); +@@ -152,7 +160,13 @@ public class Brain { + } + + Stream> memories() { +- return this.memories.entrySet().stream().map(entry -> Brain.MemoryValue.createUnchecked(entry.getKey(), entry.getValue())); ++ // Leaf start - Optimize Brain ++ List> result = new java.util.ArrayList<>(); ++ for (Entry, Optional>> entry : this.memories.entrySet()) { ++ result.add(Brain.MemoryValue.createUnchecked(entry.getKey(), entry.getValue())); ++ } ++ return result.stream(); ++ // Leaf end - Optimize Brain + } + + public boolean hasMemoryValue(MemoryModuleType type) { +@@ -194,14 +208,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); // Leaf - 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); // Leaf - Decompile fix + } + + public long getTimeUntilExpiry(MemoryModuleType type) { diff --git a/patches/server/0119-Remove-stream-in-BehaviorUtils.patch b/patches/server/0119-Remove-stream-in-BehaviorUtils.patch new file mode 100644 index 00000000..8dc1e375 --- /dev/null +++ b/patches/server/0119-Remove-stream-in-BehaviorUtils.patch @@ -0,0 +1,35 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com> +Date: Sat, 26 Oct 2024 00:56:24 +0800 +Subject: [PATCH] Remove stream in BehaviorUtils + + +diff --git a/src/main/java/net/minecraft/world/entity/ai/behavior/BehaviorUtils.java b/src/main/java/net/minecraft/world/entity/ai/behavior/BehaviorUtils.java +index 74fe07a3737728b67987ef794103346c1e428109..e2de86d7a86fde183c3ece4de0a1f899a7c3b8b0 100644 +--- a/src/main/java/net/minecraft/world/entity/ai/behavior/BehaviorUtils.java ++++ b/src/main/java/net/minecraft/world/entity/ai/behavior/BehaviorUtils.java +@@ -122,12 +122,20 @@ public class BehaviorUtils { + + public static SectionPos findSectionClosestToVillage(ServerLevel world, SectionPos center, int radius) { + int j = world.sectionsToVillage(center); +- Stream stream = SectionPos.cube(center, radius).filter((sectionposition1) -> { // CraftBukkit - decompile error +- return world.sectionsToVillage(sectionposition1) < j; +- }); ++ // Leaf start - Remove stream usage ++ SectionPos closestSection = center; ++ int closestDistance = j; + + Objects.requireNonNull(world); +- return (SectionPos) stream.min(Comparator.comparingInt(world::sectionsToVillage)).orElse(center); ++ for (SectionPos sectionPosition : SectionPos.cube(center, radius).toList()) { ++ int distance = world.sectionsToVillage(sectionPosition); ++ if (distance < closestDistance) { ++ closestDistance = distance; ++ closestSection = sectionPosition; ++ } ++ } ++ return closestSection; ++ // Leaf end - Remove stream usage + } + + public static boolean isWithinAttackRange(Mob mob, LivingEntity target, int rangedWeaponReachReduction) {