Skip to content

Commit

Permalink
Change UnionType to hold an ImmutableList instead of a Collection for…
Browse files Browse the repository at this point in the history
… the alternates 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=140442040
  • Loading branch information
lukesandberg authored and blickly committed Nov 29, 2016
1 parent 9b70a1f commit 9f58797
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 52 deletions.
15 changes: 12 additions & 3 deletions src/com/google/javascript/rhino/jstype/JSType.java
Expand Up @@ -302,7 +302,10 @@ 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()) { // use an indexed loop to avoid allocations
ImmutableList<JSType> alternatesList = this.toMaybeUnionType().getAlternatesList();
for (int i = 0; i < alternatesList.size(); i++) {
JSType alternate = alternatesList.get(i);
if (alternate.isSubtype(arrayType)) { if (alternate.isSubtype(arrayType)) {
return true; return true;
} }
Expand Down Expand Up @@ -1136,7 +1139,9 @@ static JSType filterNoResolvedType(JSType type) {
if (needsFiltering) { if (needsFiltering) {
UnionTypeBuilder builder = new UnionTypeBuilder(type.registry); UnionTypeBuilder builder = new UnionTypeBuilder(type.registry);
builder.addAlternate(type.getNativeType(JSTypeNative.NO_RESOLVED_TYPE)); builder.addAlternate(type.getNativeType(JSTypeNative.NO_RESOLVED_TYPE));
for (JSType alt : unionType.getAlternates()) { ImmutableList<JSType> alternatesList = unionType.getAlternatesList();
for (int i = 0; i < alternatesList.size(); i++) {
JSType alt = alternatesList.get(i);
if (!alt.isNoResolvedType()) { if (!alt.isNoResolvedType()) {
builder.addAlternate(alt); builder.addAlternate(alt);
} }
Expand Down Expand Up @@ -1406,7 +1411,11 @@ 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) { // use an indexed for-loop to avoid allocations
ImmutableList<JSType> alternatesWithoutStucturalTyping =
union.alternatesWithoutStucturalTyping;
for (int i = 0; i < alternatesWithoutStucturalTyping.size(); i++) {
JSType element = alternatesWithoutStucturalTyping.get(i);
if (thisType.isSubtype(element, implicitImplCache, subtypingMode)) { if (thisType.isSubtype(element, implicitImplCache, subtypingMode)) {
return true; return true;
} }
Expand Down

0 comments on commit 9f58797

Please sign in to comment.