Skip to content

Commit 0cc0a3a

Browse files
committed
[ConstraintGraph] Optimize edge contraction
Previously on every step solver would iterate over all constraints in attempt to find `BindParam` which could be contracted. Instead of doing that, let's take advantage of the fact that all (participating) closures are recorded in the constraint system during constraint generation, so it should be possible to check either outer parameter types are contractable with their inner uses.
1 parent 03e0c4a commit 0cc0a3a

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

lib/Sema/ConstraintGraph.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,12 +1312,36 @@ static bool shouldContractEdge(ConstraintKind kind) {
13121312
}
13131313

13141314
bool ConstraintGraph::contractEdges() {
1315+
// Current constraint system doesn't have any closure expressions
1316+
// associated with it so there is nothing to here.
1317+
if (CS.ClosureTypes.empty())
1318+
return false;
1319+
13151320
SmallVector<Constraint *, 16> constraints;
1316-
CS.findConstraints(constraints, [&](const Constraint &constraint) {
1317-
// Track how many constraints did contraction algorithm iterated over.
1318-
incrementConstraintsPerContractionCounter();
1319-
return shouldContractEdge(constraint.getKind());
1320-
});
1321+
for (const auto &closure : CS.ClosureTypes) {
1322+
for (const auto &param : closure.second->getParams()) {
1323+
auto paramTy = param.getPlainType()->getAs<TypeVariableType>();
1324+
if (!paramTy)
1325+
continue;
1326+
1327+
// This closure is not currently in scope.
1328+
if (!CS.TypeVariables.count(paramTy))
1329+
break;
1330+
1331+
// Nothing to contract here since outside parameter
1332+
// is already bound to a concrete type.
1333+
if (CS.getFixedType(paramTy))
1334+
continue;
1335+
1336+
for (auto *constraint : (*this)[paramTy].getConstraints()) {
1337+
// Track how many constraints did contraction algorithm iterated over.
1338+
incrementConstraintsPerContractionCounter();
1339+
1340+
if (shouldContractEdge(constraint->getKind()))
1341+
constraints.push_back(constraint);
1342+
}
1343+
}
1344+
}
13211345

13221346
bool didContractEdges = false;
13231347
for (auto *constraint : constraints) {

0 commit comments

Comments
 (0)