Avoid creation of new objects for empty byte array

This commit is contained in:
nicksavers 2014-10-12 13:06:51 +02:00
parent 3658a9d847
commit 2e56de357f
9 changed files with 18 additions and 20 deletions

View File

@ -12,7 +12,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.util.BigIntegers;
import java.math.BigInteger;
import java.security.SignatureException;
/**

View File

@ -41,7 +41,7 @@ public class AccountState {
if (code != null && code.length() > 2)
this.code = Hex.decode(code.substring(2));
else
this.code = new byte[0];
this.code = ByteUtil.EMPTY_BYTE_ARRAY;
this.nonce = new BigInteger(nonce).toByteArray();
@ -61,7 +61,7 @@ public class AccountState {
if (keyS != null && keyS.length() > 2)
data = Hex.decode(keyS);
else
data = new byte[0];
data = ByteUtil.EMPTY_BYTE_ARRAY;
key = new ByteArrayWrapper(data);
}

View File

@ -38,15 +38,13 @@ public class CallCreate {
if (data != null && data.length() > 2)
this.data = Hex.decode(data.substring(2));
else
this.data = new byte[0];
this.data = ByteUtil.EMPTY_BYTE_ARRAY;
this.destination = Hex.decode(destination);
this.gasLimit = ByteUtil.bigIntegerToBytes(new BigInteger(gasLimit));
this.value = ByteUtil.bigIntegerToBytes(new BigInteger(value));
}
public byte[] getData() {
return data;
}

View File

@ -60,12 +60,12 @@ public class Exec {
if (code != null && code.length() > 2)
this.code = Hex.decode(code.substring(2));
else
this.code = new byte[0];
this.code = ByteUtil.EMPTY_BYTE_ARRAY;
if (data != null && data.length() > 2)
this.data = Hex.decode(data.substring(2));
else
this.data = new byte[0];
this.data = ByteUtil.EMPTY_BYTE_ARRAY;
this.gas = ByteUtil.bigIntegerToBytes(new BigInteger(gas));
this.gasPrice = ByteUtil.bigIntegerToBytes(new BigInteger(gasPrice));

View File

@ -65,7 +65,7 @@ public class TestCase {
if (outString != null && outString.length() > 2)
this.out = Hex.decode(outString.substring(2));
else
this.out = new byte[0];
this.out = ByteUtil.EMPTY_BYTE_ARRAY;
for (Object key : preJSON.keySet()){

View File

@ -49,6 +49,7 @@ public class TestRunner {
Repository repository = new RepositoryImpl();
System.out.println("--------- PRE ---------");
/* 1. Store pre-exist accounts - Pre */
for (ByteArrayWrapper key : testCase.getPre().keySet()) {
@ -69,7 +70,7 @@ public class TestRunner {
byte[] address = exec.getAddress();
byte[] origin = exec.getOrigin();
byte[] caller = exec.getCaller();
byte[] balance = ByteUtil.bigIntegerToBytes(repository.getBalance(exec.getAddress()));
byte[] balance = ByteUtil.bigIntegerToBytes(repository.getBalance(exec.getAddress()));
byte[] gasPrice = exec.getGasPrice();
byte[] gas = exec.getGas();
byte[] callValue = exec.getValue();
@ -91,14 +92,14 @@ public class TestRunner {
Program program = new Program(exec.getCode(), programInvoke);
try {
System.out.println("-------- EXEC --------");
while(!program.isStopped())
vm.step(program);
System.out.println();
} catch (RuntimeException e) {
program.setRuntimeFailure(e);
}
System.out.println("--------- POST --------");
/* 5. Assert Post values */
for (ByteArrayWrapper key : testCase.getPost().keySet()) {
@ -275,7 +276,7 @@ public class TestRunner {
// assert out
byte[] expectedHReturn = testCase.getOut();
byte[] actualHReturn = new byte[0];
byte[] actualHReturn = ByteUtil.EMPTY_BYTE_ARRAY;
if (program.getResult().getHReturn() != null) {
actualHReturn = program.getResult().getHReturn().array();
}

View File

@ -80,7 +80,7 @@ public class HelloMessage extends P2pMessage {
byte[] clientId = RLP.encodeString(this.clientId);
byte[][] capabilities = new byte[this.capabilities.size()][];
for (int i = 0; i < this.capabilities.size(); i++) {
capabilities[i] = RLP.encode(this.capabilities.get(i).getBytes());
capabilities[i] = RLP.encodeElement(this.capabilities.get(i).getBytes());
}
byte[] capabilityList = RLP.encodeList(capabilities);
byte[] peerPort = RLP.encodeInt(this.listenPort);

View File

@ -649,7 +649,7 @@ public class RLP {
}
// null item
if ((msgData[pos] & 0xFF) == OFFSET_SHORT_ITEM) {
byte[] item = new byte[0];
byte[] item = ByteUtil.EMPTY_BYTE_ARRAY;
RLPItem rlpItem = new RLPItem(item);
rlpList.add(rlpItem);
pos += 1;
@ -741,7 +741,7 @@ public class RLP {
if (inputArray.size() == 0) {
return encodeLength(inputArray.size(), OFFSET_SHORT_LIST);
}
byte[] output = new byte[0];
byte[] output = ByteUtil.EMPTY_BYTE_ARRAY;
for (Object object : inputArray) {
output = concatenate(output, encode(object));
}
@ -916,13 +916,13 @@ public class RLP {
return inputString.getBytes();
} else if(input instanceof Long) {
Long inputLong = (Long) input;
return (inputLong == 0) ? new byte[0] : asUnsignedByteArray(BigInteger.valueOf(inputLong));
return (inputLong == 0) ? ByteUtil.EMPTY_BYTE_ARRAY : asUnsignedByteArray(BigInteger.valueOf(inputLong));
} else if(input instanceof Integer) {
Integer inputInt = (Integer) input;
return (inputInt == 0) ? new byte[0] : asUnsignedByteArray(BigInteger.valueOf(inputInt.intValue()));
return (inputInt == 0) ? ByteUtil.EMPTY_BYTE_ARRAY : asUnsignedByteArray(BigInteger.valueOf(inputInt.intValue()));
} else if(input instanceof BigInteger) {
BigInteger inputBigInt = (BigInteger) input;
return (inputBigInt == BigInteger.ZERO) ? new byte[0] : asUnsignedByteArray(inputBigInt);
return (inputBigInt == BigInteger.ZERO) ? ByteUtil.EMPTY_BYTE_ARRAY : asUnsignedByteArray(inputBigInt);
} else if (input instanceof Value) {
Value val = (Value) input;
return toBytes(val.asObj());

View File

@ -81,7 +81,7 @@ public class Value {
} else if(isString()) {
return asString().getBytes();
}
return new byte[0];
return ByteUtil.EMPTY_BYTE_ARRAY;
}
public int[] asSlice() {