Skip to content

Commit

Permalink
optimize TupleType creation
Browse files Browse the repository at this point in the history
  • Loading branch information
esaulpaugh committed Sep 3, 2024
1 parent 9f86212 commit c4e9211
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
22 changes: 11 additions & 11 deletions src/main/java/com/esaulpaugh/headlong/abi/TupleType.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
import com.esaulpaugh.headlong.util.FastHex;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.function.IntUnaryOperator;

import static com.esaulpaugh.headlong.abi.UnitType.UNIT_LENGTH_BYTES;
Expand Down Expand Up @@ -342,29 +340,31 @@ private TupleType<J> selectElements(final boolean[] manifest, final boolean nega
}
final StringBuilder canonicalType = new StringBuilder("(");
boolean dynamic = false;
final List<ABIType<?>> selected = new ArrayList<>(size());
final List<String> selectedNames = elementNames == null ? null : new ArrayList<>(size());
final List<String> selectedInternalTypes = elementInternalTypes == null ? null : new ArrayList<>(size());
final ABIType<?>[] selected = new ABIType<?>[size()];
final String[] selectedNames = elementNames == null ? null : new String[size()];
final String[] selectedInternalTypes = elementInternalTypes == null ? null : new String[size()];
int c = 0;
for (int i = 0; i < size(); i++) {
if (negate ^ manifest[i]) {
if (selectedNames != null) {
selectedNames.add(elementNames[i]);
selectedNames[c] = elementNames[i];
}
if (selectedInternalTypes != null) {
selectedInternalTypes.add(elementInternalTypes[i]);
selectedInternalTypes[c] = elementInternalTypes[i];
}
ABIType<?> e = get(i);
canonicalType.append(e.canonicalType).append(',');
dynamic |= e.dynamic;
selected.add(e);
selected[c] = e;
c++;
}
}
return new TupleType<>(
completeTupleTypeString(canonicalType),
dynamic,
selected.toArray(EMPTY_ARRAY),
selectedNames == null ? null : selectedNames.toArray(new String[0]),
selectedInternalTypes == null ? null : selectedInternalTypes.toArray(new String[0]),
Arrays.copyOf(selected, c),
selectedNames == null ? null : Arrays.copyOf(selectedNames, c),
selectedInternalTypes == null ? null : Arrays.copyOf(selectedInternalTypes, c),
this.flags
);
}
Expand Down
27 changes: 15 additions & 12 deletions src/main/java/com/esaulpaugh/headlong/abi/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@

import com.esaulpaugh.headlong.util.Integers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.esaulpaugh.headlong.abi.ArrayType.DYNAMIC_LENGTH;
Expand Down Expand Up @@ -212,10 +211,11 @@ static StringBuilder newTypeBuilder() {
private static TupleType<?> parseTupleType(final String rawTypeStr, final String[] elementNames, final int flags) { /* assumes that rawTypeStr.charAt(0) == '(' */
final int len = rawTypeStr.length();
if (len == 2 && "()".equals(rawTypeStr)) return TupleType.empty(flags);
final List<ABIType<?>> elements = new ArrayList<>(8);
ABIType<?>[] elements = new ABIType[8];
int argEnd = 1;
final StringBuilder canonicalType = newTypeBuilder();
boolean dynamic = false;
int i = 0;
try {
for (;;) {
final int argStart = argEnd;
Expand All @@ -228,23 +228,26 @@ private static TupleType<?> parseTupleType(final String rawTypeStr, final String
final ABIType<?> e = buildUnchecked(rawTypeStr.substring(argStart, argEnd), null, null, flags);
canonicalType.append(e.canonicalType);
dynamic |= e.dynamic;
elements.add(e);
elements[i++] = e;
if (rawTypeStr.charAt(argEnd++) == ')') {
return argEnd != len
? null
: new TupleType<>(
canonicalType.append(')').toString(),
dynamic,
elements.toArray(new ABIType[0]),
elementNames,
null,
flags
);
canonicalType.append(')').toString(),
dynamic,
Arrays.copyOf(elements, i),
elementNames,
null,
flags
);
}
if (i == elements.length) {
elements = Arrays.copyOf(elements, i << 1);
}
canonicalType.append(',');
}
} catch (IllegalArgumentException iae) {
throw new IllegalArgumentException("@ index " + elements.size() + ", " + iae.getMessage(), iae);
throw new IllegalArgumentException("@ index " + i + ", " + iae.getMessage(), iae);
}
}

Expand Down

0 comments on commit c4e9211

Please sign in to comment.