9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-26 18:39:23 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0144-Remove-stream-in-Brain.patch
Dreeam 9a4efaa230 Drop patch that causes performance regression
Originally vanilla logic is to use stream, and Mojang switched it to Guava's Collections2
since 1.21.4. It is much faster than using stream or manually adding to a new ArrayList.
Manually adding to a new ArrayList requires allocating a new object array. However, the Collections2
lazy handles filter condition on iteration, so much better.
2025-08-04 19:25:56 +08:00

66 lines
3.4 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] Remove stream in Brain
diff --git a/net/minecraft/world/entity/ai/Brain.java b/net/minecraft/world/entity/ai/Brain.java
index 98652c2c549bd7657a606d25ba4fe3cffa0548da..29fdf94db0308031edfe7915fc587a2aa5a1a18a 100644
--- a/net/minecraft/world/entity/ai/Brain.java
+++ b/net/minecraft/world/entity/ai/Brain.java
@@ -70,13 +70,22 @@ public class Brain<E extends LivingEntity> {
(new MapCodec<Brain<E>>() {
@Override
public <T> Stream<T> keys(DynamicOps<T> ops) {
- return memoryTypes.stream()
- .flatMap(
- memoryModuleType -> memoryModuleType.getCodec()
- .map(codec -> BuiltInRegistries.MEMORY_MODULE_TYPE.getKey((MemoryModuleType<?>)memoryModuleType))
- .stream()
- )
- .map(resourceLocation -> ops.createString(resourceLocation.toString()));
+ // Leaf start - Remove stream in Brain
+ List<T> results = new java.util.ArrayList<>();
+
+ for (MemoryModuleType<?> memoryModuleType : memoryTypes) {
+ final Optional<?> codec = memoryModuleType.getCodec();
+
+ if (codec.isPresent()) {
+ final net.minecraft.resources.ResourceLocation resourceLocation = BuiltInRegistries.MEMORY_MODULE_TYPE.getKey(memoryModuleType);
+ final T opsResult = ops.createString(resourceLocation.toString());
+
+ results.add(opsResult);
+ }
+ }
+
+ return results.stream();
+ // Leaf end - Remove stream in Brain
}
@Override
@@ -111,7 +120,14 @@ public class Brain<E extends LivingEntity> {
@Override
public <T> RecordBuilder<T> encode(Brain<E> input, DynamicOps<T> ops, RecordBuilder<T> prefix) {
- input.memories().forEach(memoryValue -> memoryValue.serialize(ops, prefix));
+ // Leaf start - Remove stream in Brain
+ for (Entry<MemoryModuleType<?>, Optional<? extends ExpirableValue<?>>> memory : input.memories.entrySet()) {
+ final Brain.MemoryValue<?> result = Brain.MemoryValue.createUnchecked(memory.getKey(), memory.getValue());
+
+ result.serialize(ops, prefix);
+ }
+ // Leaf end - Remove stream in Brain
+
return prefix;
}
})
@@ -153,7 +169,7 @@ public class Brain<E extends LivingEntity> {
}
Stream<Brain.MemoryValue<?>> memories() {
- return this.memories.entrySet().stream().map(memory -> Brain.MemoryValue.createUnchecked(memory.getKey(), memory.getValue()));
+ return this.memories.entrySet().stream().map(memory -> Brain.MemoryValue.createUnchecked(memory.getKey(), memory.getValue())); // Leaf - Remove stream in Brain - diff on change
}
public boolean hasMemoryValue(MemoryModuleType<?> type) {