Minor ChunkWrapper.getBlockState() GC optimization

This commit is contained in:
James Seibel
2023-09-10 17:10:01 -05:00
parent 56172c69a4
commit ef6fc07cd3
2 changed files with 25 additions and 3 deletions
@@ -76,6 +76,9 @@ public class ChunkWrapper implements IChunkWrapper
private final byte[] blockLightArray;
private final byte[] skyLightArray;
/** cache so we don't have to hit the vanilla chunk as often. */
private final IBlockStateWrapper[] blockStateWrappers;
private ArrayList<DhBlockPos> blockLightPosList = null;
private boolean useDhLighting;
@@ -115,6 +118,8 @@ public class ChunkWrapper implements IChunkWrapper
this.blockLightArray = new byte[LodUtil.CHUNK_WIDTH * LodUtil.CHUNK_WIDTH * (this.getHeight() + 1)];
this.skyLightArray = new byte[LodUtil.CHUNK_WIDTH * LodUtil.CHUNK_WIDTH * (this.getHeight() + 1)];
this.blockStateWrappers = new IBlockStateWrapper[LodUtil.CHUNK_WIDTH * LodUtil.CHUNK_WIDTH * (this.getHeight() + 1)];
chunksNeedingClientLightUpdating.add(this);
}
@@ -314,7 +319,7 @@ public class ChunkWrapper implements IChunkWrapper
}
@Override
public List<DhBlockPos> getBlockLightPosList()
public ArrayList<DhBlockPos> getBlockLightPosList()
{
// only populate the list once
if (this.blockLightPosList == null)
@@ -372,7 +377,21 @@ public class ChunkWrapper implements IChunkWrapper
@Override
public IBlockStateWrapper getBlockState(int relX, int relY, int relZ)
{
return BlockStateWrapper.fromBlockState(this.chunk.getBlockState(new BlockPos(relX, relY, relZ)), this.wrappedLevel);
this.throwIndexOutOfBoundsIfRelativePosOutsideChunkBounds(relX, relY, relZ);
int index = this.relativeBlockPosToIndex(relX, relY, relZ);
IBlockStateWrapper cachedBlockState = this.blockStateWrappers[index];
if (cachedBlockState != null)
{
return cachedBlockState;
}
else
{
// the wrapper is cached to prevent having to create a bunch of new BlockPos and hitting the vanilla chunk object
IBlockStateWrapper blockState = BlockStateWrapper.fromBlockState(this.chunk.getBlockState(new BlockPos(relX, relY, relZ)), this.wrappedLevel);
this.blockStateWrappers[index] = blockState;
return blockState;
}
}
@Override
@@ -442,7 +461,10 @@ public class ChunkWrapper implements IChunkWrapper
private void throwIndexOutOfBoundsIfRelativePosOutsideChunkBounds(int x, int y, int z) throws IndexOutOfBoundsException
{
if (RUN_RELATIVE_POS_INDEX_VALIDATION)
{
return;
}
// FIXME +1 is to handle the fact that LodDataBuilder adds +1 to all block lighting calculations, also done in the constructor
int minHeight = this.getMinBuildHeight();