Compare commits

...

4 Commits

Author SHA1 Message Date
PrototypeTrousers 2e0250de37 Merge pull request #1 from talchas/rv6-1.12
merge talchas fixes
2020-10-20 14:32:48 -03:00
yueh d730503378 Fixes #4388: Ensure correct ticking priority order (#4393) 2020-05-27 16:13:20 +02:00
talchas 34c490f92b Safer implementation of CraftingGridCache updatePatterns coalescing 2019-12-20 19:54:14 -08:00
talchas 401f1ebb3e Avoid quadratic behavior from rebuilding the entire crafting cache for
each interface visited. Instead rebuild it at most once per tick, like
cpu clusters.

This is particularly important when channels are disabled, since in
that case even adding a cable visits every grid node.
2019-12-09 22:43:55 -08:00
5 changed files with 98 additions and 57 deletions
+5 -1
View File
@@ -39,6 +39,7 @@ import appeng.api.util.IReadOnlyCollection;
import appeng.core.worlddata.WorldData;
import appeng.hooks.TickHandler;
import appeng.util.ReadOnlyCollection;
import appeng.me.cache.CraftingGridCache;
public class Grid implements IGrid
@@ -219,7 +220,10 @@ public class Grid implements IGrid
@Override
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
+4
View File
@@ -55,6 +55,7 @@ import appeng.api.util.IReadOnlyCollection;
import appeng.core.AELog;
import appeng.core.worlddata.WorldData;
import appeng.hooks.TickHandler;
import appeng.me.cache.CraftingGridCache;
import appeng.me.pathfinding.IPathItem;
import appeng.util.IWorldCallable;
import appeng.util.ReadOnlyCollection;
@@ -164,6 +165,8 @@ public class GridNode implements IGridNode, IPathItem
{
final Object tracker = new Object();
CraftingGridCache.pauseRebuilds();
Deque<GridNode> nextRun = new ArrayDeque<>();
nextRun.add( this );
@@ -203,6 +206,7 @@ public class GridNode implements IGridNode, IPathItem
}
}
}
CraftingGridCache.unpauseRebuilds();
}
@Override
+28
View File
@@ -114,6 +114,8 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
private IStorageGrid storageGrid;
private IEnergyGrid energyGrid;
private boolean updateList = false;
private static int pauseRebuilds = 0;
private static Set<CraftingGridCache> rebuildNeeded = new HashSet<>();
public CraftingGridCache( final IGrid grid )
{
@@ -240,8 +242,34 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper
// 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()
{
// 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;
// erase list.
+39 -35
View File
@@ -47,6 +47,7 @@ public class TickManagerCache implements ITickManager
private final HashMap<IGridNode, TickTracker> sleeping = new HashMap<>();
private final HashMap<IGridNode, TickTracker> awake = new HashMap<>();
private final PriorityQueue<TickTracker> upcomingTicks = new PriorityQueue<>();
private long currentTick = 0;
public TickManagerCache( final IGrid g )
@@ -80,50 +81,52 @@ public class TickManagerCache implements ITickManager
public void onUpdateTick()
{
TickTracker tt = null;
try
{
this.currentTick++;
while( !this.upcomingTicks.isEmpty() )
{
tt = this.upcomingTicks.peek();
final int diff = (int) ( this.currentTick - tt.getLastTick() );
if( diff >= tt.getCurrentRate() )
// Stop once it reaches a TickTracker running at a later tick
if( tt.getNextTick() > this.currentTick )
{
// remove tt..
this.upcomingTicks.poll();
final TickRateModulation mod = tt.getGridTickable().tickingRequest( tt.getNode(), diff );
switch( mod )
{
case FASTER:
tt.setRate( tt.getCurrentRate() - 2 );
break;
case IDLE:
tt.setRate( tt.getRequest().maxTickRate );
break;
case SAME:
break;
case SLEEP:
this.sleepDevice( tt.getNode() );
break;
case SLOWER:
tt.setRate( tt.getCurrentRate() + 1 );
break;
case URGENT:
tt.setRate( 0 );
break;
default:
break;
}
if( this.awake.containsKey( tt.getNode() ) )
{
this.addToQueue( tt );
}
break;
}
else
this.upcomingTicks.poll();
final int diff = (int) ( this.currentTick - tt.getLastTick() );
final TickRateModulation mod = tt.getGridTickable().tickingRequest( tt.getNode(), diff );
switch( mod )
{
return; // done!
case FASTER:
tt.setCurrentRate( tt.getCurrentRate() - 2 );
break;
case IDLE:
tt.setCurrentRate( tt.getRequest().maxTickRate );
break;
case SAME:
break;
case SLEEP:
this.sleepDevice( tt.getNode() );
break;
case SLOWER:
tt.setCurrentRate( tt.getCurrentRate() + 1 );
break;
case URGENT:
tt.setCurrentRate( 0 );
break;
default:
break;
}
if( this.awake.containsKey( tt.getNode() ) )
{
this.addToQueue( tt );
}
}
}
@@ -255,6 +258,7 @@ public class TickManagerCache implements ITickManager
final TickTracker gt = this.sleeping.get( node );
this.sleeping.remove( node );
this.awake.put( node, gt );
this.upcomingTicks.remove( gt );
this.addToQueue( gt );
return true;
+22 -21
View File
@@ -37,7 +37,6 @@ public class TickTracker implements Comparable<TickTracker>
private final TickingRequest request;
private final IGridTickable gt;
private final IGridNode node;
private final TickManagerCache host;
private final long LastFiveTicksTime = 0;
@@ -51,7 +50,6 @@ public class TickTracker implements Comparable<TickTracker>
this.node = node;
this.setCurrentRate( ( req.minTickRate + req.maxTickRate ) / 2 );
this.setLastTick( currentTick );
this.host = tickManagerCache;
}
public long getAvgNanos()
@@ -59,27 +57,25 @@ public class TickTracker implements Comparable<TickTracker>
return( this.LastFiveTicksTime / 5 );
}
public void setRate( final int rate )
{
this.setCurrentRate( rate );
if( this.getCurrentRate() < this.getRequest().minTickRate )
{
this.setCurrentRate( this.getRequest().minTickRate );
}
if( this.getCurrentRate() > this.getRequest().maxTickRate )
{
this.setCurrentRate( this.getRequest().maxTickRate );
}
}
@Override
public int compareTo( @Nonnull final TickTracker t )
{
final int nextTick = (int) ( ( this.getLastTick() - this.host.getCurrentTick() ) + this.getCurrentRate() );
final int ts_nextTick = (int) ( ( t.getLastTick() - this.host.getCurrentTick() ) + t.getCurrentRate() );
return nextTick - ts_nextTick;
int next = Long.compare( this.getNextTick(), t.getNextTick() );
if( next != 0 )
{
return next;
}
int last = Long.compare( this.getLastTick(), t.getLastTick() );
if( last != 0 )
{
return last;
}
return Integer.compare( this.getCurrentRate(), t.getCurrentRate() );
}
public void addEntityCrashInfo( final CrashReportCategory crashreportcategory )
@@ -111,7 +107,12 @@ public class TickTracker implements Comparable<TickTracker>
public void setCurrentRate( final int currentRate )
{
this.currentRate = currentRate;
this.currentRate = Math.min( this.getRequest().maxTickRate, Math.max( this.getRequest().minTickRate, currentRate ) );
}
public long getNextTick()
{
return this.lastTick + this.currentRate;
}
public long getLastTick()