3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-09-17 01:23:43 +02:00

Temp arrays are evil, (memory optimisation)

Dieser Commit ist enthalten in:
Myles 2016-03-07 19:19:14 +00:00
Ursprung a92a7d6e02
Commit 012eec47b5

Datei anzeigen

@ -18,19 +18,19 @@ public class NetUtil {
public static int writeNewColumn(ByteBuf out, Column column, boolean fullChunk, boolean hasSkylight) throws IOException { public static int writeNewColumn(ByteBuf out, Column column, boolean fullChunk, boolean hasSkylight) throws IOException {
int mask = 0; int mask = 0;
Chunk chunks[] = column.getChunks(); Chunk chunks[] = column.getChunks();
for(int index = 0; index < chunks.length; index++) { for (int index = 0; index < chunks.length; index++) {
Chunk chunk = chunks[index]; Chunk chunk = chunks[index];
if(chunk != null && (!fullChunk || !chunk.isEmpty())) { if (chunk != null && (!fullChunk || !chunk.isEmpty())) {
mask |= 1 << index; mask |= 1 << index;
chunk.getBlocks().write(out); chunk.getBlocks().write(out);
chunk.getBlockLight().write(out); chunk.getBlockLight().write(out);
if(hasSkylight || column.hasSkylight()) { if (hasSkylight || column.hasSkylight()) {
chunk.getSkyLight().write(out); // TODO: Make a PR to original lib to correct this chunk.getSkyLight().write(out); // TODO: Make a PR to original lib to correct this
} }
} }
} }
if(fullChunk && column.getBiomeData() != null) { if (fullChunk && column.getBiomeData() != null) {
out.writeBytes(column.getBiomeData()); out.writeBytes(column.getBiomeData());
} }
@ -48,44 +48,43 @@ public class NetUtil {
// 3 = Get sky light. // 3 = Get sky light.
Chunk[] chunks = new Chunk[16]; Chunk[] chunks = new Chunk[16];
for(int pass = 0; pass < 4; pass++) { for (int pass = 0; pass < 4; pass++) {
for(int ind = 0; ind < 16; ind++) { for (int ind = 0; ind < 16; ind++) {
if((bitmask & 1 << ind) != 0) { if ((bitmask & 1 << ind) != 0) {
if(pass == 0) { if (pass == 0) {
// Block length + Blocklight length // Block length + Blocklight length
expected += (4096 * 2) + 2048; expected += (4096 * 2) + 2048;
} }
if(pass == 1) { if (pass == 1) {
chunks[ind] = new Chunk(sky || hasSkyLight); chunks[ind] = new Chunk(sky || hasSkyLight);
buf.position(pos / 2); buf.position(pos / 2);
short[] tempData = new short[4096]; int buffPos = buf.position();
buf.get(tempData, 0, tempData.length);
// convert short array to new one // convert short array to new one
int index = 0;
for(short ss:tempData){ for (int index = 0; index < 4096; index++) {
short ss = buf.get(buffPos + index);
// s is 16 bits, 12 bits id and 4 bits data // s is 16 bits, 12 bits id and 4 bits data
int data = ss & 0xF; int data = ss & 0xF;
int id = (ss >> 4) << 4 | data; int id = (ss >> 4) << 4 | data;
int newCombined = id ; // test int newCombined = id; // test
chunks[ind].getBlocks().set(index, newCombined); chunks[ind].getBlocks().set(index, newCombined);
index++;
} }
pos += tempData.length * 2; pos += 4096 * 2;
} }
if(pass == 2) { if (pass == 2) {
NibbleArray3d blocklight = chunks[ind].getBlockLight(); NibbleArray3d blocklight = chunks[ind].getBlockLight();
System.arraycopy(input, pos, blocklight.getData(), 0, blocklight.getData().length); System.arraycopy(input, pos, blocklight.getData(), 0, blocklight.getData().length);
pos += blocklight.getData().length; pos += blocklight.getData().length;
} }
if(pass == 3) { if (pass == 3) {
if(sky) { if (sky) {
NibbleArray3d skylight = chunks[ind].getSkyLight(); NibbleArray3d skylight = chunks[ind].getSkyLight();
System.arraycopy(input, pos, skylight.getData(), 0, skylight.getData().length); System.arraycopy(input, pos, skylight.getData(), 0, skylight.getData().length);
pos += skylight.getData().length; pos += skylight.getData().length;
@ -94,16 +93,16 @@ public class NetUtil {
} }
} }
if(pass == 0) { if (pass == 0) {
// If we have more data than blocks and blocklight combined, there must be skylight data as well. // If we have more data than blocks and blocklight combined, there must be skylight data as well.
if(input.length > expected) { if (input.length > expected) {
sky = checkForSky; sky = checkForSky;
} }
} }
} }
byte biomeData[] = null; byte biomeData[] = null;
if(isFullChunk && (pos + 256 <= input.length)) { if (isFullChunk && (pos + 256 <= input.length)) {
biomeData = new byte[256]; biomeData = new byte[256];
System.arraycopy(input, pos, biomeData, 0, biomeData.length); System.arraycopy(input, pos, biomeData, 0, biomeData.length);