Performance improvements for saveChanges() and ICellInventory (#3586)

API break!
Fixes #3553
This commit is contained in:
fscan
2018-07-14 16:53:49 +02:00
committed by GitHub
parent 71a9bb2532
commit e809c688df
37 changed files with 292 additions and 166 deletions
+18 -1
View File
@@ -52,6 +52,7 @@ import appeng.core.AELog;
import appeng.core.features.IStackSrc;
import appeng.helpers.ICustomNameObject;
import appeng.helpers.IPriorityHost;
import appeng.hooks.TickHandler;
import appeng.tile.inventory.AppEngInternalAEInventory;
import appeng.util.Platform;
import appeng.util.SettingsFrom;
@@ -68,6 +69,7 @@ public class AEBaseTile extends TileEntity implements IOrientable, ICommonTile,
private EnumFacing forward = null;
private EnumFacing up = null;
private IBlockState state;
private boolean markDirtyQueued = false;
@Override
public boolean shouldRefresh( final World world, final BlockPos pos, final IBlockState oldState, final IBlockState newSate )
@@ -488,7 +490,22 @@ public class AEBaseTile extends TileEntity implements IOrientable, ICommonTile,
public void saveChanges()
{
markDirty();
if( this.world != null )
{
this.world.markChunkDirty( this.pos, this );
if( !this.markDirtyQueued )
{
TickHandler.INSTANCE.addCallable( null, this::markDirtyAtEndOfTick );
this.markDirtyQueued = true;
}
}
}
private Object markDirtyAtEndOfTick( final World w )
{
this.markDirty();
this.markDirtyQueued = false;
return null;
}
public boolean requiresTESR()
@@ -159,7 +159,7 @@ public class TileCraftingMonitorTile extends TileCraftingTile implements IColora
}
this.paintedColor = newPaintedColor;
this.markDirty();
this.saveChanges();
this.markForUpdate();
return true;
}
@@ -133,7 +133,7 @@ public class TileMolecularAssembler extends AENetworkInvTile implements IUpgrade
}
this.updateSleepiness();
this.markDirty();
this.saveChanges();
return true;
}
}
@@ -398,7 +398,7 @@ public class TileMolecularAssembler extends AENetworkInvTile implements IUpgrade
// did it eject?
if( this.gridInv.getStackInSlot( 9 ).isEmpty() )
{
this.markDirty();
this.saveChanges();
}
this.ejectHeldItems();
@@ -487,7 +487,7 @@ public class TileMolecularAssembler extends AENetworkInvTile implements IUpgrade
// ;P
}
this.markDirty();
this.saveChanges();
this.updateSleepiness();
return this.isAwake ? TickRateModulation.IDLE : TickRateModulation.SLEEP;
}
@@ -509,7 +509,7 @@ public class TileMolecularAssembler extends AENetworkInvTile implements IUpgrade
{
this.gridInv.setStackInSlot( 9, is );
this.gridInv.setStackInSlot( x, ItemStack.EMPTY );
this.markDirty();
this.saveChanges();
return;
}
}
@@ -580,7 +580,7 @@ public class TileMolecularAssembler extends AENetworkInvTile implements IUpgrade
if( size != newSize )
{
this.markDirty();
this.saveChanges();
}
return output;
@@ -0,0 +1,119 @@
package appeng.tile.inventory;
import net.minecraft.item.ItemStack;
import appeng.api.storage.ICellInventoryHandler;
import appeng.util.inv.IAEAppEngInventory;
import appeng.util.inv.IInternalItemHandler;
import appeng.util.inv.filter.IAEItemFilter;
public class AppEngCellInventory implements IInternalItemHandler
{
private final AppEngInternalInventory inv;
private final ICellInventoryHandler handlerForSlot[];
public AppEngCellInventory( final IAEAppEngInventory host, final int slots )
{
this.inv = new AppEngInternalInventory( host, slots, 1 );
this.handlerForSlot = new ICellInventoryHandler[slots];
}
public void setHandler( final int slot, final ICellInventoryHandler handler )
{
this.handlerForSlot[slot] = handler;
}
public void setFilter( IAEItemFilter filter )
{
this.inv.setFilter( filter );
}
@Override
public void setStackInSlot( int slot, ItemStack stack )
{
this.persist( slot );
this.inv.setStackInSlot( slot, stack );
this.cleanup( slot );
}
@Override
public int getSlots()
{
return this.inv.getSlots();
}
@Override
public ItemStack getStackInSlot( int slot )
{
this.persist( slot );
return this.inv.getStackInSlot( slot );
}
@Override
public ItemStack insertItem( int slot, ItemStack stack, boolean simulate )
{
this.persist( slot );
final ItemStack ret = inv.insertItem( slot, stack, simulate );
this.cleanup( slot );
return ret;
}
@Override
public ItemStack extractItem( int slot, int amount, boolean simulate )
{
this.persist( slot );
final ItemStack ret = inv.extractItem( slot, amount, simulate );
this.cleanup( slot );
return ret;
}
@Override
public int getSlotLimit( int slot )
{
return inv.getSlotLimit( slot );
}
@Override
public boolean isItemValidForSlot( int slot, ItemStack stack )
{
return inv.isItemValidForSlot( slot, stack );
}
@Override
public void markDirty( int slot )
{
this.persist( slot );
this.inv.markDirty( slot );
this.cleanup( slot );
}
public void persist()
{
for( int i = 0; i < this.getSlots(); ++i )
{
this.persist( i );
}
}
private void persist( int slot )
{
if( this.handlerForSlot[slot] != null )
{
this.handlerForSlot[slot].getCellInv().persist();
}
}
private void cleanup( int slot )
{
if( this.handlerForSlot[slot] != null )
{
if( this.handlerForSlot[slot].getCellInv().getItemStack() != this.inv.getStackInSlot( slot ) )
{
this.handlerForSlot[slot] = null;
}
}
}
}
@@ -185,7 +185,7 @@ public class TileCellWorkbench extends AEBaseTile implements IUpgradeableHost, I
this.config.setStackInSlot( x, ItemStack.EMPTY );
}
this.markDirty();
this.saveChanges();
}
this.locked = false;
@@ -380,7 +380,7 @@ public class TileInscriber extends AENetworkPowerTile implements IGridTickable,
this.sideItemHandler.setStackInSlot( 0, ItemStack.EMPTY );
}
}
this.markDirty();
this.saveChanges();
}
else if( this.finalStep == 16 )
{
@@ -130,7 +130,7 @@ public class TileInterface extends AENetworkInvTile implements IGridTickable, II
this.configureNodeSides();
this.markForUpdate();
this.markDirty();
this.saveChanges();
}
private void configureNodeSides()
@@ -145,12 +145,6 @@ public class TileInterface extends AENetworkInvTile implements IGridTickable, II
}
}
@Override
public void markDirty()
{
this.duality.markDirty();
}
@Override
public void getDrops( final World w, final BlockPos pos, final List<ItemStack> drops )
{
@@ -191,7 +191,7 @@ public class TilePaint extends AEBaseTile
}
this.markForUpdate();
this.markDirty();
this.saveChanges();
}
private void updateData()
@@ -265,7 +265,7 @@ public class TilePaint extends AEBaseTile
this.maxLit();
this.markForUpdate();
this.markDirty();
this.saveChanges();
}
}
@@ -363,7 +363,7 @@ public class TileSecurityStation extends AENetworkTile implements ITerminalHost,
}
this.paintedColor = newPaintedColor;
this.markDirty();
this.saveChanges();
this.markForUpdate();
return true;
}
@@ -261,7 +261,7 @@ public class TileVibrationChamber extends AENetworkInvTile implements IGridTicka
this.inv.setStackInSlot( 0, is );
}
this.markDirty();
this.saveChanges();
}
}
@@ -312,7 +312,7 @@ public class TileCableBus extends AEBaseTile implements AEMultiTile, ICustomColl
@Override
public void markForSave()
{
super.markDirty();
this.saveChanges();
}
@Override
@@ -163,9 +163,10 @@ public class TileWireless extends AENetworkInvTile implements IWirelessAccessPoi
}
@Override
public void markDirty()
public void saveChanges()
{
this.updatePower();
super.saveChanges();
}
@Override
@@ -69,6 +69,8 @@ import appeng.api.networking.security.ISecurityGrid;
import appeng.api.networking.storage.IBaseMonitor;
import appeng.api.networking.storage.IStorageGrid;
import appeng.api.storage.ICellHandler;
import appeng.api.storage.ICellInventory;
import appeng.api.storage.ICellInventoryHandler;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.IMEInventoryHandler;
import appeng.api.storage.IMEMonitor;
@@ -121,7 +123,6 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, ITerminal
private boolean wasActive = false;
private AEColor paintedColor = AEColor.TRANSPARENT;
private boolean isCached = false;
private ICellHandler cellHandler;
private MEMonitorHandler<IAEItemStack> itemCell;
private MEMonitorHandler<IAEFluidStack> fluidCell;
private Accessor accessor;
@@ -226,23 +227,23 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, ITerminal
if( !is.isEmpty() )
{
this.isCached = true;
this.cellHandler = AEApi.instance().registries().cell().getHandler( is );
if( this.cellHandler != null )
ICellHandler cellHandler = AEApi.instance().registries().cell().getHandler( is );
if( cellHandler != null )
{
double power = 1.0;
final IMEInventoryHandler<IAEItemStack> itemCell = this.cellHandler.getCellInventory( is, this,
final ICellInventoryHandler<IAEItemStack> itemCell = cellHandler.getCellInventory( is, this,
AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) );
final IMEInventoryHandler<IAEFluidStack> fluidCell = this.cellHandler.getCellInventory( is, this,
final ICellInventoryHandler<IAEFluidStack> fluidCell = cellHandler.getCellInventory( is, this,
AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ) );
if( itemCell != null )
{
power += this.cellHandler.cellIdleDrain( is, itemCell );
power += cellHandler.cellIdleDrain( is, itemCell );
}
else if( fluidCell != null )
{
power += this.cellHandler.cellIdleDrain( is, fluidCell );
power += cellHandler.cellIdleDrain( is, fluidCell );
}
this.getProxy().setIdlePowerUsage( power );
@@ -766,15 +767,16 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, ITerminal
}
this.paintedColor = newPaintedColor;
this.markDirty();
this.saveChanges();
this.markForUpdate();
return true;
}
@Override
public void saveChanges( final IMEInventory cellInventory )
public void saveChanges( final ICellInventory<?> cellInventory )
{
this.markDirty();
cellInventory.persist();
this.world.markChunkDirty( this.pos, this );
}
private static class ChestNoHandler extends Exception
@@ -43,7 +43,8 @@ import appeng.api.networking.events.MENetworkPowerStatusChange;
import appeng.api.networking.security.IActionSource;
import appeng.api.networking.storage.IStorageGrid;
import appeng.api.storage.ICellHandler;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.ICellInventory;
import appeng.api.storage.ICellInventoryHandler;
import appeng.api.storage.IMEInventoryHandler;
import appeng.api.storage.IStorageChannel;
import appeng.api.storage.data.IAEItemStack;
@@ -56,9 +57,8 @@ import appeng.helpers.IPriorityHost;
import appeng.me.GridAccessException;
import appeng.me.helpers.MachineSource;
import appeng.me.storage.DriveWatcher;
import appeng.me.storage.MEInventoryHandler;
import appeng.tile.grid.AENetworkInvTile;
import appeng.tile.inventory.AppEngInternalInventory;
import appeng.tile.inventory.AppEngCellInventory;
import appeng.util.Platform;
import appeng.util.inv.InvOperation;
import appeng.util.inv.filter.IAEItemFilter;
@@ -71,7 +71,7 @@ public class TileDrive extends AENetworkInvTile implements IChestOrDrive, IPrior
private static final int BIT_BLINK_MASK = 0x24924924;
private static final int BIT_STATE_MASK = 0xDB6DB6DB;
private final AppEngInternalInventory inv = new AppEngInternalInventory( this, 10, 1 );
private final AppEngCellInventory inv = new AppEngCellInventory( this, 10 );
private final ICellHandler[] handlersBySlot = new ICellHandler[10];
private final DriveWatcher<IAEItemStack>[] invBySlot = new DriveWatcher[10];
private final IActionSource mySrc;
@@ -145,21 +145,13 @@ public class TileDrive extends AENetworkInvTile implements IChestOrDrive, IPrior
return ( this.state >> ( slot * 3 ) ) & 3;
}
final ItemStack cell = this.inv.getStackInSlot( 2 );
final ICellHandler ch = this.handlersBySlot[slot];
final MEInventoryHandler handler = this.invBySlot[slot];
final DriveWatcher handler = this.invBySlot[slot];
if( handler == null )
{
return 0;
}
if( ch != null )
{
return ch.getStatusForCell( cell, handler.getInternal() );
}
return 0;
return handler.getStatus();
}
@Override
@@ -307,10 +299,11 @@ public class TileDrive extends AENetworkInvTile implements IChestOrDrive, IPrior
for( IStorageChannel<? extends IAEStack<?>> channel : storageChannels )
{
IMEInventoryHandler cell = this.handlersBySlot[x].getCellInventory( is, this, channel );
ICellInventoryHandler cell = this.handlersBySlot[x].getCellInventory( is, this, channel );
if( cell != null )
{
this.inv.setHandler( x, cell );
power += this.handlersBySlot[x].cellIdleDrain( is, cell );
final DriveWatcher<IAEItemStack> ih = new DriveWatcher( cell, is, this.handlersBySlot[x], this );
@@ -360,7 +353,7 @@ public class TileDrive extends AENetworkInvTile implements IChestOrDrive, IPrior
public void setPriority( final int newValue )
{
this.priority = newValue;
this.markDirty();
this.saveChanges();
this.isCached = false; // recalculate the storage cell.
this.updateState();
@@ -384,7 +377,7 @@ public class TileDrive extends AENetworkInvTile implements IChestOrDrive, IPrior
}
@Override
public void saveChanges( final IMEInventory cellInventory )
public void saveChanges( final ICellInventory<?> cellInventory )
{
this.world.markChunkDirty( this.pos, this );
}