Merge pull request #3026 from necauqua/moar-perf
Reuse ItemExternalStorageCache to avoid excessive calls to getStackInSlot
This commit is contained in:
@@ -117,19 +117,7 @@ public class FluidExternalStorage implements IExternalStorage<FluidStack> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getStored() {
|
public int getStored() {
|
||||||
IFluidHandler fluidHandler = handlerSupplier.get();
|
return cache.getStored();
|
||||||
|
|
||||||
if (fluidHandler != null) {
|
|
||||||
int stored = 0;
|
|
||||||
|
|
||||||
for (int i = 0; i < fluidHandler.getTanks(); ++i) {
|
|
||||||
stored += fluidHandler.getFluidInTank(i).getAmount();
|
|
||||||
}
|
|
||||||
|
|
||||||
return stored;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -12,24 +12,36 @@ import java.util.List;
|
|||||||
|
|
||||||
public class FluidExternalStorageCache {
|
public class FluidExternalStorageCache {
|
||||||
private List<FluidStack> cache;
|
private List<FluidStack> cache;
|
||||||
|
private int stored = 0;
|
||||||
|
|
||||||
|
public int getStored() {
|
||||||
|
return stored;
|
||||||
|
}
|
||||||
|
|
||||||
public void update(INetwork network, @Nullable IFluidHandler handler) {
|
public void update(INetwork network, @Nullable IFluidHandler handler) {
|
||||||
if (handler == null) {
|
if (handler == null) {
|
||||||
|
stored = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cache == null) {
|
if (cache == null) {
|
||||||
cache = new ArrayList<>();
|
cache = new ArrayList<>();
|
||||||
|
|
||||||
|
int stored = 0;
|
||||||
for (int i = 0; i < handler.getTanks(); ++i) {
|
for (int i = 0; i < handler.getTanks(); ++i) {
|
||||||
cache.add(handler.getFluidInTank(i).copy());
|
FluidStack stack = handler.getFluidInTank(i).copy();
|
||||||
|
cache.add(stack);
|
||||||
|
stored += stack.getAmount();
|
||||||
}
|
}
|
||||||
|
this.stored = stored;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int stored = 0;
|
||||||
for (int i = 0; i < handler.getTanks(); ++i) {
|
for (int i = 0; i < handler.getTanks(); ++i) {
|
||||||
FluidStack actual = handler.getFluidInTank(i);
|
FluidStack actual = handler.getFluidInTank(i);
|
||||||
|
stored += actual.getAmount();
|
||||||
|
|
||||||
if (i >= cache.size()) { // ENLARGED
|
if (i >= cache.size()) { // ENLARGED
|
||||||
if (!actual.isEmpty()) {
|
if (!actual.isEmpty()) {
|
||||||
@@ -70,6 +82,7 @@ public class FluidExternalStorageCache {
|
|||||||
cached.setAmount(actual.getAmount());
|
cached.setAmount(actual.getAmount());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.stored = stored;
|
||||||
|
|
||||||
if (cache.size() > handler.getTanks()) { // SHRUNK
|
if (cache.size() > handler.getTanks()) { // SHRUNK
|
||||||
for (int i = cache.size() - 1; i >= handler.getTanks(); --i) { // Reverse order for the remove call.
|
for (int i = cache.size() - 1; i >= handler.getTanks(); --i) { // Reverse order for the remove call.
|
||||||
|
@@ -137,19 +137,7 @@ public class ItemExternalStorage implements IExternalStorage<ItemStack> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getStored() {
|
public int getStored() {
|
||||||
IItemHandler handler = handlerSupplier.get();
|
return cache.getStored();
|
||||||
|
|
||||||
if (handler == null) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int size = 0;
|
|
||||||
|
|
||||||
for (int i = 0; i < handler.getSlots(); ++i) {
|
|
||||||
size += handler.getStackInSlot(i).getCount();
|
|
||||||
}
|
|
||||||
|
|
||||||
return size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -11,24 +11,36 @@ import java.util.List;
|
|||||||
|
|
||||||
public class ItemExternalStorageCache {
|
public class ItemExternalStorageCache {
|
||||||
private List<ItemStack> cache;
|
private List<ItemStack> cache;
|
||||||
|
private int stored = 0;
|
||||||
|
|
||||||
|
public int getStored() {
|
||||||
|
return stored;
|
||||||
|
}
|
||||||
|
|
||||||
public void update(INetwork network, @Nullable IItemHandler handler) {
|
public void update(INetwork network, @Nullable IItemHandler handler) {
|
||||||
if (handler == null) {
|
if (handler == null) {
|
||||||
|
stored = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cache == null) {
|
if (cache == null) {
|
||||||
cache = new ArrayList<>();
|
cache = new ArrayList<>();
|
||||||
|
|
||||||
|
int stored = 0;
|
||||||
for (int i = 0; i < handler.getSlots(); ++i) {
|
for (int i = 0; i < handler.getSlots(); ++i) {
|
||||||
cache.add(handler.getStackInSlot(i).copy());
|
ItemStack stack = handler.getStackInSlot(i).copy();
|
||||||
|
cache.add(stack);
|
||||||
|
stored += stack.getCount();
|
||||||
}
|
}
|
||||||
|
this.stored = stored;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int stored = 0;
|
||||||
for (int i = 0; i < handler.getSlots(); ++i) {
|
for (int i = 0; i < handler.getSlots(); ++i) {
|
||||||
ItemStack actual = handler.getStackInSlot(i);
|
ItemStack actual = handler.getStackInSlot(i);
|
||||||
|
stored += actual.getCount();
|
||||||
|
|
||||||
if (i >= cache.size()) { // ENLARGED
|
if (i >= cache.size()) { // ENLARGED
|
||||||
if (!actual.isEmpty()) {
|
if (!actual.isEmpty()) {
|
||||||
@@ -69,6 +81,7 @@ public class ItemExternalStorageCache {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.stored = stored;
|
||||||
|
|
||||||
if (cache.size() > handler.getSlots()) { // SHRUNK
|
if (cache.size() > handler.getSlots()) { // SHRUNK
|
||||||
for (int i = cache.size() - 1; i >= handler.getSlots(); --i) { // Reverse order for the remove call.
|
for (int i = cache.size() - 1; i >= handler.getSlots(); --i) { // Reverse order for the remove call.
|
||||||
|
Reference in New Issue
Block a user