Improve AESharedItemStack caching
Reduces the item stack churn a little, improving overall server time taken from 6% to 2% on average
This commit is contained in:
@@ -23,14 +23,16 @@
|
||||
|
||||
package appeng.util.item;
|
||||
|
||||
import com.google.common.collect.MapMaker;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public final class AEItemStackRegistry {
|
||||
private static final WeakHashMap<AESharedItemStack, WeakReference<AESharedItemStack>> REGISTRY = new WeakHashMap<>();
|
||||
|
||||
private static final ItemStackHashStrategy HASH_STRATEGY = ItemStackHashStrategy.comparingAllButCount();
|
||||
private static final Map<Integer, AESharedItemStack> REGISTRY = new MapMaker().weakValues().makeMap();
|
||||
|
||||
private AEItemStackRegistry() {
|
||||
}
|
||||
@@ -40,23 +42,18 @@ public final class AEItemStackRegistry {
|
||||
throw new IllegalArgumentException("stack cannot be empty");
|
||||
}
|
||||
|
||||
int oldStackSize = itemStack.getCount();
|
||||
itemStack.setCount(1);
|
||||
|
||||
AESharedItemStack search = new AESharedItemStack(itemStack);
|
||||
WeakReference<AESharedItemStack> weak = REGISTRY.get(search);
|
||||
AESharedItemStack ret = null;
|
||||
|
||||
if (weak != null) {
|
||||
ret = weak.get();
|
||||
}
|
||||
|
||||
if (ret == null) {
|
||||
ret = new AESharedItemStack(itemStack.copy());
|
||||
REGISTRY.put(ret, new WeakReference<>(ret));
|
||||
}
|
||||
itemStack.setCount(oldStackSize);
|
||||
|
||||
var hash = HASH_STRATEGY.hashCode(itemStack);
|
||||
var ret = REGISTRY.get(hash);
|
||||
if (ret != null) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// computeIfAbsent is not feasible since new AESharedItemStack gets
|
||||
// instantly GC'd when leaving the lambda.
|
||||
var itemStackCopy = itemStack.copy();
|
||||
itemStackCopy.setCount(1);
|
||||
var sharedStack = new AESharedItemStack(itemStackCopy);
|
||||
REGISTRY.put(hash, sharedStack);
|
||||
return sharedStack;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user