9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-31 04:46:38 +00:00

Remove list allocation

This commit is contained in:
Dreeam
2025-08-08 01:49:00 +08:00
parent 428d135fb8
commit c19b9cd657

View File

@@ -10,52 +10,48 @@ By: fxmorin <28154542+fxmorin@users.noreply.github.com>
Original license: MIT Original license: MIT
Original project: https://github.com/fxmorin/carpet-fixes Original project: https://github.com/fxmorin/carpet-fixes
Dreeam: Remove stream, better than original version
Optimized the RecipeManager getFirstMatch call to be up to 3x faster Optimized the RecipeManager getFirstMatch call to be up to 3x faster
This is a fully vanilla optimization. Improves: [Blast]Furnace/Campfire/Smoker/Stonecutter/Crafting/Sheep Color Choosing This is a fully vanilla optimization. Improves: [Blast]Furnace/Campfire/Smoker/Stonecutter/Crafting/Sheep Color Choosing
This was mostly made for the auto crafting table, since the performance boost is much more visible while using that mod This was mostly made for the auto crafting table, since the performance boost is much more visible while using that mod
diff --git a/net/minecraft/world/item/crafting/RecipeManager.java b/net/minecraft/world/item/crafting/RecipeManager.java diff --git a/net/minecraft/world/item/crafting/RecipeManager.java b/net/minecraft/world/item/crafting/RecipeManager.java
index 07d7c4737635f671f33b8f73001d67928fc75782..eb7a018ee7c13aac1744fe9c93a4701e6cc441cc 100644 index 07d7c4737635f671f33b8f73001d67928fc75782..c30fafe40e52d1c5c713e48364dce6c9797443a2 100644
--- a/net/minecraft/world/item/crafting/RecipeManager.java --- a/net/minecraft/world/item/crafting/RecipeManager.java
+++ b/net/minecraft/world/item/crafting/RecipeManager.java +++ b/net/minecraft/world/item/crafting/RecipeManager.java
@@ -167,7 +167,7 @@ public class RecipeManager extends SimplePreparableReloadListener<RecipeMap> imp @@ -167,8 +167,20 @@ public class RecipeManager extends SimplePreparableReloadListener<RecipeMap> imp
public <I extends RecipeInput, T extends Recipe<I>> Optional<RecipeHolder<T>> getRecipeFor(RecipeType<T> recipeType, I input, Level level) { public <I extends RecipeInput, T extends Recipe<I>> Optional<RecipeHolder<T>> getRecipeFor(RecipeType<T> recipeType, I input, Level level) {
// CraftBukkit start // CraftBukkit start
- List<RecipeHolder<T>> list = this.recipes.getRecipesFor(recipeType, input, level).toList(); - List<RecipeHolder<T>> list = this.recipes.getRecipesFor(recipeType, input, level).toList();
+ List<RecipeHolder<T>> list = this.recipes.getRecipesForList(recipeType, input, level); // Leaf - Carpet-Fixes - Use optimized RecipeManager - Remove streams to be faster - return (list.isEmpty()) ? Optional.empty() : Optional.of(list.getLast()); // CraftBukkit - SPIGOT-4638: last recipe gets priority
return (list.isEmpty()) ? Optional.empty() : Optional.of(list.getLast()); // CraftBukkit - SPIGOT-4638: last recipe gets priority + // Leaf start - Carpet-Fixes - Use optimized RecipeManager - Remove streams to be faster
+ if (input.isEmpty()) {
+ return Optional.empty();
+ }
+
+ RecipeHolder<T> lastRecipe = null;
+ for (RecipeHolder<T> recipeHolder : this.recipes.byType(recipeType)) {
+ if (recipeHolder.value().matches(input, level)) {
+ lastRecipe = recipeHolder;
+ }
+ }
+
+ return lastRecipe != null ? Optional.of(lastRecipe) : Optional.empty();
+ // Leaf end - Carpet-Fixes - Use optimized RecipeManager - Remove streams to be faster
// CraftBukkit end // CraftBukkit end
} }
diff --git a/net/minecraft/world/item/crafting/RecipeMap.java b/net/minecraft/world/item/crafting/RecipeMap.java diff --git a/net/minecraft/world/item/crafting/RecipeMap.java b/net/minecraft/world/item/crafting/RecipeMap.java
index 098753ddd215b6ef5915fac71d8c4f0b19cf4142..68c2b7e532f0cfa373b7c698da7778c58fe98364 100644 index 098753ddd215b6ef5915fac71d8c4f0b19cf4142..0fb79f5a0284d5ef27a82eb7771b7acd3d36253e 100644
--- a/net/minecraft/world/item/crafting/RecipeMap.java --- a/net/minecraft/world/item/crafting/RecipeMap.java
+++ b/net/minecraft/world/item/crafting/RecipeMap.java +++ b/net/minecraft/world/item/crafting/RecipeMap.java
@@ -73,6 +73,26 @@ public class RecipeMap { @@ -73,6 +73,6 @@ public class RecipeMap {
} }
public <I extends RecipeInput, T extends Recipe<I>> Stream<RecipeHolder<T>> getRecipesFor(RecipeType<T> type, I input, Level level) { public <I extends RecipeInput, T extends Recipe<I>> Stream<RecipeHolder<T>> getRecipesFor(RecipeType<T> type, I input, Level level) {
- return input.isEmpty() ? Stream.empty() : this.byType(type).stream().filter(recipeHolder -> recipeHolder.value().matches(input, level)); - return input.isEmpty() ? Stream.empty() : this.byType(type).stream().filter(recipeHolder -> recipeHolder.value().matches(input, level));
+ return input.isEmpty() ? Stream.empty() : this.byType(type).stream().filter(recipeHolder -> recipeHolder.value().matches(input, level)); // Leaf - Carpet-Fixes - Use optimized RecipeManager - diff on change + return input.isEmpty() ? Stream.empty() : this.byType(type).stream().filter(recipeHolder -> recipeHolder.value().matches(input, level)); // Leaf - Carpet-Fixes - Use optimized RecipeManager - diff on change
} }
+
+ // Leaf start - Carpet-Fixes - Use optimized RecipeManager - Remove streams to be faster
+ public <I extends RecipeInput, T extends Recipe<I>> java.util.List<RecipeHolder<T>> getRecipesForList(RecipeType<T> type, I input, Level level) {
+ java.util.List<RecipeHolder<T>> list;
+
+ if (input.isEmpty()) {
+ return java.util.List.of();
+ } else {
+ list = new java.util.ArrayList<>();
+ }
+
+ for (RecipeHolder<T> recipeHolder : this.byType(type)) {
+ if (recipeHolder.value().matches(input, level)) {
+ list.add(recipeHolder);
+ }
+ }
+
+ return list;
+ }
+ // Leaf end - Carpet-Fixes - Use optimized RecipeManager - Remove streams to be faster
} }