Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce Memory pressure of UpgradePath #5060

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import crazypants.enderio.api.upgrades.IDarkSteelUpgrade;
import crazypants.enderio.base.EnderIO;
import crazypants.enderio.base.lang.Lang;
import crazypants.enderio.util.StringUtil;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.player.EntityPlayer;
Expand Down Expand Up @@ -188,6 +187,12 @@ public static void addAdvancedTooltipEntries(@Nonnull ItemStack itemstack, Entit
return result.isEmpty() ? "" : NullHelper.first(result.substring(1), "");
}

public static @Nonnull NNList<IDarkSteelUpgrade> getUpgrades(@Nonnull ItemStack stack) {
NNList<IDarkSteelUpgrade> list = UpgradeRegistry.getUpgrades();
list.removeIf(upgrade -> !upgrade.hasUpgrade(stack));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I worry that this O(n) code is going to slow down the JEI handler significantly. Have you profiled the difference?

return list;
}

public static NNList<ItemStack> getRecipes(@Nonnull Set<UpgradePath> list, @Nonnull NNList<ItemStack> input) {
NNList<ItemStack> output = new NNList<ItemStack>();
NNIterator<ItemStack> iterator = input.iterator();
Expand Down Expand Up @@ -222,14 +227,17 @@ public static NNList<ItemStack> getRecipes(@Nonnull Set<UpgradePath> list, @Nonn
public static class UpgradePath {
private final @Nonnull ItemStack input, upgrade, output;
private final @Nonnull IDarkSteelUpgrade dsupgrade;
private final @Nonnull String id;
private final int hash;

UpgradePath(@Nonnull IDarkSteelUpgrade dsupgrade, @Nonnull ItemStack input, @Nonnull ItemStack upgrade, @Nonnull ItemStack output) {
this.input = input;
this.upgrade = upgrade;
this.output = output;
this.dsupgrade = dsupgrade;
this.id = StringUtil.format("%s:%s:%s", input.getItem().getRegistryName(), getUpgradesAsString(input), getUpgradesAsString(output));
int hash = input.getItem().getRegistryName().hashCode();
hash = hash * 31 + getUpgradesAsString(input).hashCode();
hash = hash * 31 + getUpgradesAsString(output).hashCode();
this.hash = hash;
}

public @Nonnull ItemStack getInput() {
Expand All @@ -250,7 +258,7 @@ public static class UpgradePath {

@Override
public int hashCode() {
return id.hashCode();
return hash;
}

@Override
Expand All @@ -265,7 +273,13 @@ public boolean equals(Object obj) {
return false;
}
UpgradePath other = (UpgradePath) obj;
if (!id.equals(other.id)) {
if (input.getItem() != other.getInput().getItem()) {
return false;
}
if (!getUpgrades(input).equals(getUpgrades(other.input))) {
return false;
}
if (!getUpgrades(output).equals(getUpgrades(other.output))) {
return false;
}
return true;
Expand Down