Chunk state lookup performance in 1.11.2

This commit is contained in:
Mike Primm 2018-09-06 22:11:34 -05:00
parent 54163396fb
commit 9e8850c633
2 changed files with 133 additions and 94 deletions

View File

@ -1,5 +1,7 @@
package org.dynmap.forge_1_11_2; package org.dynmap.forge_1_11_2;
import java.util.Arrays;
import org.dynmap.Log; import org.dynmap.Log;
import org.dynmap.renderer.DynmapBlockState; import org.dynmap.renderer.DynmapBlockState;
@ -12,11 +14,15 @@ import net.minecraft.nbt.NBTTagList;
*/ */
public class ChunkSnapshot public class ChunkSnapshot
{ {
private static interface Section {
public DynmapBlockState getBlockType(int x, int y, int z);
public int getBlockSkyLight(int x, int y, int z);
public int getBlockEmittedLight(int x, int y, int z);
public boolean isEmpty();
}
private final int x, z; private final int x, z;
private final int[][] blockidx; /* Block state index, by section */ private final Section[] section;
private final byte[][] skylight;
private final byte[][] emitlight;
private final boolean[] empty;
private final int[] hmap; // Height map private final int[] hmap; // Height map
private final int[] biome; private final int[] biome;
private final long captureFulltime; private final long captureFulltime;
@ -25,18 +31,66 @@ public class ChunkSnapshot
private static final int BLOCKS_PER_SECTION = 16 * 16 * 16; private static final int BLOCKS_PER_SECTION = 16 * 16 * 16;
private static final int COLUMNS_PER_CHUNK = 16 * 16; private static final int COLUMNS_PER_CHUNK = 16 * 16;
private static final int[] emptyIdx = new int[BLOCKS_PER_SECTION];
private static final byte[] emptyData = new byte[BLOCKS_PER_SECTION / 2]; private static final byte[] emptyData = new byte[BLOCKS_PER_SECTION / 2];
private static final byte[] fullData = new byte[BLOCKS_PER_SECTION / 2]; private static final byte[] fullData = new byte[BLOCKS_PER_SECTION / 2];
static static
{ {
for (int i = 0; i < fullData.length; i++) Arrays.fill(fullData, (byte)0xFF);
{ }
fullData[i] = (byte)0xFF;
private static class EmptySection implements Section {
@Override
public DynmapBlockState getBlockType(int x, int y, int z) {
return DynmapBlockState.AIR;
}
@Override
public int getBlockSkyLight(int x, int y, int z) {
return 15;
}
@Override
public int getBlockEmittedLight(int x, int y, int z) {
return 0;
}
@Override
public boolean isEmpty() {
return true;
} }
} }
private static final EmptySection empty_section = new EmptySection();
private static class StdSection implements Section {
DynmapBlockState[] states;
byte[] skylight;
byte[] emitlight;
public StdSection() {
states = new DynmapBlockState[BLOCKS_PER_SECTION];
Arrays.fill(states, DynmapBlockState.AIR);
skylight = emptyData;
emitlight = emptyData;
}
@Override
public DynmapBlockState getBlockType(int x, int y, int z) {
return states[((y & 0xF) << 8) | (z << 4) | x];
}
@Override
public int getBlockSkyLight(int x, int y, int z) {
int off = ((y & 0xF) << 7) | (z << 3) | (x >> 1);
return (skylight[off] >> (4 * (x & 1))) & 0xF;
}
@Override
public int getBlockEmittedLight(int x, int y, int z)
{
int off = ((y & 0xF) << 7) | (z << 3) | (x >> 1);
return (emitlight[off] >> (4 * (x & 1))) & 0xF;
}
@Override
public boolean isEmpty() {
return false;
}
}
/** /**
* Construct empty chunk snapshot * Construct empty chunk snapshot
* *
@ -51,18 +105,11 @@ public class ChunkSnapshot
this.biome = new int[COLUMNS_PER_CHUNK]; this.biome = new int[COLUMNS_PER_CHUNK];
this.sectionCnt = worldheight / 16; this.sectionCnt = worldheight / 16;
/* Allocate arrays indexed by section */ /* Allocate arrays indexed by section */
this.blockidx = new int[this.sectionCnt][]; this.section = new Section[this.sectionCnt];
this.skylight = new byte[this.sectionCnt][];
this.emitlight = new byte[this.sectionCnt][];
this.empty = new boolean[this.sectionCnt];
/* Fill with empty data */ /* Fill with empty data */
for (int i = 0; i < this.sectionCnt; i++) for (int i = 0; i < this.sectionCnt; i++) {
{ this.section[i] = empty_section;
this.empty[i] = true;
this.blockidx[i] = emptyIdx;
this.emitlight[i] = emptyData;
this.skylight[i] = fullData;
} }
/* Create empty height map */ /* Create empty height map */
@ -84,16 +131,10 @@ public class ChunkSnapshot
this.inhabitedTicks = 0; this.inhabitedTicks = 0;
} }
/* Allocate arrays indexed by section */ /* Allocate arrays indexed by section */
this.blockidx = new int[this.sectionCnt][]; this.section = new Section[this.sectionCnt];
this.skylight = new byte[this.sectionCnt][];
this.emitlight = new byte[this.sectionCnt][];
this.empty = new boolean[this.sectionCnt];
/* Fill with empty data */ /* Fill with empty data */
for (int i = 0; i < this.sectionCnt; i++) { for (int i = 0; i < this.sectionCnt; i++) {
this.empty[i] = true; this.section[i] = empty_section;
this.blockidx[i] = emptyIdx;
this.emitlight[i] = emptyData;
this.skylight[i] = fullData;
} }
/* Get sections */ /* Get sections */
NBTTagList sect = nbt.getTagList("Sections", 10); NBTTagList sect = nbt.getTagList("Sections", 10);
@ -104,8 +145,10 @@ public class ChunkSnapshot
Log.info("Section " + (int) secnum + " above world height " + worldheight); Log.info("Section " + (int) secnum + " above world height " + worldheight);
continue; continue;
} }
int[] blkidxs = new int[BLOCKS_PER_SECTION]; // Create normal section to initialize
this.blockidx[secnum] = blkidxs; StdSection cursect = new StdSection();
this.section[secnum] = cursect;
DynmapBlockState[] states = cursect.states;
// JEI format // JEI format
if (sec.hasKey("Palette", 11)) { if (sec.hasKey("Palette", 11)) {
int[] p = sec.getIntArray("Palette"); int[] p = sec.getIntArray("Palette");
@ -123,59 +166,73 @@ public class ChunkSnapshot
idx2 += (255 & msb_bytes[2*j+1]) << 4; idx2 += (255 & msb_bytes[2*j+1]) << 4;
} }
// Get even block id // Get even block id
blkidxs[2*j] = (idx < p.length) ? p[idx] : 0; states[2*j] = DynmapPlugin.stateByID[(idx < p.length) ? p[idx] : 0];
// Get odd block id // Get odd block id
blkidxs[2*j+1] = (idx2 < p.length) ? p[idx2] : 0; states[2*j+1] = DynmapPlugin.stateByID[(idx2 < p.length) ? p[idx2] : 0];
} }
} }
else { else {
// Get block IDs
byte[] lsb_bytes = sec.getByteArray("Blocks"); byte[] lsb_bytes = sec.getByteArray("Blocks");
int len = BLOCKS_PER_SECTION; if (lsb_bytes.length < BLOCKS_PER_SECTION) {
if(len > lsb_bytes.length) len = lsb_bytes.length; lsb_bytes = Arrays.copyOf(lsb_bytes, BLOCKS_PER_SECTION);
for(int j = 0; j < len; j++) {
blkidxs[j] = (0xFF & lsb_bytes[j]) << 4;
} }
// Get any additional ID data
byte[] addid = null;
if (sec.hasKey("Add", 7)) { /* If additional data, add it */ if (sec.hasKey("Add", 7)) { /* If additional data, add it */
byte[] msb = sec.getByteArray("Add"); addid = sec.getByteArray("Add");
len = BLOCKS_PER_SECTION / 2; if (addid.length < (BLOCKS_PER_SECTION / 2)) {
if(len > msb.length) len = msb.length; addid = Arrays.copyOf(addid, (BLOCKS_PER_SECTION / 2));
for (int j = 0; j < len; j++) {
short b = (short)(msb[j] & 0xFF);
if (b == 0) {
continue;
}
blkidxs[j << 1] |= (b & 0x0F) << 12;
blkidxs[(j << 1) + 1] |= (b & 0xF0) << 8;
} }
} }
// Check for NEID additional additional ID data
byte[] addid2 = null;
if (sec.hasKey("Add2", 7)) { /* If additional data (NEID), add it */ if (sec.hasKey("Add2", 7)) { /* If additional data (NEID), add it */
byte[] msb = sec.getByteArray("Add2"); addid2 = sec.getByteArray("Add2");
len = BLOCKS_PER_SECTION / 2; if (addid2.length < (BLOCKS_PER_SECTION / 2)) {
if(len > msb.length) len = msb.length; addid2 = Arrays.copyOf(addid2, (BLOCKS_PER_SECTION / 2));
for (int j = 0; j < len; j++) {
short b = (short)(msb[j] & 0xFF);
if (b == 0) {
continue;
}
blkidxs[j << 1] |= (b & 0x0F) << 16;
blkidxs[(j << 1) + 1] |= (b & 0xF0) << 12;
} }
} }
byte[] bd = sec.getByteArray("Data"); // Get meta nibble data
for (int j = 0; j < bd.length; j++) { byte[] bd = null;
int b = bd[j] & 0xFF; if (sec.hasKey("Data", 7)) {
if (b == 0) { bd = sec.getByteArray("Data");
continue; if (bd.length < (BLOCKS_PER_SECTION / 2)) {
} bd = Arrays.copyOf(bd, (BLOCKS_PER_SECTION / 2));
blkidxs[j << 1] |= b & 0x0F;
blkidxs[(j << 1) + 1] |= (b & 0xF0) >> 4;
} }
} }
this.emitlight[secnum] = sec.getByteArray("BlockLight"); // Traverse section
for(int j = 0; j < BLOCKS_PER_SECTION; j += 2) {
// Start with block ID
int id = (0xFF & lsb_bytes[j]) << 4;
int id2 = (0xFF & lsb_bytes[j+1]) << 4;
// Add in additional parts
if (addid != null) {
byte b = addid[j >> 1];
id += (0xF & b) << 12;
id2 += (0xF0 & b) << 8;
}
// Add in additional additional parts
if (addid2 != null) {
byte b = addid2[j >> 1];
id += (0xF & b) << 16;
id2 += (0xF0 & b) << 12;
}
// Add in metadata
if (bd != null) {
byte b = bd[j >> 1];
id += (0xF & b);
id2 += (0xF0 & b) >> 4;
}
// Compute states
states[j] = DynmapPlugin.stateByID[id];
states[j+1] = DynmapPlugin.stateByID[id2];
}
}
cursect.emitlight = sec.getByteArray("BlockLight");
if (sec.hasKey("SkyLight")) { if (sec.hasKey("SkyLight")) {
this.skylight[secnum] = sec.getByteArray("SkyLight"); cursect.skylight = sec.getByteArray("SkyLight");
} }
this.empty[secnum] = false;
} }
/* Get biome data */ /* Get biome data */
this.biome = new int[COLUMNS_PER_CHUNK]; this.biome = new int[COLUMNS_PER_CHUNK];
@ -209,32 +266,19 @@ public class ChunkSnapshot
return z; return z;
} }
public int getBlockTypeId(int x, int y, int z)
{
return blockidx[y >> 4][((y & 0xF) << 8) | (z << 4) | x] >> 4;
}
public int getBlockData(int x, int y, int z)
{
return blockidx[y >> 4][((y & 0xF) << 8) | (z << 4) | x] & 0xF;
}
public DynmapBlockState getBlockType(int x, int y, int z) public DynmapBlockState getBlockType(int x, int y, int z)
{ {
int id = blockidx[y >> 4][((y & 0xF) << 8) | (z << 4) | x]; return section[y >> 4].getBlockType(x, y, z);
return DynmapPlugin.stateByID[id];
} }
public int getBlockSkyLight(int x, int y, int z) public int getBlockSkyLight(int x, int y, int z)
{ {
int off = ((y & 0xF) << 7) | (z << 3) | (x >> 1); return section[y >> 4].getBlockSkyLight(x, y, z);
return (skylight[y >> 4][off] >> ((x & 1) << 2)) & 0xF;
} }
public int getBlockEmittedLight(int x, int y, int z) public int getBlockEmittedLight(int x, int y, int z)
{ {
int off = ((y & 0xF) << 7) | (z << 3) | (x >> 1); return section[y >> 4].getBlockEmittedLight(x, y, z);
return (emitlight[y >> 4][off] >> ((x & 1) << 2)) & 0xF;
} }
public int getHighestBlockYAt(int x, int z) public int getHighestBlockYAt(int x, int z)
@ -254,7 +298,7 @@ public class ChunkSnapshot
public boolean isSectionEmpty(int sy) public boolean isSectionEmpty(int sy)
{ {
return empty[sy]; return section[sy].isEmpty();
} }
public long getInhabitedTicks() { public long getInhabitedTicks() {

View File

@ -850,11 +850,6 @@ public class ForgeMapChunkCache extends MapChunkCache
return DynmapBlockState.AIR; return DynmapBlockState.AIR;
} }
@Override @Override
public final int getBlockData(int x, int y, int z)
{
return 0;
}
@Override
public final int getBlockSkyLight(int x, int y, int z) public final int getBlockSkyLight(int x, int y, int z)
{ {
if (y < 64) if (y < 64)