Skip to content

Commit

Permalink
Small optimizations to startup time
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Sep 27, 2018
1 parent f5acb39 commit d3123ed
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 71 deletions.
3 changes: 3 additions & 0 deletions src/main/java/mezz/jei/gui/ingredients/GuiIngredient.java
Expand Up @@ -129,6 +129,9 @@ private List<T> filterOutHidden(List<T> ingredients) {
if (ingredient == null || ingredientRegistry.isIngredientVisible(ingredient, ingredientFilter)) {
visible.add(ingredient);
}
if (visible.size() > 100) {
return visible;
}
}
if (visible.size() > 0) {
return visible;
Expand Down
Expand Up @@ -72,8 +72,8 @@ private void addItemAndSubItems(StackHelper stackHelper, @Nullable Item item, Li
if (item == null || item == Items.AIR) {
return;
}

NonNullList<ItemStack> items = stackHelper.getSubtypes(item, 1);
NonNullList<ItemStack> items = NonNullList.create();
stackHelper.addSubtypesToList(items, item);
for (ItemStack stack : items) {
addItemStack(stackHelper, stack, itemList, itemNameSet);
}
Expand Down Expand Up @@ -111,20 +111,18 @@ private void addBlockAndSubBlocks(StackHelper stackHelper, @Nullable Block block
}

private void addItemStack(StackHelper stackHelper, ItemStack stack, List<ItemStack> itemList, Set<String> itemNameSet) {
String itemKey = null;
final String itemKey;

try {
addFallbackSubtypeInterpreter(stack);
itemKey = stackHelper.getUniqueIdentifierForStack(stack, StackHelper.UidMode.FULL);
} catch (RuntimeException | LinkageError e) {
String stackInfo = ErrorUtil.getItemStackInfo(stack);
Log.get().error("Couldn't get unique name for itemStack {}", stackInfo, e);
return;
}

if (itemKey != null) {
if (itemNameSet.contains(itemKey)) {
return;
}
if (!itemNameSet.contains(itemKey)) {
itemNameSet.add(itemKey);
itemList.add(stack);
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/mezz/jei/recipes/RecipeMap.java
Expand Up @@ -43,9 +43,7 @@ public <V> List<String> getRecipeCategories(V ingredient) {
return recipeCategoryOrdering.immutableSortedCopy(recipeCategories);
}

public <V> void addRecipeCategory(IRecipeCategory recipeCategory, V ingredient) {
IIngredientHelper<V> ingredientHelper = ingredientRegistry.getIngredientHelper(ingredient);

public <V> void addRecipeCategory(IRecipeCategory recipeCategory, V ingredient, IIngredientHelper<V> ingredientHelper) {
String key = ingredientHelper.getUniqueId(ingredient);
List<String> recipeCategories = categoryUidMap.get(key);
String recipeCategoryUid = recipeCategory.getUid();
Expand Down Expand Up @@ -104,7 +102,7 @@ private <T extends IRecipeWrapper, V> void addRecipe(T recipeWrapper, IRecipeCat

recipeWrappers.add(recipeWrapper);

addRecipeCategory(recipeCategory, ingredient);
addRecipeCategory(recipeCategory, ingredient, ingredientHelper);
}
}
}
16 changes: 6 additions & 10 deletions src/main/java/mezz/jei/recipes/RecipeRegistry.java
Expand Up @@ -13,6 +13,7 @@
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.ingredients.VanillaTypes;
import mezz.jei.api.recipe.IFocus;
import mezz.jei.api.recipe.IIngredientType;
import mezz.jei.api.recipe.IRecipeCategory;
import mezz.jei.api.recipe.IRecipeHandler;
import mezz.jei.api.recipe.IRecipeRegistryPlugin;
Expand Down Expand Up @@ -107,7 +108,9 @@ public RecipeRegistry(
Collection<Object> catalystIngredients = recipeCatalystEntry.getValue();
recipeCatalystsBuilder.putAll(recipeCategory, catalystIngredients);
for (Object catalystIngredient : catalystIngredients) {
recipeInputMap.addRecipeCategory(recipeCategory, catalystIngredient);
IIngredientType ingredientType = ingredientRegistry.getIngredientType(catalystIngredient);
IIngredientHelper ingredientHelper = ingredientRegistry.getIngredientHelper(ingredientType);
recipeInputMap.addRecipeCategory(recipeCategory, catalystIngredient, ingredientHelper);
String catalystIngredientKey = getUniqueId(catalystIngredient);
categoriesForRecipeCatalystKeysBuilder.put(catalystIngredientKey, recipeCategoryUid);
}
Expand Down Expand Up @@ -166,12 +169,12 @@ private void addRecipes(List<Object> unsortedRecipes, ListMultiMap<String, Objec
String recipeCategoryUid = entry.getKey();
for (Object recipe : entry.getValue()) {
progressBar.step("");
addRecipe(recipe, recipeCategoryUid);
addRecipe(recipe, recipe.getClass(), recipeCategoryUid);
}
}
for (Object recipe : unsortedRecipes) {
progressBar.step("");
addRecipe(recipe);
addRecipe(recipe, recipe.getClass(), null);
}
ProgressManager.pop(progressBar);
}
Expand Down Expand Up @@ -210,13 +213,6 @@ public void addRecipe(IRecipeWrapper recipe, String recipeCategoryUid) {
}
}

private void addRecipe(Object recipe, String recipeCategoryUid) {
ErrorUtil.checkNotNull(recipe, "recipe");
ErrorUtil.checkNotNull(recipeCategoryUid, "recipeCategoryUid");

addRecipe(recipe, recipe.getClass(), recipeCategoryUid);
}

private <T> void addRecipe(T recipe, Class<? extends T> recipeClass, @Nullable String recipeCategoryUid) {
if (recipeCategoryUid == null) {
IRecipeHandler<T> recipeHandler = getRecipeHandler(recipeClass, null);
Expand Down
123 changes: 73 additions & 50 deletions src/main/java/mezz/jei/startup/StackHelper.java
Expand Up @@ -2,7 +2,6 @@

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
Expand Down Expand Up @@ -206,55 +205,65 @@ public boolean isEquivalent(@Nullable ItemStack lhs, @Nullable ItemStack rhs) {

@Override
public List<ItemStack> getSubtypes(@Nullable ItemStack itemStack) {
if (itemStack == null) {
return Collections.emptyList();
}

if (itemStack.isEmpty()) {
if (itemStack == null || itemStack.isEmpty()) {
return Collections.emptyList();
}

if (itemStack.getItemDamage() != OreDictionary.WILDCARD_VALUE) {
return Collections.singletonList(itemStack);
}

return getSubtypes(itemStack.getItem(), itemStack.getCount());
NonNullList<ItemStack> subtypes = NonNullList.create();
addSubtypesToList(subtypes, itemStack);
return subtypes;
}

public NonNullList<ItemStack> getSubtypes(final Item item, final int stackSize) {
NonNullList<ItemStack> itemStacks = NonNullList.create();
private void addSubtypesToList(final List<ItemStack> subtypeList, ItemStack itemStack) {
final Item item = itemStack.getItem();
final int stackSize = itemStack.getCount();
for (CreativeTabs itemTab : item.getCreativeTabs()) {
if (itemTab == null) {
subtypeList.add(itemStack);
} else {
addSubtypesFromCreativeTabToList(subtypeList, item, stackSize, itemTab);
}
}
}

public void addSubtypesToList(final List<ItemStack> subtypeList, Item item) {
for (CreativeTabs itemTab : item.getCreativeTabs()) {
if (itemTab == null) {
itemStacks.add(new ItemStack(item, stackSize));
subtypeList.add(new ItemStack(item, 1));
} else {
NonNullList<ItemStack> subItems = NonNullList.create();
try {
item.getSubItems(itemTab, subItems);
} catch (RuntimeException | LinkageError e) {
Log.get().warn("Caught a crash while getting sub-items of {}", item, e);
}
addSubtypesFromCreativeTabToList(subtypeList, item, 1, itemTab);
}
}
}

for (ItemStack subItem : subItems) {
if (subItem.isEmpty()) {
Log.get().warn("Found an empty subItem of {}", item);
} else if (subItem.getMetadata() == OreDictionary.WILDCARD_VALUE) {
String itemStackInfo = ErrorUtil.getItemStackInfo(subItem);
Log.get().error("Found an subItem of {} with wildcard metadata: {}", item, itemStackInfo);
} else {
if (subItem.getCount() != stackSize) {
ItemStack subItemCopy = subItem.copy();
subItemCopy.setCount(stackSize);
itemStacks.add(subItemCopy);
} else {
itemStacks.add(subItem);
}
}
private void addSubtypesFromCreativeTabToList(List<ItemStack> subtypeList, Item item, final int stackSize, CreativeTabs itemTab) {
NonNullList<ItemStack> subItems = NonNullList.create();
try {
item.getSubItems(itemTab, subItems);
} catch (RuntimeException | LinkageError e) {
Log.get().warn("Caught a crash while getting sub-items of {}", item, e);
}

for (ItemStack subItem : subItems) {
if (subItem.isEmpty()) {
Log.get().warn("Found an empty subItem of {}", item);
} else if (subItem.getMetadata() == OreDictionary.WILDCARD_VALUE) {
String itemStackInfo = ErrorUtil.getItemStackInfo(subItem);
Log.get().error("Found an subItem of {} with wildcard metadata: {}", item, itemStackInfo);
} else {
if (subItem.getCount() != stackSize) {
ItemStack subItemCopy = subItem.copy();
subItemCopy.setCount(stackSize);
subtypeList.add(subItemCopy);
} else {
subtypeList.add(subItem);
}
}
}

return itemStacks;
}

@Override
Expand All @@ -264,7 +273,7 @@ public List<ItemStack> getAllSubtypes(@Nullable Iterable stacks) {
}

List<ItemStack> allSubtypes = new ArrayList<>();
getAllSubtypes(allSubtypes, stacks);
addSubtypesToList(allSubtypes, stacks);

if (isAllNulls(allSubtypes)) {
return Collections.emptyList();
Expand All @@ -282,14 +291,19 @@ private static boolean isAllNulls(Iterable<?> iterable) {
return true;
}

private void getAllSubtypes(List<ItemStack> subtypesList, Iterable stacks) {
private void addSubtypesToList(List<ItemStack> subtypesList, Iterable stacks) {
for (Object obj : stacks) {
if (obj instanceof ItemStack) {
ItemStack itemStack = (ItemStack) obj;
List<ItemStack> subtypes = getSubtypes(itemStack);
subtypesList.addAll(subtypes);
if (!itemStack.isEmpty()) {
if (itemStack.getItemDamage() == OreDictionary.WILDCARD_VALUE) {
addSubtypesToList(subtypesList, itemStack);
} else {
subtypesList.add(itemStack);
}
}
} else if (obj instanceof Iterable) {
getAllSubtypes(subtypesList, (Iterable) obj);
addSubtypesToList(subtypesList, (Iterable) obj);
} else if (obj != null) {
Log.get().error("Unknown object found: {}", obj);
} else {
Expand Down Expand Up @@ -333,21 +347,17 @@ public NonNullList<ItemStack> toItemStackList(Object stacks, boolean expandSubty

private void toItemStackList(UniqueItemStackListBuilder itemStackListBuilder, @Nullable Object input, boolean expandSubtypes) {
if (input instanceof ItemStack) {
ItemStack stack = (ItemStack) input;
if (expandSubtypes && stack.getMetadata() == OreDictionary.WILDCARD_VALUE) {
List<ItemStack> subtypes = getSubtypes(stack);
for (ItemStack subtype : subtypes) {
itemStackListBuilder.add(subtype);
}
} else {
itemStackListBuilder.add(stack);
}
toItemStackList(itemStackListBuilder, (ItemStack) input, expandSubtypes);
} else if (input instanceof String) {
List<ItemStack> stacks = OreDictionary.getOres((String) input);
toItemStackList(itemStackListBuilder, stacks, expandSubtypes);
for (ItemStack stack : stacks) {
toItemStackList(itemStackListBuilder, stack, expandSubtypes);
}
} else if (input instanceof Ingredient) {
List<ItemStack> stacks = Arrays.asList(((Ingredient) input).getMatchingStacks());
toItemStackList(itemStackListBuilder, stacks, expandSubtypes);
ItemStack[] stacks = ((Ingredient) input).getMatchingStacks();
for (ItemStack stack : stacks) {
toItemStackList(itemStackListBuilder, stack, expandSubtypes);
}
} else if (input instanceof Iterable) {
for (Object obj : (Iterable) input) {
toItemStackList(itemStackListBuilder, obj, expandSubtypes);
Expand All @@ -357,6 +367,19 @@ private void toItemStackList(UniqueItemStackListBuilder itemStackListBuilder, @N
}
}

private void toItemStackList(UniqueItemStackListBuilder itemStackListBuilder, @Nullable ItemStack itemStack, boolean expandSubtypes) {
if (itemStack != null) {
if (expandSubtypes && itemStack.getMetadata() == OreDictionary.WILDCARD_VALUE) {
List<ItemStack> subtypes = getSubtypes(itemStack);
for (ItemStack subtype : subtypes) {
itemStackListBuilder.add(subtype);
}
} else {
itemStackListBuilder.add(itemStack);
}
}
}

public String getUniqueIdentifierForStack(ItemStack stack) {
return getUniqueIdentifierForStack(stack, UidMode.NORMAL);
}
Expand Down

0 comments on commit d3123ed

Please sign in to comment.