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
@@ -206,7 +206,7 @@ public final class CraftingCPUCluster implements IAECluster, ICraftingCPU
}
te.setCoreBlock( false );
te.markDirty();
te.saveChanges();
this.tiles.add( 0, te );
if( te.isStorage() )
@@ -395,7 +395,7 @@ public final class CraftingCPUCluster implements IAECluster, ICraftingCPU
private void markDirty()
{
this.getCore().markDirty();
this.getCore().saveChanges();
}
private void postCraftingStatusChange( final IAEItemStack diff )
@@ -42,7 +42,7 @@ import appeng.util.Platform;
*/
public abstract class AbstractCellInventory<T extends IAEStack<T>> implements ICellInventory<T>
{
private static final int MAX_ITEM_TYPES = 63;
private static final String ITEM_TYPE_TAG = "it";
private static final String ITEM_COUNT_TAG = "ic";
private static final String ITEM_SLOT = "#";
@@ -51,46 +51,37 @@ public abstract class AbstractCellInventory<T extends IAEStack<T>> implements IC
protected static final String ITEM_PRE_FORMATTED_SLOT = "PF#";
protected static final String ITEM_PRE_FORMATTED_NAME = "PN";
protected static final String ITEM_PRE_FORMATTED_FUZZY = "FP";
private static String[] itemSlots;
private static String[] itemSlotCount;
private static final String[] ITEM_SLOT_KEYS = new String[MAX_ITEM_TYPES];
private static final String[] ITEM_SLOT_COUNT_KEYS = new String[MAX_ITEM_TYPES];
private final NBTTagCompound tagCompound;
protected final ISaveProvider container;
private int maxItemTypes = 63;
private int maxItemTypes = MAX_ITEM_TYPES;
private short storedItems = 0;
private int storedItemCount = 0;
protected IItemList<T> cellItems;
protected ItemStack i;
protected IStorageCell<T> cellType;
protected final ItemStack i;
protected final IStorageCell<T> cellType;
protected final int itemsPerByte;
private boolean isPersisted = true;
protected AbstractCellInventory( final NBTTagCompound data, final ISaveProvider container, final int itemsPerByte )
static
{
this.tagCompound = data;
this.container = container;
this.itemsPerByte = itemsPerByte;
for( int x = 0; x < MAX_ITEM_TYPES; x++ )
{
ITEM_SLOT_KEYS[x] = ITEM_SLOT + x;
ITEM_SLOT_COUNT_KEYS[x] = ITEM_SLOT_COUNT + x;
}
}
protected AbstractCellInventory( final ItemStack o, final ISaveProvider container, final int itemsPerByte ) throws AppEngException
{
this.itemsPerByte = itemsPerByte;
if( itemSlots == null )
{
itemSlots = new String[this.maxItemTypes];
itemSlotCount = new String[this.maxItemTypes];
for( int x = 0; x < this.maxItemTypes; x++ )
{
itemSlots[x] = ITEM_SLOT + x;
itemSlotCount[x] = ITEM_SLOT_COUNT + x;
}
}
if( o == null )
{
throw new AppEngException( "ItemStack was used as a cell, but was not a cell!" );
}
this.cellType = null;
this.i = o;
final Item type = this.i.getItem();
@@ -99,8 +90,7 @@ public abstract class AbstractCellInventory<T extends IAEStack<T>> implements IC
this.cellType = (IStorageCell<T>) this.i.getItem();
this.maxItemTypes = this.cellType.getTotalTypes( this.i );
}
if( this.cellType == null )
else
{
throw new AppEngException( "ItemStack was used as a cell, but was not a cell!" );
}
@@ -110,9 +100,9 @@ public abstract class AbstractCellInventory<T extends IAEStack<T>> implements IC
throw new AppEngException( "ItemStack was used as a cell, but was not a cell!" );
}
if( this.maxItemTypes > 63 )
if( this.maxItemTypes > MAX_ITEM_TYPES )
{
this.maxItemTypes = 63;
this.maxItemTypes = MAX_ITEM_TYPES;
}
if( this.maxItemTypes < 1 )
{
@@ -142,14 +132,14 @@ public abstract class AbstractCellInventory<T extends IAEStack<T>> implements IC
return this.cellItems;
}
protected void updateItemCount( final long delta )
@Override
public void persist()
{
this.storedItemCount += delta;
this.tagCompound.setInteger( ITEM_COUNT_TAG, this.storedItemCount );
}
if( this.isPersisted )
{
return;
}
protected void saveChanges()
{
int itemCount = 0;
// add new pretty stuff...
@@ -160,9 +150,8 @@ public abstract class AbstractCellInventory<T extends IAEStack<T>> implements IC
final NBTTagCompound g = new NBTTagCompound();
v.writeToNBT( g );
this.tagCompound.setTag( itemSlots[x], g );
this.tagCompound.setInteger( itemSlotCount[x], (int) v.getStackSize() );
this.tagCompound.setTag( ITEM_SLOT_KEYS[x], g );
this.tagCompound.setInteger( ITEM_SLOT_COUNT_KEYS[x], (int) v.getStackSize() );
x++;
}
@@ -192,14 +181,33 @@ public abstract class AbstractCellInventory<T extends IAEStack<T>> implements IC
// clean any old crusty stuff...
for( ; x < oldStoredItems && x < this.maxItemTypes; x++ )
{
this.tagCompound.removeTag( itemSlots[x] );
this.tagCompound.removeTag( itemSlotCount[x] );
this.tagCompound.removeTag( ITEM_SLOT_KEYS[x] );
this.tagCompound.removeTag( ITEM_SLOT_COUNT_KEYS[x] );
}
this.isPersisted = true;
}
protected void saveChanges()
{
// recalculate values
this.storedItems = (short) this.cellItems.size();
this.storedItemCount = 0;
for( final T v : this.cellItems )
{
this.storedItemCount += v.getStackSize();
}
this.isPersisted = false;
if( this.container != null )
{
this.container.saveChanges( this );
}
else
{
// if there is no ISaveProvider, store to NBT immediately
this.persist();
}
}
private void loadCellItems()
@@ -215,8 +223,8 @@ public abstract class AbstractCellInventory<T extends IAEStack<T>> implements IC
for( int slot = 0; slot < types; slot++ )
{
NBTTagCompound compoundTag = this.tagCompound.getCompoundTag( itemSlots[slot] );
int stackSize = this.tagCompound.getInteger( itemSlotCount[slot] );
NBTTagCompound compoundTag = this.tagCompound.getCompoundTag( ITEM_SLOT_KEYS[slot] );
int stackSize = this.tagCompound.getInteger( ITEM_SLOT_COUNT_KEYS[slot] );
this.loadCellItem( compoundTag, stackSize );
}
}
@@ -25,6 +25,7 @@ import appeng.api.AEApi;
import appeng.api.config.AccessRestriction;
import appeng.api.config.Actionable;
import appeng.api.networking.security.IActionSource;
import appeng.api.storage.ICellInventoryHandler;
import appeng.api.storage.IMEInventoryHandler;
import appeng.api.storage.IStorageChannel;
import appeng.api.storage.channels.IItemStorageChannel;
@@ -53,7 +54,7 @@ public class CreativeCellInventory implements IMEInventoryHandler<IAEItemStack>
}
}
public static IMEInventoryHandler getCell( final ItemStack o )
public static ICellInventoryHandler getCell( final ItemStack o )
{
return new ItemCellInventoryHandler( new CreativeCellInventory( o ) );
}
@@ -45,6 +45,11 @@ public class DriveWatcher<T extends IAEStack<T>> extends MEInventoryHandler<T>
this.cord = cod;
}
public int getStatus()
{
return this.handler.getStatusForCell( this.is, this.getInternal() );
}
@Override
public T injectItems( final T input, final Actionable type, final IActionSource src )
{
@@ -54,7 +59,7 @@ public class DriveWatcher<T extends IAEStack<T>> extends MEInventoryHandler<T>
if( type == Actionable.MODULATE && ( a == null || a.getStackSize() != size ) )
{
final int newStatus = this.handler.getStatusForCell( this.is, this.getInternal() );
final int newStatus = this.getStatus();
if( newStatus != this.oldStatus )
{
@@ -73,7 +78,7 @@ public class DriveWatcher<T extends IAEStack<T>> extends MEInventoryHandler<T>
if( type == Actionable.MODULATE && a != null )
{
final int newStatus = this.handler.getStatusForCell( this.is, this.getInternal() );
final int newStatus = this.getStatus();
if( newStatus != this.oldStatus )
{
@@ -28,8 +28,8 @@ import appeng.api.config.Actionable;
import appeng.api.exceptions.AppEngException;
import appeng.api.implementations.items.IStorageCell;
import appeng.api.networking.security.IActionSource;
import appeng.api.storage.ICellInventoryHandler;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.IMEInventoryHandler;
import appeng.api.storage.ISaveProvider;
import appeng.api.storage.IStorageChannel;
import appeng.api.storage.channels.IItemStorageChannel;
@@ -40,17 +40,12 @@ import appeng.core.AELog;
public class ItemCellInventory extends AbstractCellInventory<IAEItemStack>
{
protected ItemCellInventory( final NBTTagCompound data, final ISaveProvider container )
{
super( data, container, 8 );
}
private ItemCellInventory( final ItemStack o, final ISaveProvider container ) throws AppEngException
{
super( o, container, 8 );
}
public static IMEInventoryHandler getCell( final ItemStack o, final ISaveProvider container2 )
public static ICellInventoryHandler<IAEItemStack> getCell( final ItemStack o, final ISaveProvider container2 )
{
try
{
@@ -95,7 +90,7 @@ public class ItemCellInventory extends AbstractCellInventory<IAEItemStack>
final Item type = i.getItem();
if( type instanceof IStorageCell )
{
if ( ( (IStorageCell) type ).getChannel() == AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) )
if( ( (IStorageCell) type ).getChannel() == AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) )
{
return ( (IStorageCell) type ).isStorageCell( i );
}
@@ -148,7 +143,6 @@ public class ItemCellInventory extends AbstractCellInventory<IAEItemStack>
if( mode == Actionable.MODULATE )
{
l.setStackSize( l.getStackSize() + remainingItemCount );
this.updateItemCount( remainingItemCount );
this.saveChanges();
}
return r;
@@ -158,7 +152,6 @@ public class ItemCellInventory extends AbstractCellInventory<IAEItemStack>
if( mode == Actionable.MODULATE )
{
l.setStackSize( l.getStackSize() + input.getStackSize() );
this.updateItemCount( input.getStackSize() );
this.saveChanges();
}
return null;
@@ -180,8 +173,6 @@ public class ItemCellInventory extends AbstractCellInventory<IAEItemStack>
toWrite.setStackSize( remainingItemCount );
this.cellItems.add( toWrite );
this.updateItemCount( toWrite.getStackSize() );
this.saveChanges();
}
return toReturn;
@@ -189,7 +180,6 @@ public class ItemCellInventory extends AbstractCellInventory<IAEItemStack>
if( mode == Actionable.MODULATE )
{
this.updateItemCount( input.getStackSize() );
this.cellItems.add( input );
this.saveChanges();
}
@@ -223,7 +213,6 @@ public class ItemCellInventory extends AbstractCellInventory<IAEItemStack>
Results.setStackSize( l.getStackSize() );
if( mode == Actionable.MODULATE )
{
this.updateItemCount( -l.getStackSize() );
l.setStackSize( 0 );
this.saveChanges();
}
@@ -234,7 +223,6 @@ public class ItemCellInventory extends AbstractCellInventory<IAEItemStack>
if( mode == Actionable.MODULATE )
{
l.setStackSize( l.getStackSize() - size );
this.updateItemCount( -size );
this.saveChanges();
}
}