Skip to content

Commit

Permalink
Use a proper key implementation for the search cache (#254)
Browse files Browse the repository at this point in the history
* Use a proper key implementation for the search cache
* Synchronize itemSearchNames accesses to prevent CMEs
* Use a ConcurrentHashMap instead of synchronizing
  This allows multiple threads to read at the same time, which is the more common scenario.
  • Loading branch information
embeddedt authored May 29, 2022
1 parent 2b31129 commit ae21012
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions src/main/java/codechicken/nei/api/ItemInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

/**
* This is an internal class for storing information about items, to be accessed by the API
Expand All @@ -82,8 +83,36 @@ public static enum Layout

public static final HashMap<Item, String> itemOwners = new HashMap<>();

private static class ItemStackKey {
public final ItemStack stack;
public ItemStackKey(ItemStack stack) {
this.stack = stack;
}

@Override
public int hashCode() {
if(this.stack == null)
return 1;
int hashCode = 1;
hashCode = 31 * hashCode + stack.stackSize;
hashCode = 31 * hashCode + Item.getIdFromItem(stack.getItem());
hashCode = 31 * hashCode + stack.getItemDamage();
hashCode = 31 * hashCode + (!stack.hasTagCompound() ? 0 : stack.getTagCompound().hashCode());
return hashCode;
}

@Override
public boolean equals(Object o) {
if(o == this)
return true;
if(!(o instanceof ItemStackKey))
return false;
return ItemStack.areItemStacksEqual(this.stack, ((ItemStackKey)o).stack);
}
}

//lookup optimisation
public static final HashMap<ItemStack, String> itemSearchNames = new HashMap<>();
public static final ConcurrentHashMap<ItemStackKey, String> itemSearchNames = new ConcurrentHashMap<>();

public static boolean isHidden(ItemStack stack) {
return hiddenItems.contains(stack);
Expand Down Expand Up @@ -129,7 +158,18 @@ public static void load(World world) {
}

private static void addSearchOptimisation() {
ItemList.loadCallbacks.add(itemSearchNames::clear);
ItemList.loadCallbacks.add(ItemInfo::populateSearchMap);
}

private static void populateSearchMap() {
/* Create a snapshot of the current keys in the cache */
HashSet<ItemStackKey> oldItems = new HashSet<>(itemSearchNames.keySet());
for(ItemStack stack : ItemList.items) {
/* Populate each entry and remove it from the snapshot */
getSearchName(stack);
oldItems.remove(new ItemStackKey(stack));
}
itemSearchNames.keySet().removeAll(oldItems);
}

private static void addHiddenItemFilter() {
Expand Down Expand Up @@ -517,11 +557,8 @@ public static List<String> getText(ItemStack itemStack, World world, EntityPlaye
}

public static String getSearchName(ItemStack stack) {
String s = itemSearchNames.get(stack);
if(s == null) {
s = EnumChatFormatting.getTextWithoutFormattingCodes(GuiContainerManager.concatenatedDisplayName(stack, true).toLowerCase());
itemSearchNames.put(stack, s);
}
return s;
return itemSearchNames.computeIfAbsent(new ItemStackKey(stack), key ->
EnumChatFormatting.getTextWithoutFormattingCodes(GuiContainerManager.concatenatedDisplayName(key.stack, true).toLowerCase())
);
}
}

0 comments on commit ae21012

Please sign in to comment.