Cache ore dictionary compare checks
This commit is contained in:
		| @@ -1,11 +1,15 @@ | |||||||
| package com.raoulvdberge.refinedstorage.apiimpl.util; | package com.raoulvdberge.refinedstorage.apiimpl.util; | ||||||
|  |  | ||||||
| import com.raoulvdberge.refinedstorage.api.util.IComparer; | import com.raoulvdberge.refinedstorage.api.util.IComparer; | ||||||
|  | import com.raoulvdberge.refinedstorage.apiimpl.API; | ||||||
| import net.minecraft.item.ItemStack; | import net.minecraft.item.ItemStack; | ||||||
| import net.minecraftforge.fluids.FluidStack; | import net.minecraftforge.fluids.FluidStack; | ||||||
| import net.minecraftforge.oredict.OreDictionary; | import net.minecraftforge.oredict.OreDictionary; | ||||||
| import org.apache.commons.lang3.ArrayUtils; | import org.apache.commons.lang3.ArrayUtils; | ||||||
|  |  | ||||||
|  | import java.util.HashMap; | ||||||
|  | import java.util.Map; | ||||||
|  |  | ||||||
| public class Comparer implements IComparer { | public class Comparer implements IComparer { | ||||||
|     @Override |     @Override | ||||||
|     public boolean isEqual(ItemStack left, ItemStack right, int flags) { |     public boolean isEqual(ItemStack left, ItemStack right, int flags) { | ||||||
| @@ -92,6 +96,8 @@ public class Comparer implements IComparer { | |||||||
|         return true; |         return true; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     private Map<Integer, Boolean> oredictCache = new HashMap<>(); | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public boolean isEqualOredict(ItemStack left, ItemStack right) { |     public boolean isEqualOredict(ItemStack left, ItemStack right) { | ||||||
|         if (left == null && right == null) { |         if (left == null && right == null) { | ||||||
| @@ -102,15 +108,26 @@ public class Comparer implements IComparer { | |||||||
|             return false; |             return false; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         int code = API.instance().getItemStackHashCode(left); | ||||||
|  |         code = 31 * code + API.instance().getItemStackHashCode(right); | ||||||
|  |  | ||||||
|  |         if (oredictCache.containsKey(code)) { | ||||||
|  |             return oredictCache.get(code); | ||||||
|  |         } | ||||||
|  |  | ||||||
|         int[] leftIds = OreDictionary.getOreIDs(left); |         int[] leftIds = OreDictionary.getOreIDs(left); | ||||||
|         int[] rightIds = OreDictionary.getOreIDs(right); |         int[] rightIds = OreDictionary.getOreIDs(right); | ||||||
|  |  | ||||||
|         for (int i : rightIds) { |         for (int i : rightIds) { | ||||||
|             if (ArrayUtils.contains(leftIds, i)) { |             if (ArrayUtils.contains(leftIds, i)) { | ||||||
|  |                 oredictCache.put(code, true); | ||||||
|  |  | ||||||
|                 return true; |                 return true; | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         oredictCache.put(code, false); | ||||||
|  |  | ||||||
|         return false; |         return false; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Raoul Van den Berge
					Raoul Van den Berge