Small performance improvement: only sort the storages when needed, #1145

This commit is contained in:
raoulvdberge
2017-04-22 12:10:36 +02:00
parent b6479aea1c
commit e2ce330397
10 changed files with 49 additions and 9 deletions

View File

@@ -13,6 +13,7 @@
- You can now use up and down arrows to scroll through Grid search history (raoulvdberge) - You can now use up and down arrows to scroll through Grid search history (raoulvdberge)
- Fixed Grid crash (raoulvdberge) - Fixed Grid crash (raoulvdberge)
- Fixed Fluid Grid not formatting large quantities correctly (raoulvdberge) - Fixed Fluid Grid not formatting large quantities correctly (raoulvdberge)
- Small performance improvement: only sort the storages when needed (raoulvdberge)
### 1.4.2 ### 1.4.2
- Updated Forge to 2261 (raoulvdberge) - Updated Forge to 2261 (raoulvdberge)

View File

@@ -2,6 +2,7 @@ package com.raoulvdberge.refinedstorage;
import com.raoulvdberge.refinedstorage.api.network.INetworkMaster; import com.raoulvdberge.refinedstorage.api.network.INetworkMaster;
import com.raoulvdberge.refinedstorage.api.storage.AccessType; import com.raoulvdberge.refinedstorage.api.storage.AccessType;
import com.raoulvdberge.refinedstorage.api.storage.IStorage;
import com.raoulvdberge.refinedstorage.api.storage.IStorageDisk; import com.raoulvdberge.refinedstorage.api.storage.IStorageDisk;
import com.raoulvdberge.refinedstorage.api.storage.IStorageDiskProvider; import com.raoulvdberge.refinedstorage.api.storage.IStorageDiskProvider;
import com.raoulvdberge.refinedstorage.api.util.IStackList; import com.raoulvdberge.refinedstorage.api.util.IStackList;
@@ -63,6 +64,12 @@ import java.util.function.Function;
public final class RSUtils { public final class RSUtils {
public static final ItemStack EMPTY_BUCKET = new ItemStack(Items.BUCKET); public static final ItemStack EMPTY_BUCKET = new ItemStack(Items.BUCKET);
public static final Comparator<IStorage> STORAGE_COMPARATOR = (left, right) -> {
int compare = Integer.compare(right.getPriority(), left.getPriority());
return compare != 0 ? compare : Integer.compare(right.getStored(), left.getStored());
};
public static final DecimalFormat QUANTITY_FORMATTER = new DecimalFormat("####0.#", DecimalFormatSymbols.getInstance(Locale.US)); public static final DecimalFormat QUANTITY_FORMATTER = new DecimalFormat("####0.#", DecimalFormatSymbols.getInstance(Locale.US));
private static final String NBT_INVENTORY = "Inventory_%d"; private static final String NBT_INVENTORY = "Inventory_%d";

View File

@@ -16,6 +16,7 @@ import java.util.List;
public interface IStorageCache<T> { public interface IStorageCache<T> {
/** /**
* Invalidates the cache. * Invalidates the cache.
* Should also call {@link IStorageCache#sort()} to sort the storages correctly.
* Typically called when a {@link IStorageProvider} is added or removed from the network. * Typically called when a {@link IStorageProvider} is added or removed from the network.
*/ */
void invalidate(); void invalidate();
@@ -45,6 +46,12 @@ public interface IStorageCache<T> {
*/ */
void remove(@Nonnull T stack, int size); void remove(@Nonnull T stack, int size);
/**
* Resorts the storages in this cache according to their priority. This needs to be called when the priority
* of a storage changes.
*/
void sort();
/** /**
* @return the list behind this cache * @return the list behind this cache
*/ */

View File

@@ -316,6 +316,10 @@ public class NetworkNodeFluidStorage extends NetworkNode implements IGuiStorage,
this.priority = priority; this.priority = priority;
markDirty(); markDirty();
if (network != null) {
network.getFluidStorageCache().sort();
}
} }
@Override @Override

View File

@@ -323,5 +323,9 @@ public class NetworkNodeStorage extends NetworkNode implements IGuiStorage, ISto
this.priority = priority; this.priority = priority;
markDirty(); markDirty();
if (network != null) {
network.getItemStorageCache().sort();
}
} }
} }

View File

@@ -338,6 +338,11 @@ public class NetworkNodeDiskDrive extends NetworkNode implements IGuiStorage, IS
this.priority = priority; this.priority = priority;
markDirty(); markDirty();
if (network != null) {
network.getItemStorageCache().sort();
network.getFluidStorageCache().sort();
}
} }
public IItemHandler getDisks() { public IItemHandler getDisks() {

View File

@@ -181,6 +181,11 @@ public class NetworkNodeExternalStorage extends NetworkNode implements IStorageP
this.priority = priority; this.priority = priority;
markDirty(); markDirty();
if (network != null) {
network.getItemStorageCache().sort();
network.getFluidStorageCache().sort();
}
} }
public void updateStorage(INetworkMaster network) { public void updateStorage(INetworkMaster network) {

View File

@@ -1,5 +1,6 @@
package com.raoulvdberge.refinedstorage.apiimpl.storage; package com.raoulvdberge.refinedstorage.apiimpl.storage;
import com.raoulvdberge.refinedstorage.RSUtils;
import com.raoulvdberge.refinedstorage.api.network.INetworkMaster; import com.raoulvdberge.refinedstorage.api.network.INetworkMaster;
import com.raoulvdberge.refinedstorage.api.storage.AccessType; import com.raoulvdberge.refinedstorage.api.storage.AccessType;
import com.raoulvdberge.refinedstorage.api.storage.IStorage; import com.raoulvdberge.refinedstorage.api.storage.IStorage;
@@ -33,6 +34,8 @@ public class StorageCacheFluid implements IStorageCache<FluidStack> {
list.clear(); list.clear();
sort();
for (IStorage<FluidStack> storage : storages) { for (IStorage<FluidStack> storage : storages) {
if (storage.getAccessType() == AccessType.INSERT) { if (storage.getAccessType() == AccessType.INSERT) {
continue; continue;
@@ -62,6 +65,11 @@ public class StorageCacheFluid implements IStorageCache<FluidStack> {
} }
} }
@Override
public void sort() {
storages.sort(RSUtils.STORAGE_COMPARATOR);
}
@Override @Override
public IStackList<FluidStack> getList() { public IStackList<FluidStack> getList() {
return list; return list;

View File

@@ -1,5 +1,6 @@
package com.raoulvdberge.refinedstorage.apiimpl.storage; package com.raoulvdberge.refinedstorage.apiimpl.storage;
import com.raoulvdberge.refinedstorage.RSUtils;
import com.raoulvdberge.refinedstorage.api.network.INetworkMaster; import com.raoulvdberge.refinedstorage.api.network.INetworkMaster;
import com.raoulvdberge.refinedstorage.api.storage.AccessType; import com.raoulvdberge.refinedstorage.api.storage.AccessType;
import com.raoulvdberge.refinedstorage.api.storage.IStorage; import com.raoulvdberge.refinedstorage.api.storage.IStorage;
@@ -33,6 +34,8 @@ public class StorageCacheItem implements IStorageCache<ItemStack> {
list.clear(); list.clear();
sort();
for (IStorage<ItemStack> storage : storages) { for (IStorage<ItemStack> storage : storages) {
if (storage.getAccessType() == AccessType.INSERT) { if (storage.getAccessType() == AccessType.INSERT) {
continue; continue;
@@ -64,6 +67,11 @@ public class StorageCacheItem implements IStorageCache<ItemStack> {
} }
} }
@Override
public void sort() {
storages.sort(RSUtils.STORAGE_COMPARATOR);
}
@Override @Override
public IStackList<ItemStack> getList() { public IStackList<ItemStack> getList() {
return list; return list;

View File

@@ -142,12 +142,6 @@ public class TileController extends TileBase implements INetworkMaster, IRedston
return (left.getEnergyUsage() > right.getEnergyUsage()) ? -1 : 1; return (left.getEnergyUsage() > right.getEnergyUsage()) ? -1 : 1;
}; };
private static final Comparator<IStorage> STORAGE_COMPARATOR = (left, right) -> {
int compare = Integer.compare(right.getPriority(), left.getPriority());
return compare != 0 ? compare : Integer.compare(right.getStored(), left.getStored());
};
private IItemGridHandler itemGridHandler = new ItemGridHandler(this); private IItemGridHandler itemGridHandler = new ItemGridHandler(this);
private IFluidGridHandler fluidGridHandler = new FluidGridHandler(this); private IFluidGridHandler fluidGridHandler = new FluidGridHandler(this);
@@ -222,9 +216,6 @@ public class TileController extends TileBase implements INetworkMaster, IRedston
public void update() { public void update() {
if (!getWorld().isRemote) { if (!getWorld().isRemote) {
if (canRun()) { if (canRun()) {
itemStorage.getStorages().sort(STORAGE_COMPARATOR);
fluidStorage.getStorages().sort(STORAGE_COMPARATOR);
craftingManager.update(); craftingManager.update();
for (IReaderWriterChannel channel : readerWriterChannels.values()) { for (IReaderWriterChannel channel : readerWriterChannels.values()) {