Revamped onChange Events to post lists of items making bulk operations much less cpu intensive.

This commit is contained in:
AlgorithmX2 2014-09-10 23:12:34 -05:00
parent 3f91868d5e
commit 5b79ad0b8c
12 changed files with 83 additions and 88 deletions

View file

@ -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<IAEItemStack> monitor, IAEItemStack change, BaseActionSource actionSource)
public void postChange(IBaseMonitor<IAEItemStack> monitor, Iterable<IAEItemStack> 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

View file

@ -317,9 +317,10 @@ public class ContainerMEMonitorable extends AEBaseContainer implements IConfigMa
}
@Override
public void postChange(IBaseMonitor<IAEItemStack> monitor, IAEItemStack change, BaseActionSource source)
public void postChange(IBaseMonitor<IAEItemStack> monitor, Iterable<IAEItemStack> change, BaseActionSource source)
{
items.add( change );
for (IAEItemStack is : change)
items.add( is );
}
@Override

View file

@ -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<IAEItemStack, Set<ICraftingPatternDetails>> 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

View file

@ -232,28 +232,10 @@ public class GridStorageCache implements IStorageGrid
switch (chan)
{
case FLUIDS:
for (IAEFluidStack fs : ((IItemList<IAEFluidStack>) 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<IAEFluidStack>) availableItems, src );
break;
case ITEMS:
for (IAEItemStack fs : ((IItemList<IAEItemStack>) 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<IAEItemStack>) 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<? extends IAEStack> input, BaseActionSource src)
{
if ( chan == StorageChannel.ITEMS )
itemMonitor.postChange( (IAEItemStack) input, src );
itemMonitor.postChange( true, (Iterable<IAEItemStack>) input, src );
else if ( chan == StorageChannel.FLUIDS )
fluidMonitor.postChange( (IAEFluidStack) input, src );
fluidMonitor.postChange( true, (Iterable<IAEFluidStack>) input, src );
}
@Override

View file

@ -48,8 +48,7 @@ public class NetworkMonitor<T extends IAEStack<T>> extends MEMonitorHandler<T>
final static public LinkedList depth = new LinkedList();
@Override
protected void postChange(T diff, BaseActionSource src)
protected void postChange(boolean Add, Iterable<T> changes, BaseActionSource src)
{
if ( depth.contains( this ) )
return;
@ -57,28 +56,36 @@ public class NetworkMonitor<T extends IAEStack<T>> extends MEMonitorHandler<T>
depth.push( this );
sendEvent = true;
super.postChange( diff, src );
postChangeToListeners( changes, src );
if ( myGridCache.interestManager.containsKey( diff ) )
IItemList<T> myStorageList = getStorageList();
for (T changedItem : changes)
{
Set<ItemWatcher> list = myGridCache.interestManager.get( diff );
if ( !list.isEmpty() )
{
IItemList<T> 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<ItemWatcher> 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();
}
}

View file

@ -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<Entry<IMEMonitorHandlerReceiver<IAEItemStack>, Object>> i = getListeners();
ImmutableList<IAEItemStack> single = null;
// protect integrity
if ( i.hasNext() )
diff = diff.copy();
single = ImmutableList.of( diff.copy() );
while (i.hasNext())
{
Entry<IMEMonitorHandlerReceiver<IAEItemStack>, Object> o = i.next();
IMEMonitorHandlerReceiver<IAEItemStack> 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<IAEItemStack> 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;

View file

@ -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<IAEItemStack>, IMEMonit
{
boolean changed = false;
LinkedList<IAEItemStack> changes = new LinkedList<IAEItemStack>();
list.resetStatus();
for (ItemSlot is : adaptor)
{
@ -178,12 +181,12 @@ public class MEMonitorIInventory implements IMEInventory<IAEItemStack>, 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<IAEItemStack>, 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<IAEItemStack>, IMEMonit
return !Platform.isSameItemPrecise( a, b );
}
private void postDiffrence(IAEItemStack a)
private void postDiffrence(Iterable<IAEItemStack> a)
{
// AELog.info( a.getItemStack().getUnlocalizedName() + " @ " + a.getStackSize() );
if ( a != null )

View file

@ -90,7 +90,7 @@ public class MEMonitorPassthu<T extends IAEStack<T>> extends MEPassthru<T> imple
}
@Override
public void postChange(IBaseMonitor<T> monitor, T change, BaseActionSource source)
public void postChange(IBaseMonitor<T> monitor, Iterable<T> change, BaseActionSource source)
{
Iterator<Entry<IMEMonitorHandlerReceiver<T>, Object>> i = listeners.entrySet().iterator();
while (i.hasNext())

View file

@ -257,7 +257,7 @@ public class PartLevelEmitter extends PartUpgradeable implements IEnergyWatcherH
}
@Override
public void postChange(IBaseMonitor<IAEItemStack> monitor, IAEItemStack change, BaseActionSource actionSource)
public void postChange(IBaseMonitor<IAEItemStack> monitor, Iterable<IAEItemStack> change, BaseActionSource actionSource)
{
updateReportingValue( (IMEMonitor<IAEItemStack>) monitor );
}

View file

@ -450,7 +450,7 @@ public class PartStorageBus extends PartUpgradeable implements IGridTickable, IC
}
@Override
public void postChange(IBaseMonitor<IAEItemStack> monitor, IAEItemStack change, BaseActionSource source)
public void postChange(IBaseMonitor<IAEItemStack> monitor, Iterable<IAEItemStack> change, BaseActionSource source)
{
try
{

View file

@ -304,7 +304,7 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, IFluidHan
}
@Override
public void postChange(IBaseMonitor<T> monitor, T change, BaseActionSource source)
public void postChange(IBaseMonitor<T> monitor, Iterable<T> change, BaseActionSource source)
{
if ( source == mySrc || (source instanceof PlayerSource && ((PlayerSource) source).via == TileChest.this) )
{

View file

@ -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<IAEItemStack> itemChanges = AEApi.instance().storage().createItemList();
IItemList<IAEFluidStack> fluidChanges = AEApi.instance().storage().createFluidList();
if ( removed != null )
{
IMEInventory<IAEItemStack> 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<IAEFluidStack> 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<IAEItemStack> 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<IAEFluidStack> 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 <T extends IAEStack<T>> void postListChanges(IItemList<T> before, IItemList<T> after, IMEMonitorHandlerReceiver<T> meMonitorPassthu,
BaseActionSource source)
{
LinkedList<T> 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)