From 3c2c7acb0bb0f352b03b5ab07dffd5f07e2ad6d1 Mon Sep 17 00:00:00 2001 From: themode Date: Fri, 13 Nov 2020 03:57:13 +0100 Subject: [PATCH] Optimize PaletteStorage operations --- .../instance/palette/PaletteStorage.java | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/palette/PaletteStorage.java b/src/main/java/net/minestom/server/instance/palette/PaletteStorage.java index d5ecaae45..6557448d2 100644 --- a/src/main/java/net/minestom/server/instance/palette/PaletteStorage.java +++ b/src/main/java/net/minestom/server/instance/palette/PaletteStorage.java @@ -214,22 +214,11 @@ public class PaletteStorage { return; } - x = toChunkCoordinate(x); - z = toChunkCoordinate(z); - 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 bitsPerEntry = paletteStorage.bitsPerEntry; - final int index = sectionIndex / valuesPerLong; - final int bitIndex = (sectionIndex % valuesPerLong) * bitsPerEntry; - if (paletteStorage.sectionBlocks[section].length == 0) { if (blockId == 0) { // 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)]; } + 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]; long block = sectionBlock[index]; @@ -259,10 +259,23 @@ public class PaletteStorage { 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); 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 bitsPerEntry = paletteStorage.bitsPerEntry; @@ -270,15 +283,6 @@ public class PaletteStorage { final int index = sectionIndex / valuesPerLong; 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]; // Change to palette value and return @@ -335,6 +339,7 @@ public class PaletteStorage { * @return the section index of the position */ public static int getSectionIndex(int x, int y, int z) { + y %= CHUNK_SECTION_SIZE; return y << 8 | z << 4 | x; }