Chunk state performance for 1.8.9

This commit is contained in:
Mike Primm 2018-09-06 22:39:26 -05:00
parent 690b56d6d3
commit a23b2f8237
2 changed files with 132 additions and 100 deletions

View File

@ -1,17 +1,12 @@
package org.dynmap.forge_1_8_9; package org.dynmap.forge_1_8_9;
import java.lang.reflect.InvocationTargetException; import java.util.Arrays;
import java.lang.reflect.Method;
import org.dynmap.Log; import org.dynmap.Log;
import org.dynmap.renderer.DynmapBlockState; import org.dynmap.renderer.DynmapBlockState;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.NibbleArray;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
import scala.actors.threadpool.Arrays;
/** /**
* Represents a static, thread-safe snapshot of chunk of blocks * Represents a static, thread-safe snapshot of chunk of blocks
@ -19,11 +14,15 @@ import scala.actors.threadpool.Arrays;
*/ */
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;
@ -32,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
* *
@ -58,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 */
@ -91,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);
@ -111,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");
@ -130,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;
} }
if (sec.hasKey("Add", 7)) { /* If additional data, add it */ // Get any additional ID data
byte[] msb = sec.getByteArray("Add"); byte[] addid = null;
len = BLOCKS_PER_SECTION / 2; if (sec.hasKey("Add", 7)) { /* If additional data, add it */
if(len > msb.length) len = msb.length; addid = sec.getByteArray("Add");
for (int j = 0; j < len; j++) { if (addid.length < (BLOCKS_PER_SECTION / 2)) {
short b = (short)(msb[j] & 0xFF); addid = Arrays.copyOf(addid, (BLOCKS_PER_SECTION / 2));
if (b == 0) { }
continue; }
} // Check for NEID additional additional ID data
blkidxs[j << 1] |= (b & 0x0F) << 12; byte[] addid2 = null;
blkidxs[(j << 1) + 1] |= (b & 0xF0) << 8; if (sec.hasKey("Add2", 7)) { /* If additional data (NEID), add it */
} addid2 = sec.getByteArray("Add2");
} if (addid2.length < (BLOCKS_PER_SECTION / 2)) {
if (sec.hasKey("Add2", 7)) { /* If additional data (NEID), add it */ addid2 = Arrays.copyOf(addid2, (BLOCKS_PER_SECTION / 2));
byte[] msb = sec.getByteArray("Add2"); }
len = BLOCKS_PER_SECTION / 2; }
if(len > msb.length) len = msb.length; // Get meta nibble data
for (int j = 0; j < len; j++) { byte[] bd = null;
short b = (short)(msb[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) << 16; }
blkidxs[(j << 1) + 1] |= (b & 0xF0) << 12; }
} // Traverse section
} for(int j = 0; j < BLOCKS_PER_SECTION; j += 2) {
byte[] bd = sec.getByteArray("Data"); // Start with block ID
for (int j = 0; j < bd.length; j++) { int id = (0xFF & lsb_bytes[j]) << 4;
int b = bd[j] & 0xFF; int id2 = (0xFF & lsb_bytes[j+1]) << 4;
if (b == 0) { // Add in additional parts
continue; if (addid != null) {
} byte b = addid[j >> 1];
blkidxs[j << 1] |= b & 0x0F; id += (0xF & b) << 12;
blkidxs[(j << 1) + 1] |= (b & 0xF0) >> 4; 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];
} }
} }
this.emitlight[secnum] = sec.getByteArray("BlockLight"); 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];
@ -215,33 +265,20 @@ 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)
@ -261,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

@ -852,11 +852,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)