From 012eec47b52f543534914bf467a231224a58ea45 Mon Sep 17 00:00:00 2001 From: Myles Date: Mon, 7 Mar 2016 19:19:14 +0000 Subject: [PATCH] Temp arrays are evil, (memory optimisation) --- .../org/spacehq/mc/protocol/util/NetUtil.java | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/spacehq/mc/protocol/util/NetUtil.java b/src/main/java/org/spacehq/mc/protocol/util/NetUtil.java index 08f168850..72cc1e07b 100644 --- a/src/main/java/org/spacehq/mc/protocol/util/NetUtil.java +++ b/src/main/java/org/spacehq/mc/protocol/util/NetUtil.java @@ -18,19 +18,19 @@ public class NetUtil { public static int writeNewColumn(ByteBuf out, Column column, boolean fullChunk, boolean hasSkylight) throws IOException { int mask = 0; Chunk chunks[] = column.getChunks(); - for(int index = 0; index < chunks.length; index++) { + for (int index = 0; index < chunks.length; index++) { Chunk chunk = chunks[index]; - if(chunk != null && (!fullChunk || !chunk.isEmpty())) { + if (chunk != null && (!fullChunk || !chunk.isEmpty())) { mask |= 1 << index; chunk.getBlocks().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 } } } - if(fullChunk && column.getBiomeData() != null) { + if (fullChunk && column.getBiomeData() != null) { out.writeBytes(column.getBiomeData()); } @@ -48,44 +48,43 @@ public class NetUtil { // 3 = Get sky light. Chunk[] chunks = new Chunk[16]; - for(int pass = 0; pass < 4; pass++) { - for(int ind = 0; ind < 16; ind++) { - if((bitmask & 1 << ind) != 0) { - if(pass == 0) { + for (int pass = 0; pass < 4; pass++) { + for (int ind = 0; ind < 16; ind++) { + if ((bitmask & 1 << ind) != 0) { + if (pass == 0) { // Block length + Blocklight length expected += (4096 * 2) + 2048; } - if(pass == 1) { + if (pass == 1) { chunks[ind] = new Chunk(sky || hasSkyLight); buf.position(pos / 2); - short[] tempData = new short[4096]; - buf.get(tempData, 0, tempData.length); + int buffPos = buf.position(); // 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 int data = ss & 0xF; int id = (ss >> 4) << 4 | data; - int newCombined = id ; // test + int newCombined = id; // test 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(); System.arraycopy(input, pos, blocklight.getData(), 0, blocklight.getData().length); pos += blocklight.getData().length; } - if(pass == 3) { - if(sky) { + if (pass == 3) { + if (sky) { NibbleArray3d skylight = chunks[ind].getSkyLight(); System.arraycopy(input, pos, skylight.getData(), 0, 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(input.length > expected) { + if (input.length > expected) { sky = checkForSky; } } } byte biomeData[] = null; - if(isFullChunk && (pos + 256 <= input.length)) { + if (isFullChunk && (pos + 256 <= input.length)) { biomeData = new byte[256]; System.arraycopy(input, pos, biomeData, 0, biomeData.length);