Safer implementation of CraftingGridCache updatePatterns coalescing

This commit is contained in:
talchas
2019-12-09 22:39:38 -08:00
parent 401f1ebb3e
commit 34c490f92b
3 changed files with 40 additions and 11 deletions
+5 -1
View File
@@ -39,6 +39,7 @@ import appeng.api.util.IReadOnlyCollection;
import appeng.core.worlddata.WorldData; import appeng.core.worlddata.WorldData;
import appeng.hooks.TickHandler; import appeng.hooks.TickHandler;
import appeng.util.ReadOnlyCollection; import appeng.util.ReadOnlyCollection;
import appeng.me.cache.CraftingGridCache;
public class Grid implements IGrid public class Grid implements IGrid
@@ -219,7 +220,10 @@ public class Grid implements IGrid
@Override @Override
public MENetworkEvent postEvent( final MENetworkEvent ev ) public MENetworkEvent postEvent( final MENetworkEvent ev )
{ {
return this.eventBus.postEvent( this, ev ); CraftingGridCache.pauseRebuilds();
final MENetworkEvent ret = this.eventBus.postEvent( this, ev );
CraftingGridCache.unpauseRebuilds();
return ret;
} }
@Override @Override
+4
View File
@@ -55,6 +55,7 @@ import appeng.api.util.IReadOnlyCollection;
import appeng.core.AELog; import appeng.core.AELog;
import appeng.core.worlddata.WorldData; import appeng.core.worlddata.WorldData;
import appeng.hooks.TickHandler; import appeng.hooks.TickHandler;
import appeng.me.cache.CraftingGridCache;
import appeng.me.pathfinding.IPathItem; import appeng.me.pathfinding.IPathItem;
import appeng.util.IWorldCallable; import appeng.util.IWorldCallable;
import appeng.util.ReadOnlyCollection; import appeng.util.ReadOnlyCollection;
@@ -164,6 +165,8 @@ public class GridNode implements IGridNode, IPathItem
{ {
final Object tracker = new Object(); final Object tracker = new Object();
CraftingGridCache.pauseRebuilds();
Deque<GridNode> nextRun = new ArrayDeque<>(); Deque<GridNode> nextRun = new ArrayDeque<>();
nextRun.add( this ); nextRun.add( this );
@@ -203,6 +206,7 @@ public class GridNode implements IGridNode, IPathItem
} }
} }
} }
CraftingGridCache.unpauseRebuilds();
} }
@Override @Override
+31 -10
View File
@@ -114,7 +114,8 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
private IStorageGrid storageGrid; private IStorageGrid storageGrid;
private IEnergyGrid energyGrid; private IEnergyGrid energyGrid;
private boolean updateList = false; private boolean updateList = false;
private boolean updatePatterns = false; private static int pauseRebuilds = 0;
private static Set<CraftingGridCache> rebuildNeeded = new HashSet<>();
public CraftingGridCache( final IGrid grid ) public CraftingGridCache( final IGrid grid )
{ {
@@ -139,12 +140,6 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
this.updateCPUClusters(); this.updateCPUClusters();
} }
if( this.updatePatterns )
{
this.updatePatterns = false;
this.updatePatterns();
}
final Iterator<CraftingLinkNexus> craftingLinkIterator = this.craftingLinks.values().iterator(); final Iterator<CraftingLinkNexus> craftingLinkIterator = this.craftingLinks.values().iterator();
while( craftingLinkIterator.hasNext() ) while( craftingLinkIterator.hasNext() )
{ {
@@ -192,7 +187,7 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
if( machine instanceof ICraftingProvider ) if( machine instanceof ICraftingProvider )
{ {
this.craftingProviders.remove( machine ); this.craftingProviders.remove( machine );
this.updatePatterns = true; this.updatePatterns();
} }
} }
@@ -226,7 +221,7 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
if( machine instanceof ICraftingProvider ) if( machine instanceof ICraftingProvider )
{ {
this.craftingProviders.add( (ICraftingProvider) machine ); this.craftingProviders.add( (ICraftingProvider) machine );
this.updatePatterns = true; this.updatePatterns();
} }
} }
@@ -247,8 +242,34 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
// nothing! // nothing!
} }
public static void pauseRebuilds()
{
pauseRebuilds++;
}
public static void unpauseRebuilds()
{
pauseRebuilds--;
if (pauseRebuilds == 0 && rebuildNeeded.size() > 0)
{
ImmutableSet<CraftingGridCache> needed = ImmutableSet.copyOf(rebuildNeeded);
rebuildNeeded.clear();
for ( CraftingGridCache cache: needed )
{
cache.updatePatterns();
}
}
}
private void updatePatterns() private void updatePatterns()
{ {
// coalesce change events during a grid traversal to a single rebuild
if (pauseRebuilds != 0)
{
rebuildNeeded.add(this);
return;
}
final Map<IAEItemStack, ImmutableList<ICraftingPatternDetails>> oldItems = this.craftableItems; final Map<IAEItemStack, ImmutableList<ICraftingPatternDetails>> oldItems = this.craftableItems;
// erase list. // erase list.
@@ -344,7 +365,7 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
@MENetworkEventSubscribe @MENetworkEventSubscribe
public void updateCPUClusters( final MENetworkCraftingPatternChange c ) public void updateCPUClusters( final MENetworkCraftingPatternChange c )
{ {
this.updatePatterns = true; this.updatePatterns();
} }
@Override @Override