mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-12-14 07:09:04 +00:00
Changed byte array in Memory to char array (+60% performance improvement)
This commit is contained in:
@@ -5,6 +5,8 @@ import net.simon987.server.assembly.exception.AssemblyException;
|
||||
import net.simon987.server.assembly.exception.DuplicateSegmentException;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
@@ -30,7 +32,7 @@ public class AssemblyResult {
|
||||
/**
|
||||
* Offset of the code segment
|
||||
*/
|
||||
public int codeSegmentOffset;
|
||||
private int codeSegmentOffset;
|
||||
/**
|
||||
* Line of the code segment definition (for editor icons)
|
||||
*/
|
||||
@@ -104,4 +106,20 @@ public class AssemblyResult {
|
||||
|
||||
}
|
||||
|
||||
public char[] getWords() {
|
||||
|
||||
char[] assembledCode = new char[bytes.length / 2];
|
||||
ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).asCharBuffer().get(assembledCode);
|
||||
|
||||
return assembledCode;
|
||||
}
|
||||
|
||||
public int getCodeSegmentOffset() {
|
||||
if (codeSegmentSet) {
|
||||
return codeSegmentOffset;
|
||||
} else {
|
||||
return origin;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
package net.simon987.server.assembly;
|
||||
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.io.JSONSerialisable;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.zip.Deflater;
|
||||
@@ -23,7 +26,7 @@ public class Memory implements Target, JSONSerialisable {
|
||||
/**
|
||||
* Contents of the memory
|
||||
*/
|
||||
private byte[] bytes;
|
||||
private char[] words;
|
||||
|
||||
/**
|
||||
* Create an empty Memory object
|
||||
@@ -31,7 +34,7 @@ public class Memory implements Target, JSONSerialisable {
|
||||
* @param size Size of the memory, in words
|
||||
*/
|
||||
public Memory(int size) {
|
||||
bytes = new byte[size];
|
||||
words = new char[size];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,29 +45,26 @@ public class Memory implements Target, JSONSerialisable {
|
||||
*/
|
||||
@Override
|
||||
public int get(int address) {
|
||||
address = address * 2; //Because our Memory is only divisible by 16bits
|
||||
address = (char) address;
|
||||
|
||||
if (address + 2 > bytes.length) {
|
||||
if (address >= words.length) {
|
||||
LogManager.LOGGER.info("DEBUG: Trying to get memory out of bounds " + address);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ((bytes[address] & 0xFF) << 8) | (bytes[address + 1] & 0xFF);
|
||||
return words[address];
|
||||
}
|
||||
|
||||
/**
|
||||
* Write x words from an array at an offset
|
||||
*/
|
||||
public boolean write(int offset, byte[] bytes, int srcOffset, int count) {
|
||||
public boolean write(int offset, char[] src, int srcOffset, int count) {
|
||||
|
||||
offset = (char)offset * 2;
|
||||
|
||||
|
||||
if (offset + count > this.bytes.length || srcOffset >= bytes.length || count < 0 || offset < 0) {
|
||||
if (offset + count > this.words.length || srcOffset >= src.length || count < 0 || offset < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
System.arraycopy(bytes, srcOffset, this.bytes, offset, count);
|
||||
System.arraycopy(src, srcOffset, this.words, offset, count);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -76,30 +76,31 @@ public class Memory implements Target, JSONSerialisable {
|
||||
*/
|
||||
@Override
|
||||
public void set(int address, int value) {
|
||||
address = (char) address;
|
||||
|
||||
address = (char)address * 2;
|
||||
|
||||
|
||||
if (address + 2 > bytes.length) {
|
||||
if (address >= words.length) {
|
||||
LogManager.LOGGER.info("DEBUG: Trying to set memory out of bounds: " + address);
|
||||
return;
|
||||
}
|
||||
|
||||
bytes[address] = (byte) ((value >> 8));
|
||||
bytes[address + 1] = (byte) (value & 0xFF);
|
||||
words[address] = (char) value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill the memory with 0s
|
||||
*/
|
||||
public void clear() {
|
||||
Arrays.fill(bytes, (byte) 0);
|
||||
Arrays.fill(words, (char) 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get byte array of the Memory object
|
||||
*/
|
||||
public byte[] getBytes() {
|
||||
|
||||
byte[] bytes = new byte[words.length * 2];
|
||||
ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).asCharBuffer().put(words);
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
@@ -112,7 +113,7 @@ public class Memory implements Target, JSONSerialisable {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
Deflater compressor = new Deflater(Deflater.BEST_COMPRESSION, true);
|
||||
DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(stream, compressor);
|
||||
deflaterOutputStream.write(bytes);
|
||||
deflaterOutputStream.write(getBytes());
|
||||
deflaterOutputStream.close();
|
||||
byte[] compressedBytes = stream.toByteArray();
|
||||
|
||||
@@ -128,25 +129,39 @@ public class Memory implements Target, JSONSerialisable {
|
||||
public static Memory deserialize(JSONObject json){
|
||||
|
||||
Memory memory = new Memory(0);
|
||||
byte[] compressedBytes = Base64.getDecoder().decode((String)json.get("zipBytes"));
|
||||
|
||||
try {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
Inflater decompressor = new Inflater(true);
|
||||
InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(baos, decompressor);
|
||||
inflaterOutputStream.write(compressedBytes);
|
||||
inflaterOutputStream.close();
|
||||
String zipBytesStr = (String) json.get("zipBytes");
|
||||
|
||||
memory.bytes = baos.toByteArray();
|
||||
if (zipBytesStr != null) {
|
||||
byte[] compressedBytes = Base64.getDecoder().decode((String) json.get("zipBytes"));
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
try {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
Inflater decompressor = new Inflater(true);
|
||||
InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(baos, decompressor);
|
||||
inflaterOutputStream.write(compressedBytes);
|
||||
inflaterOutputStream.close();
|
||||
|
||||
memory.setBytes(baos.toByteArray());
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
LogManager.LOGGER.severe("Memory was manually deleted");
|
||||
memory = new Memory(GameServer.INSTANCE.getConfig().getInt("memory_size"));
|
||||
}
|
||||
|
||||
|
||||
return memory;
|
||||
}
|
||||
|
||||
public void setBytes(byte[] bytes) {
|
||||
this.bytes = bytes;
|
||||
this.words = new char[bytes.length / 2];
|
||||
ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).asCharBuffer().get(this.words);
|
||||
}
|
||||
|
||||
public char[] getWords() {
|
||||
return words;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,10 +113,11 @@ public class GameUniverse implements JSONSerialisable{
|
||||
user.getCpu().getMemory().clear();
|
||||
|
||||
//Write assembled code to mem
|
||||
user.getCpu().getMemory().write((short) ar.origin, ar.bytes, 0, ar.bytes.length);
|
||||
user.getCpu().setCodeSegmentOffset(ar.codeSegmentOffset);
|
||||
char[] assembledCode = ar.getWords();
|
||||
|
||||
user.getCpu().getMemory().write((char) ar.origin, assembledCode, 0, assembledCode.length);
|
||||
user.getCpu().setCodeSegmentOffset(ar.getCodeSegmentOffset());
|
||||
|
||||
//Init
|
||||
|
||||
} else {
|
||||
user = new User(null);
|
||||
|
||||
@@ -28,8 +28,10 @@ public class CodeUploadHandler implements MessageHandler {
|
||||
user.getUser().getCpu().getMemory().clear();
|
||||
|
||||
//Write assembled code to mem
|
||||
user.getUser().getCpu().getMemory().write((char) ar.origin, ar.bytes, 0, ar.bytes.length);
|
||||
user.getUser().getCpu().setCodeSegmentOffset(ar.codeSegmentOffset);
|
||||
char[] assembledCode = ar.getWords();
|
||||
|
||||
user.getUser().getCpu().getMemory().write((char) ar.origin, assembledCode, 0, assembledCode.length);
|
||||
user.getUser().getCpu().setCodeSegmentOffset(ar.getCodeSegmentOffset());
|
||||
|
||||
JSONObject response = new JSONObject();
|
||||
response.put("t", "codeResponse");
|
||||
|
||||
@@ -15,11 +15,15 @@ public class FloppyHandler implements MessageHandler {
|
||||
|
||||
LogManager.LOGGER.info("(WS) Floppy download request from " + user.getUser().getUsername());
|
||||
|
||||
if (user.isGuest()) {
|
||||
return;
|
||||
}
|
||||
|
||||
//floppy
|
||||
byte[] bytes = user.getUser().getControlledUnit().getFloppyData().getBytes();
|
||||
if (user.getUser().getControlledUnit().getFloppyData() != null) {
|
||||
byte[] bytes = user.getUser().getControlledUnit().getFloppyData().getBytes();
|
||||
user.getWebSocket().send(bytes);
|
||||
}
|
||||
|
||||
user.getWebSocket().send(bytes);
|
||||
|
||||
} else if (json.get("t").equals("floppyUp")) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user