Skip to content

Commit

Permalink
Revert "Speedup lub distribution for big lubs (sorbet#3192)" (sorbet#…
Browse files Browse the repository at this point in the history
…3197)

This reverts commit a02d44c.
  • Loading branch information
jez authored Jun 15, 2020
1 parent 0930372 commit 4fb3a6e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 241 deletions.
1 change: 0 additions & 1 deletion core/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,6 @@ class OrType final : public GroundType {
friend TypePtr lubGround(const GlobalState &gs, const TypePtr &t1, const TypePtr &t2);
friend TypePtr Types::lub(const GlobalState &gs, const TypePtr &t1, const TypePtr &t2);
friend TypePtr Types::glb(const GlobalState &gs, const TypePtr &t1, const TypePtr &t2);
friend TypePtr filterOrComponents(const TypePtr &originalType, const InlinedVector<Type *, 4> &typeFilter);
friend TypePtr Types::dropSubtypesOf(const GlobalState &gs, const TypePtr &from, SymbolRef klass);
friend TypePtr Types::unwrapSelfTypeParam(Context ctx, const TypePtr &t1);
friend class Symbol; // the actual method is `recordSealedSubclass(Mutableconst GlobalState &gs, SymbolRef
Expand Down
81 changes: 24 additions & 57 deletions core/types/subtyping.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,69 +44,36 @@ const TypePtr underlying(const TypePtr &t1) {
return t1;
}

void fillInOrComponents(InlinedVector<TypePtr, 4> &orComponents, const TypePtr &type) {
auto *o = cast_type<OrType>(type.get());
if (o == nullptr) {
orComponents.emplace_back(type);
} else {
fillInOrComponents(orComponents, o->left);
fillInOrComponents(orComponents, o->right);
}
}

TypePtr filterOrComponents(const TypePtr &originalType, const InlinedVector<Type *, 4> &typeFilter) {
auto *o = cast_type<OrType>(originalType.get());
if (o == nullptr) {
if (absl::c_linear_search(typeFilter, originalType.get())) {
return nullptr;
}
return originalType;
} else {
auto left = filterOrComponents(o->left, typeFilter);
auto right = filterOrComponents(o->right, typeFilter);
if (left == nullptr) {
return right;
}
if (right == nullptr) {
return left;
}
if (left == o->left && right == o->right) {
return originalType;
}
return OrType::make_shared(left, right);
}
}

TypePtr lubDistributeOr(const GlobalState &gs, const TypePtr &t1, const TypePtr &t2) {
InlinedVector<TypePtr, 4> originalOrComponents;
InlinedVector<Type *, 4> typesConsumed;
auto *o1 = cast_type<OrType>(t1.get());
ENFORCE(o1 != nullptr);
fillInOrComponents(originalOrComponents, o1->left);
fillInOrComponents(originalOrComponents, o1->right);

for (auto &component : originalOrComponents) {
auto lubbed = Types::any(gs, component, t2);
if (lubbed.get() == component.get()) {
categoryCounterInc("lubDistributeOr.outcome", "t1");
return t1;
}
if (lubbed.get() == t2.get()) {
categoryCounterInc("lubDistributeOr.outcome", "consumedComponent");
typesConsumed.emplace_back(component.get());
}
TypePtr n1 = Types::any(gs, o1->left, t2);
if (n1.get() == o1->left.get()) {
categoryCounterInc("lubDistributeOr.outcome", "t1");
return t1;
}
TypePtr n2 = Types::any(gs, o1->right, t2);
if (n1.get() == t2.get()) {
categoryCounterInc("lubDistributeOr.outcome", "n2'");
return n2;
}
if (typesConsumed.empty()) {
categoryCounterInc("lubDistributeOr.outcome", "worst");
return OrType::make_shared(t1, underlying(t2));
if (n2.get() == o1->right.get()) {
categoryCounterInc("lubDistributeOr.outcome", "t1'");
return t1;
}
categoryCounterInc("lubDistributeOr.outcome", "consumedComponent");
// lub back everything except typesComsumed
auto remainingTypes = filterOrComponents(t1, typesConsumed);
if (remainingTypes == nullptr) {
return t2;
if (n2.get() == t2.get()) {
categoryCounterInc("lubDistributeOr.outcome", "n1'");
return n1;
}
if (Types::isSubType(gs, n1, n2)) {
categoryCounterInc("lubDistributeOr.outcome", "n2''");
return n2;
} else if (Types::isSubType(gs, n2, n1)) {
categoryCounterInc("lubDistributeOr.outcome", "n1'''");
return n1;
}
return OrType::make_shared(move(remainingTypes), underlying(t2));
categoryCounterInc("lubDistributeOr.outcome", "worst");
return OrType::make_shared(t1, underlying(t2)); // order matters for perf
}

TypePtr glbDistributeAnd(const GlobalState &gs, const TypePtr &t1, const TypePtr &t2) {
Expand Down
183 changes: 0 additions & 183 deletions test/testdata/infer/fast_big_lubs.rb

This file was deleted.

0 comments on commit 4fb3a6e

Please sign in to comment.