Skip to content

Commit

Permalink
Change UnionType to hold a List instead of a Collection for the alter…
Browse files Browse the repository at this point in the history
…nates and switch most accessors to do indexed iteration.

heap profiling showed that the iteration in JsType.checkEquivalenceHelper was allocating lots of iterators. This is an easy, if slightly annoying, way to make these operations a little cheaper.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=139602401
  • Loading branch information
lukesandberg authored and blickly committed Nov 19, 2016
1 parent ca89eb9 commit e918996
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 63 deletions.
21 changes: 14 additions & 7 deletions src/com/google/javascript/rhino/jstype/JSType.java
Expand Up @@ -44,13 +44,14 @@
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.HashBasedTable; import com.google.common.collect.HashBasedTable;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet;
import com.google.javascript.rhino.ErrorReporter; import com.google.javascript.rhino.ErrorReporter;
import com.google.javascript.rhino.JSDocInfo; import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.TypeI; import com.google.javascript.rhino.TypeI;
import java.io.Serializable; import java.io.Serializable;
import java.util.Comparator; import java.util.Comparator;
import java.util.IdentityHashMap; import java.util.IdentityHashMap;
import java.util.List;


/** /**
* Represents JavaScript value types.<p> * Represents JavaScript value types.<p>
Expand Down Expand Up @@ -78,8 +79,8 @@ public abstract class JSType implements TypeI, Serializable {
private static final CanCastToVisitor CAN_CAST_TO_VISITOR = private static final CanCastToVisitor CAN_CAST_TO_VISITOR =
new CanCastToVisitor(); new CanCastToVisitor();


private static final ImmutableList<String> COVARIANT_TYPES = private static final ImmutableSet<String> COVARIANT_TYPES =
ImmutableList.of("Object", "IArrayLike", "Array"); ImmutableSet.of("Object", "IArrayLike", "Array");


/** /**
* Total ordering on types based on their textual representation. * Total ordering on types based on their textual representation.
Expand Down Expand Up @@ -302,7 +303,9 @@ public boolean containsArray() {
// Check if this is a union that contains an array // Check if this is a union that contains an array
if (this.isUnionType()) { if (this.isUnionType()) {
JSType arrayType = registry.getNativeType(JSTypeNative.ARRAY_TYPE); JSType arrayType = registry.getNativeType(JSTypeNative.ARRAY_TYPE);
for (JSType alternate : this.toMaybeUnionType().getAlternates()) { List<JSType> alternates = this.toMaybeUnionType().getAlternates();
for (int i = 0; i < alternates.size(); i++) {
JSType alternate = alternates.get(i);
if (alternate.isSubtype(arrayType)) { if (alternate.isSubtype(arrayType)) {
return true; return true;
} }
Expand Down Expand Up @@ -1126,7 +1129,9 @@ static JSType filterNoResolvedType(JSType type) {
} else if (type.isUnionType()) { } else if (type.isUnionType()) {
UnionType unionType = type.toMaybeUnionType(); UnionType unionType = type.toMaybeUnionType();
boolean needsFiltering = false; boolean needsFiltering = false;
for (JSType alt : unionType.getAlternates()) { List<JSType> alternates = unionType.getAlternates();
for (int i = 0; i < alternates.size(); i++) {
JSType alt = alternates.get(i);
if (alt.isNoResolvedType()) { if (alt.isNoResolvedType()) {
needsFiltering = true; needsFiltering = true;
break; break;
Expand Down Expand Up @@ -1406,8 +1411,10 @@ static boolean isSubtypeHelper(JSType thisType, JSType thatType,
// unions // unions
if (thatType.isUnionType()) { if (thatType.isUnionType()) {
UnionType union = thatType.toMaybeUnionType(); UnionType union = thatType.toMaybeUnionType();
for (JSType element : union.alternatesWithoutStucturalTyping) { List<JSType> alternates = union.alternatesWithoutStucturalTyping;
if (thisType.isSubtype(element, implicitImplCache, subtypingMode)) { // use indexed iteration to avoid iterator allocations
for (int i = 0; i < alternates.size(); i++) {
if (thisType.isSubtype(alternates.get(i), implicitImplCache, subtypingMode)) {
return true; return true;
} }
} }
Expand Down

0 comments on commit e918996

Please sign in to comment.