From 5b79ad0b8cdcfca242159d7c0b0a0c5fd3543d9e Mon Sep 17 00:00:00 2001 From: AlgorithmX2 Date: Wed, 10 Sep 2014 23:12:34 -0500 Subject: [PATCH] Revamped onChange Events to post lists of items making bulk operations much less cpu intensive. --- .../implementations/ContainerCraftingCPU.java | 15 ++++--- .../ContainerMEMonitorable.java | 5 ++- me/cache/CraftingGridCache.java | 8 ++-- me/cache/GridStorageCache.java | 28 +++--------- me/cache/NetworkMonitor.java | 45 +++++++++++-------- .../implementations/CraftingCPUCluster.java | 11 +++-- me/storage/MEMonitorIInventory.java | 14 ++++-- me/storage/MEMonitorPassthu.java | 2 +- parts/automation/PartLevelEmitter.java | 2 +- parts/misc/PartStorageBus.java | 2 +- tile/storage/TileChest.java | 2 +- util/Platform.java | 37 +++++++-------- 12 files changed, 83 insertions(+), 88 deletions(-) diff --git a/container/implementations/ContainerCraftingCPU.java b/container/implementations/ContainerCraftingCPU.java index 21b4efe0..3db93e63 100644 --- a/container/implementations/ContainerCraftingCPU.java +++ b/container/implementations/ContainerCraftingCPU.java @@ -57,9 +57,9 @@ public class ContainerCraftingCPU extends AEBaseContainer implements IMEMonitorH protected void setCPU(ICraftingCPU c) { - if ( c== monitor) + if ( c == monitor ) return; - + if ( monitor != null ) monitor.removeListener( this ); @@ -185,11 +185,14 @@ public class ContainerCraftingCPU extends AEBaseContainer implements IMEMonitorH } @Override - public void postChange(IBaseMonitor monitor, IAEItemStack change, BaseActionSource actionSource) + public void postChange(IBaseMonitor monitor, Iterable change, BaseActionSource actionSource) { - change = change.copy(); - change.setStackSize( 1 ); - list.add( change ); + for (IAEItemStack is : change) + { + is = is.copy(); + is.setStackSize( 1 ); + list.add( is ); + } } @Override diff --git a/container/implementations/ContainerMEMonitorable.java b/container/implementations/ContainerMEMonitorable.java index 397f82dd..bb3e2fc9 100644 --- a/container/implementations/ContainerMEMonitorable.java +++ b/container/implementations/ContainerMEMonitorable.java @@ -317,9 +317,10 @@ public class ContainerMEMonitorable extends AEBaseContainer implements IConfigMa } @Override - public void postChange(IBaseMonitor monitor, IAEItemStack change, BaseActionSource source) + public void postChange(IBaseMonitor monitor, Iterable change, BaseActionSource source) { - items.add( change ); + for (IAEItemStack is : change) + items.add( is ); } @Override diff --git a/me/cache/CraftingGridCache.java b/me/cache/CraftingGridCache.java index 0e21ebc2..075bf13b 100644 --- a/me/cache/CraftingGridCache.java +++ b/me/cache/CraftingGridCache.java @@ -295,8 +295,7 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper emitableItems.clear(); // update the stuff that was in the list... - for (IAEItemStack out : oldItems.keySet()) - sg.postAlterationOfStoredItems( StorageChannel.ITEMS, out, new BaseActionSource() ); + sg.postAlterationOfStoredItems( StorageChannel.ITEMS, oldItems.keySet(), new BaseActionSource() ); // re-create list.. for (ICraftingProvider cp : providers) @@ -324,10 +323,9 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper // make them immutable for (Entry> e : tmpCraft.entrySet()) - { craftableItems.put( e.getKey(), ImmutableSortedSet.copyOf( e.getValue() ) ); - sg.postAlterationOfStoredItems( StorageChannel.ITEMS, e.getKey(), new BaseActionSource() ); - } + + sg.postAlterationOfStoredItems( StorageChannel.ITEMS, craftableItems.keySet(), new BaseActionSource() ); } @Override diff --git a/me/cache/GridStorageCache.java b/me/cache/GridStorageCache.java index 7da2bd5e..ee494912 100644 --- a/me/cache/GridStorageCache.java +++ b/me/cache/GridStorageCache.java @@ -232,28 +232,10 @@ public class GridStorageCache implements IStorageGrid switch (chan) { case FLUIDS: - for (IAEFluidStack fs : ((IItemList) availableItems)) - { - if ( up_or_down > 0 ) - fluidMonitor.postChange( fs, src ); - else - { - fs.setStackSize( -fs.getStackSize() ); - fluidMonitor.postChange( fs, src ); - } - } + fluidMonitor.postChange( up_or_down > 0, (IItemList) availableItems, src ); break; case ITEMS: - for (IAEItemStack fs : ((IItemList) availableItems)) - { - if ( up_or_down > 0 ) - itemMonitor.postChange( fs, src ); - else - { - fs.setStackSize( -fs.getStackSize() ); - itemMonitor.postChange( fs, src ); - } - } + itemMonitor.postChange( up_or_down > 0, (IItemList) availableItems, src ); break; default: } @@ -274,12 +256,12 @@ public class GridStorageCache implements IStorageGrid } @Override - public void postAlterationOfStoredItems(StorageChannel chan, IAEStack input, BaseActionSource src) + public void postAlterationOfStoredItems(StorageChannel chan, Iterable input, BaseActionSource src) { if ( chan == StorageChannel.ITEMS ) - itemMonitor.postChange( (IAEItemStack) input, src ); + itemMonitor.postChange( true, (Iterable) input, src ); else if ( chan == StorageChannel.FLUIDS ) - fluidMonitor.postChange( (IAEFluidStack) input, src ); + fluidMonitor.postChange( true, (Iterable) input, src ); } @Override diff --git a/me/cache/NetworkMonitor.java b/me/cache/NetworkMonitor.java index dfccffc9..ceb14a99 100644 --- a/me/cache/NetworkMonitor.java +++ b/me/cache/NetworkMonitor.java @@ -48,8 +48,7 @@ public class NetworkMonitor> extends MEMonitorHandler final static public LinkedList depth = new LinkedList(); - @Override - protected void postChange(T diff, BaseActionSource src) + protected void postChange(boolean Add, Iterable changes, BaseActionSource src) { if ( depth.contains( this ) ) return; @@ -57,28 +56,36 @@ public class NetworkMonitor> extends MEMonitorHandler depth.push( this ); sendEvent = true; - super.postChange( diff, src ); + postChangeToListeners( changes, src ); - if ( myGridCache.interestManager.containsKey( diff ) ) + IItemList myStorageList = getStorageList(); + + for (T changedItem : changes) { - Set list = myGridCache.interestManager.get( diff ); - if ( !list.isEmpty() ) - { - IItemList myStorageList = getStorageList(); + T diffrence = changedItem; - IAEStack fullStack = myStorageList.findPrecise( diff ); - if ( fullStack == null ) + if ( !Add && changedItem != null ) + (diffrence = changedItem.copy()).setStackSize( -changedItem.getStackSize() ); + + if ( myGridCache.interestManager.containsKey( changedItem ) ) + { + Set list = myGridCache.interestManager.get( changedItem ); + if ( !list.isEmpty() ) { - fullStack = diff.copy(); - fullStack.setStackSize( 0 ); + IAEStack fullStack = myStorageList.findPrecise( changedItem ); + if ( fullStack == null ) + { + fullStack = changedItem.copy(); + fullStack.setStackSize( 0 ); + } + + myGridCache.interestManager.enableTransactions(); + + for (ItemWatcher iw : list) + iw.getHost().onStackChange( myStorageList, fullStack, diffrence, src, getChannel() ); + + myGridCache.interestManager.disableTransactions(); } - - myGridCache.interestManager.enableTransactions(); - - for (ItemWatcher iw : list) - iw.getHost().onStackChange( myStorageList, fullStack, diff, src, getChannel() ); - - myGridCache.interestManager.disableTransactions(); } } diff --git a/me/cluster/implementations/CraftingCPUCluster.java b/me/cluster/implementations/CraftingCPUCluster.java index 0abe62e2..8eaa87d3 100644 --- a/me/cluster/implementations/CraftingCPUCluster.java +++ b/me/cluster/implementations/CraftingCPUCluster.java @@ -54,6 +54,7 @@ import appeng.tile.crafting.TileCraftingTile; import appeng.util.Platform; import appeng.util.item.AEItemStack; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import cpw.mods.fml.common.FMLCommonHandler; @@ -108,16 +109,18 @@ public class CraftingCPUCluster implements IAECluster, ICraftingCPU { Iterator, Object>> i = getListeners(); + ImmutableList single = null; + // protect integrity if ( i.hasNext() ) - diff = diff.copy(); + single = ImmutableList.of( diff.copy() ); while (i.hasNext()) { Entry, Object> o = i.next(); IMEMonitorHandlerReceiver recv = o.getKey(); if ( recv.isValid( o.getValue() ) ) - recv.postChange( null, diff, src ); + recv.postChange( null, single, src ); else i.remove(); } @@ -494,8 +497,8 @@ public class CraftingCPUCluster implements IAECluster, ICraftingCPU IItemList list; getListOfItem( list = AEApi.instance().storage().createItemList(), CraftingItemList.ALL ); - for (IAEItemStack g : list) - postChange( g, machineSrc ); + for (IAEItemStack is : list) + postChange( is, machineSrc ); isComplete = true; myLastLink = null; diff --git a/me/storage/MEMonitorIInventory.java b/me/storage/MEMonitorIInventory.java index a7dce719..93144f15 100644 --- a/me/storage/MEMonitorIInventory.java +++ b/me/storage/MEMonitorIInventory.java @@ -2,6 +2,7 @@ package appeng.me.storage; import java.util.HashMap; import java.util.Iterator; +import java.util.LinkedList; import java.util.Map.Entry; import java.util.TreeMap; @@ -163,6 +164,8 @@ public class MEMonitorIInventory implements IMEInventory, IMEMonit { boolean changed = false; + LinkedList changes = new LinkedList(); + list.resetStatus(); for (ItemSlot is : adaptor) { @@ -178,12 +181,12 @@ public class MEMonitorIInventory implements IMEInventory, IMEMonit if ( old != null && old.aeStack != null ) { old.aeStack.setStackSize( -old.aeStack.getStackSize() ); - postDiffrence( old.aeStack ); + changes.add( old.aeStack ); } if ( cis != null && cis.aeStack != null ) { - postDiffrence( cis.aeStack ); + changes.add( cis.aeStack ); list.add( cis.aeStack ); } @@ -208,12 +211,15 @@ public class MEMonitorIInventory implements IMEInventory, IMEMonit IAEItemStack a = stack.copy(); a.setStackSize( diff ); - postDiffrence( a ); + changes.add( a ); changed = true; } } } + if ( !changes.isEmpty() ) + postDiffrence( changes ); + return changed ? TickRateModulation.URGENT : TickRateModulation.SLOWER; } @@ -228,7 +234,7 @@ public class MEMonitorIInventory implements IMEInventory, IMEMonit return !Platform.isSameItemPrecise( a, b ); } - private void postDiffrence(IAEItemStack a) + private void postDiffrence(Iterable a) { // AELog.info( a.getItemStack().getUnlocalizedName() + " @ " + a.getStackSize() ); if ( a != null ) diff --git a/me/storage/MEMonitorPassthu.java b/me/storage/MEMonitorPassthu.java index 6e8cf6d8..3587e30a 100644 --- a/me/storage/MEMonitorPassthu.java +++ b/me/storage/MEMonitorPassthu.java @@ -90,7 +90,7 @@ public class MEMonitorPassthu> extends MEPassthru imple } @Override - public void postChange(IBaseMonitor monitor, T change, BaseActionSource source) + public void postChange(IBaseMonitor monitor, Iterable change, BaseActionSource source) { Iterator, Object>> i = listeners.entrySet().iterator(); while (i.hasNext()) diff --git a/parts/automation/PartLevelEmitter.java b/parts/automation/PartLevelEmitter.java index 67d1397e..8ffe4be6 100644 --- a/parts/automation/PartLevelEmitter.java +++ b/parts/automation/PartLevelEmitter.java @@ -257,7 +257,7 @@ public class PartLevelEmitter extends PartUpgradeable implements IEnergyWatcherH } @Override - public void postChange(IBaseMonitor monitor, IAEItemStack change, BaseActionSource actionSource) + public void postChange(IBaseMonitor monitor, Iterable change, BaseActionSource actionSource) { updateReportingValue( (IMEMonitor) monitor ); } diff --git a/parts/misc/PartStorageBus.java b/parts/misc/PartStorageBus.java index 6e7293c2..5f0546ae 100644 --- a/parts/misc/PartStorageBus.java +++ b/parts/misc/PartStorageBus.java @@ -450,7 +450,7 @@ public class PartStorageBus extends PartUpgradeable implements IGridTickable, IC } @Override - public void postChange(IBaseMonitor monitor, IAEItemStack change, BaseActionSource source) + public void postChange(IBaseMonitor monitor, Iterable change, BaseActionSource source) { try { diff --git a/tile/storage/TileChest.java b/tile/storage/TileChest.java index 92e93fa6..676eb7d4 100644 --- a/tile/storage/TileChest.java +++ b/tile/storage/TileChest.java @@ -304,7 +304,7 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, IFluidHan } @Override - public void postChange(IBaseMonitor monitor, T change, BaseActionSource source) + public void postChange(IBaseMonitor monitor, Iterable change, BaseActionSource source) { if ( source == mySrc || (source instanceof PlayerSource && ((PlayerSource) source).via == TileChest.this) ) { diff --git a/util/Platform.java b/util/Platform.java index d52c9147..7e798aca 100644 --- a/util/Platform.java +++ b/util/Platform.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.EnumSet; import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import java.util.Random; import java.util.Set; @@ -1379,28 +1380,25 @@ public class Platform public static void postChanges(IStorageGrid gs, ItemStack removed, ItemStack added, BaseActionSource src) { + IItemList itemChanges = AEApi.instance().storage().createItemList(); + IItemList fluidChanges = AEApi.instance().storage().createFluidList(); + if ( removed != null ) { IMEInventory myItems = AEApi.instance().registries().cell().getCellInventory( removed, null, StorageChannel.ITEMS ); if ( myItems != null ) { - for (IAEItemStack is : myItems.getAvailableItems( AEApi.instance().storage().createItemList() )) - { + for (IAEItemStack is : myItems.getAvailableItems( itemChanges )) is.setStackSize( -is.getStackSize() ); - gs.postAlterationOfStoredItems( StorageChannel.ITEMS, is, src ); - } } IMEInventory myFluids = AEApi.instance().registries().cell().getCellInventory( removed, null, StorageChannel.FLUIDS ); if ( myFluids != null ) { - for (IAEFluidStack is : myFluids.getAvailableItems( AEApi.instance().storage().createFluidList() )) - { + for (IAEFluidStack is : myFluids.getAvailableItems( fluidChanges )) is.setStackSize( -is.getStackSize() ); - gs.postAlterationOfStoredItems( StorageChannel.FLUIDS, is, src ); - } } } @@ -1409,28 +1407,22 @@ public class Platform IMEInventory myItems = AEApi.instance().registries().cell().getCellInventory( added, null, StorageChannel.ITEMS ); if ( myItems != null ) - { - for (IAEItemStack is : myItems.getAvailableItems( AEApi.instance().storage().createItemList() )) - { - gs.postAlterationOfStoredItems( StorageChannel.ITEMS, is, src ); - } - } + myItems.getAvailableItems( itemChanges ); IMEInventory myFluids = AEApi.instance().registries().cell().getCellInventory( added, null, StorageChannel.FLUIDS ); if ( myFluids != null ) - { - for (IAEFluidStack is : myFluids.getAvailableItems( AEApi.instance().storage().createFluidList() )) - { - gs.postAlterationOfStoredItems( StorageChannel.FLUIDS, is, src ); - } - } + myFluids.getAvailableItems( fluidChanges ); } + + gs.postAlterationOfStoredItems( StorageChannel.ITEMS, itemChanges, src ); } static public > void postListChanges(IItemList before, IItemList after, IMEMonitorHandlerReceiver meMonitorPassthu, BaseActionSource source) { + LinkedList changes = new LinkedList(); + for (T is : before) is.setStackSize( -is.getStackSize() ); @@ -1441,9 +1433,12 @@ public class Platform { if ( is.getStackSize() != 0 ) { - meMonitorPassthu.postChange( null, is, source ); + changes.add( is ); } } + + if ( !changes.isEmpty() ) + meMonitorPassthu.postChange( null, changes, source ); } public static int generateTileHash(TileEntity target)