Skip to content

Commit

Permalink
inline some local vars
Browse files Browse the repository at this point in the history
  • Loading branch information
esaulpaugh committed Mar 6, 2021
1 parent 781e175 commit 2f9a0dd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 30 deletions.
10 changes: 4 additions & 6 deletions src/main/java/com/esaulpaugh/headlong/abi/PackedDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,11 @@ private static int countDynamicsArrayType(ABIType<?> type) {

private static int decodeTuple(TupleType tupleType, byte[] buffer, int start, int end, Object[] parentElements, int pei) {
final ABIType<?>[] elementTypes = tupleType.elementTypes;
final int len = elementTypes.length;
final Object[] elements = new Object[len];
final Object[] elements = new Object[elementTypes.length];

int mark = -1;

for (int i = len - 1; i >= 0; i--) {
for (int i = elementTypes.length - 1; i >= 0; i--) {
final ABIType<?> type = elementTypes[i];
if (type.dynamic) {
mark = i;
Expand Down Expand Up @@ -136,9 +135,8 @@ private static int decode(ABIType<?> type, byte[] buffer, int idx, int end, Obje

private static int decodeTupleStatic(TupleType tupleType, byte[] buffer, int idx, int end, Object[] parentElements, int pei) {
final ABIType<?>[] elementTypes = tupleType.elementTypes;
final int len = elementTypes.length;
final Object[] elements = new Object[len];
for (int i = 0; i < len; i++) {
final Object[] elements = new Object[elementTypes.length];
for (int i = 0; i < elementTypes.length; i++) {
idx += decode(elementTypes[i], buffer, idx, end, elements, i);
}
return tupleType.byteLengthPacked(parentElements[pei] = new Tuple(elements));
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/com/esaulpaugh/headlong/abi/TupleType.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,17 @@ Tuple decode(ByteBuffer bb, byte[] unitBuffer) {
}

static void decodeObjects(ByteBuffer bb, byte[] unitBuffer, IntFunction<ABIType<?>> getType, Object[] objects) {
final int len = objects.length;
final int start = bb.position(); // save this value before offsets are decoded
final int[] offsets = new int[len];
for(int i = 0; i < len; i++) {
final int[] offsets = new int[objects.length];
for(int i = 0; i < objects.length; i++) {
ABIType<?> t = getType.apply(i);
if(!t.dynamic) {
objects[i] = t.decode(bb, unitBuffer);
} else {
offsets[i] = Encoding.UINT31.decode(bb, unitBuffer);
}
}
for (int i = 0; i < len; i++) {
for (int i = 0; i < objects.length; i++) {
final int offset = offsets[i];
if(offset > 0) {
final int jump = start + offset;
Expand Down Expand Up @@ -279,12 +278,11 @@ public TupleType subTupleTypeNegative(boolean... manifest) {
}

private TupleType subTupleType(boolean[] manifest, boolean negate) {
final int len = manifest.length;
if(len == elementTypes.length) {
if(manifest.length == elementTypes.length) {
final StringBuilder canonicalBuilder = new StringBuilder("(");
boolean dynamic = false;
final ArrayList<ABIType<?>> selected = new ArrayList<>(len);
for (int i = 0; i < len; i++) {
final ArrayList<ABIType<?>> selected = new ArrayList<>(manifest.length);
for (int i = 0; i < manifest.length; i++) {
if (negate ^ manifest[i]) {
ABIType<?> e = elementTypes[i];
canonicalBuilder.append(e.canonicalType).append(',');
Expand All @@ -294,7 +292,7 @@ private TupleType subTupleType(boolean[] manifest, boolean negate) {
}
return new TupleType(completeTupleTypeString(canonicalBuilder), dynamic, selected.toArray(EMPTY_TYPE_ARRAY));
}
throw new IllegalArgumentException("manifest.length != elementTypes.length: " + len + " != " + elementTypes.length);
throw new IllegalArgumentException("manifest.length != elementTypes.length: " + manifest.length + " != " + elementTypes.length);
}

static String completeTupleTypeString(StringBuilder sb) {
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/esaulpaugh/headlong/util/FastBase64.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ private static final class Standard { // inner class to delay loading of table u

static short[] init(byte[] smallTable) {
final short[] largeTable = new short[1 << 12];
final int len = smallTable.length;
for (int i = 0; i < len; i++) {
final int offset = i * len;
for (int j = 0; j < len; j++) {
for (int i = 0; i < smallTable.length; i++) {
final int offset = i * smallTable.length;
for (int j = 0; j < smallTable.length; j++) {
largeTable[offset + j] = (short) ((smallTable[i] << Byte.SIZE) | (smallTable[j] & 0xFF));
}
}
Expand Down
19 changes: 8 additions & 11 deletions src/main/java/com/esaulpaugh/headlong/util/SuperSerial.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,10 @@ private static Object serializeBigInteger(UnitType<?> ut, BigInteger bigInt) {
}

private static Object deserialize(ABIType<?> type, RLPItem item) {
final int typeCode = type.typeCode();
if(typeCode < TYPE_CODE_ARRAY && item.isList()) {
if(type.typeCode() < TYPE_CODE_ARRAY && item.isList()) {
throw new IllegalArgumentException("RLPList not allowed for this type: " + type);
}
switch (typeCode) {
switch (type.typeCode()) {
case TYPE_CODE_BOOLEAN: return item.asBoolean();
case TYPE_CODE_BYTE: return item.asByte(); // case currently goes unused
case TYPE_CODE_INT: return deserializeInt((IntType) type, item);
Expand Down Expand Up @@ -177,10 +176,9 @@ private static BigInteger deserializeBigInteger(UnitType<?> ut, RLPItem item) {
}

private static byte[] toSigned(int typeBits, BigInteger val) {
final int signum = val.signum();
if(signum != 0) {
if(val.signum() != 0) {
final byte[] bytes = val.toByteArray();
return signum < 0
return val.signum() < 0
? signExtendNegative(bytes, typeBits / Byte.SIZE)
: bytes[0] != 0
? bytes
Expand All @@ -197,14 +195,13 @@ private static byte[] signExtendNegative(final byte[] negative, final int newWid
}

private static BigInteger asSigned(int typeBits, RLPItem item) {
final int dataLen = item.dataLength;
if(dataLen != 0) {
if (dataLen * Byte.SIZE < typeBits) {
byte[] padded = new byte[dataLen + 1];
if(item.dataLength != 0) {
if (item.dataLength * Byte.SIZE < typeBits) {
byte[] padded = new byte[item.dataLength + 1];
item.exportData(padded, 1);
return new BigInteger(padded);
}
if(dataLen > UNIT_LENGTH_BYTES) {
if(item.dataLength > UNIT_LENGTH_BYTES) {
throw new IllegalArgumentException("integer data cannot exceed " + UNIT_LENGTH_BYTES + " bytes");
}
return item.asBigIntSigned();
Expand Down

0 comments on commit 2f9a0dd

Please sign in to comment.