Optimize PaletteStorage operations

This commit is contained in:
themode 2020-11-13 03:57:13 +01:00
parent 0ae656c1f2
commit 3c2c7acb0b

View File

@ -214,22 +214,11 @@ public class PaletteStorage {
return; return;
} }
x = toChunkCoordinate(x);
z = toChunkCoordinate(z);
final int section = ChunkUtils.getSectionAt(y); final int section = ChunkUtils.getSectionAt(y);
// Change to palette value
blockId = paletteStorage.getPaletteIndex(section, blockId);
final int sectionIndex = getSectionIndex(x, y % CHUNK_SECTION_SIZE, z);
final int valuesPerLong = paletteStorage.valuesPerLong; final int valuesPerLong = paletteStorage.valuesPerLong;
final int bitsPerEntry = paletteStorage.bitsPerEntry; final int bitsPerEntry = paletteStorage.bitsPerEntry;
final int index = sectionIndex / valuesPerLong;
final int bitIndex = (sectionIndex % valuesPerLong) * bitsPerEntry;
if (paletteStorage.sectionBlocks[section].length == 0) { if (paletteStorage.sectionBlocks[section].length == 0) {
if (blockId == 0) { if (blockId == 0) {
// Section is empty and method is trying to place an air block, stop unnecessary computation // Section is empty and method is trying to place an air block, stop unnecessary computation
@ -240,6 +229,17 @@ public class PaletteStorage {
paletteStorage.sectionBlocks[section] = new long[getSize(valuesPerLong)]; paletteStorage.sectionBlocks[section] = new long[getSize(valuesPerLong)];
} }
x = toChunkCoordinate(x);
z = toChunkCoordinate(z);
// Change to palette value
blockId = paletteStorage.getPaletteIndex(section, blockId);
final int sectionIndex = getSectionIndex(x, y, z);
final int index = sectionIndex / valuesPerLong;
final int bitIndex = (sectionIndex % valuesPerLong) * bitsPerEntry;
final long[] sectionBlock = paletteStorage.sectionBlocks[section]; final long[] sectionBlock = paletteStorage.sectionBlocks[section];
long block = sectionBlock[index]; long block = sectionBlock[index];
@ -259,10 +259,23 @@ public class PaletteStorage {
return 0; return 0;
} }
final int section = ChunkUtils.getSectionAt(y);
final long[] blocks;
// Retrieve the longs and check if the section is empty
{
blocks = paletteStorage.sectionBlocks[section];
if (blocks.length == 0) {
// Section is not loaded, can only be air
return 0;
}
}
x = toChunkCoordinate(x); x = toChunkCoordinate(x);
z = toChunkCoordinate(z); z = toChunkCoordinate(z);
final int sectionIndex = getSectionIndex(x, y % CHUNK_SECTION_SIZE, z); final int sectionIndex = getSectionIndex(x, y, z);
final int valuesPerLong = paletteStorage.valuesPerLong; final int valuesPerLong = paletteStorage.valuesPerLong;
final int bitsPerEntry = paletteStorage.bitsPerEntry; final int bitsPerEntry = paletteStorage.bitsPerEntry;
@ -270,15 +283,6 @@ public class PaletteStorage {
final int index = sectionIndex / valuesPerLong; final int index = sectionIndex / valuesPerLong;
final int bitIndex = sectionIndex % valuesPerLong * bitsPerEntry; final int bitIndex = sectionIndex % valuesPerLong * bitsPerEntry;
final int section = ChunkUtils.getSectionAt(y);
final long[] blocks = paletteStorage.sectionBlocks[section];
if (blocks.length == 0) {
// Section is not loaded, can only be air
return 0;
}
final long value = blocks[index] >> bitIndex & MAGIC_MASKS[bitsPerEntry]; final long value = blocks[index] >> bitIndex & MAGIC_MASKS[bitsPerEntry];
// Change to palette value and return // Change to palette value and return
@ -335,6 +339,7 @@ public class PaletteStorage {
* @return the section index of the position * @return the section index of the position
*/ */
public static int getSectionIndex(int x, int y, int z) { public static int getSectionIndex(int x, int y, int z) {
y %= CHUNK_SECTION_SIZE;
return y << 8 | z << 4 | x; return y << 8 | z << 4 | x;
} }