Skip to content

Commit

Permalink
Cache Format details once acquired, should fix performance where item…
Browse files Browse the repository at this point in the history
…s are constantly inspected. ( #116 )
  • Loading branch information
AlgorithmX2 committed Aug 5, 2016
1 parent c6351e5 commit b5fc9d9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
@@ -1,10 +1,7 @@
package mod.chiselsandbits.chiseledblock;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.zip.InflaterInputStream;

import io.netty.buffer.Unpooled;
import mod.chiselsandbits.chiseledblock.data.VoxelBlob;
import mod.chiselsandbits.chiseledblock.data.VoxelBlob.BlobStats;
import mod.chiselsandbits.chiseledblock.data.VoxelBlobStateReference;
Expand All @@ -15,7 +12,6 @@
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;

public class NBTBlobConverter
{
Expand Down Expand Up @@ -95,7 +91,7 @@ public NBTBlobConverter(
isNormalCube = tile.isNormalCube;
primaryBlockState = Block.getStateId( tile.getBlockState( Blocks.COBBLESTONE ) );
voxelBlobRef = tile.getBlobStateReference();
format = getFormat( voxelBlobRef );
format = voxelBlobRef == null ? -1 : voxelBlobRef.getFormat();
}

public void fillWith(
Expand All @@ -109,7 +105,7 @@ public void setBlob(
final VoxelBlob vb )
{
voxelBlobRef = new VoxelBlobStateReference( vb, 0 );
format = getFormat( voxelBlobRef );
format = voxelBlobRef.getFormat();
updateFromBlob();
}

Expand Down Expand Up @@ -170,37 +166,14 @@ public final void readChisleData(
}

voxelBlobRef = new VoxelBlobStateReference( v, 0 );
format = getFormat( voxelBlobRef );
format = voxelBlobRef.getFormat();

if ( tile != null )
{
tile.updateBlob( this, triggerUpdates );
}
}

private int getFormat(
final VoxelBlobStateReference v )
{
if ( v == null || v.getByteArray() == null || v.getByteArray().length == 0 )
{
return -1;
}

try
{
final InflaterInputStream arrayPeek = new InflaterInputStream( new ByteArrayInputStream( v.getByteArray() ) );
final byte[] peekBytes = new byte[5];
arrayPeek.read( peekBytes );

final PacketBuffer header = new PacketBuffer( Unpooled.wrappedBuffer( peekBytes ) );
return header.readVarIntFromBuffer();
}
catch ( final IOException e )
{
return 0;
}
}

public void updateFromBlob()
{
final VoxelBlob vb = getRef().getVoxelBlob();
Expand Down
@@ -1,15 +1,20 @@
package mod.chiselsandbits.chiseledblock.data;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.zip.InflaterInputStream;

import io.netty.buffer.Unpooled;
import mod.chiselsandbits.chiseledblock.BoxCollection;
import mod.chiselsandbits.chiseledblock.BoxType;
import mod.chiselsandbits.core.Log;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.math.AxisAlignedBB;

public final class VoxelBlobStateInstance implements Comparable<VoxelBlobStateInstance>
Expand Down Expand Up @@ -216,4 +221,36 @@ private AxisAlignedBB[] generateBoxes(

return cache.toArray( new AxisAlignedBB[cache.size()] );
}

// cache the format after reading it once.
private int format = Integer.MIN_VALUE;

public int getFormat()
{
if ( format == Integer.MIN_VALUE )
{
if ( voxelBytes.length == 0 )
{
format = -1;
}
else
{
try
{
final InflaterInputStream arrayPeek = new InflaterInputStream( new ByteArrayInputStream( voxelBytes ) );
final byte[] peekBytes = new byte[5];
arrayPeek.read( peekBytes );

final PacketBuffer header = new PacketBuffer( Unpooled.wrappedBuffer( peekBytes ) );
format = header.readVarIntFromBuffer();
}
catch ( final IOException e )
{
format = 0;
}
}
}

return format;
}
}
Expand Up @@ -168,4 +168,9 @@ public Collection<AxisAlignedBB> getBoxes(
return data.getBoxes( type );
}

public int getFormat()
{
return data.getFormat();
}

}

0 comments on commit b5fc9d9

Please sign in to comment.