Skip to content

Commit 109a33c

Browse files
authored
Merge pull request #36220 from xedin/optimize-edge-contraction
[ConstraintGraph] Optimize edge contraction
2 parents 2ee7a32 + b519695 commit 109a33c

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

lib/Sema/ConstraintGraph.cpp

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,26 +1314,49 @@ ConstraintGraph::computeConnectedComponents(
13141314
return cc.getComponents();
13151315
}
13161316

1317+
bool ConstraintGraph::contractEdges() {
1318+
// Current constraint system doesn't have any closure expressions
1319+
// associated with it so there is nothing to here.
1320+
if (CS.ClosureTypes.empty())
1321+
return false;
13171322

1318-
/// For a given constraint kind, decide if we should attempt to eliminate its
1319-
/// edge in the graph.
1320-
static bool shouldContractEdge(ConstraintKind kind) {
1321-
switch (kind) {
1322-
case ConstraintKind::BindParam:
1323-
return true;
1323+
// For a given constraint kind, decide if we should attempt to eliminate its
1324+
// edge in the graph.
1325+
auto shouldContractEdge = [](ConstraintKind kind) {
1326+
switch (kind) {
1327+
case ConstraintKind::BindParam:
1328+
return true;
13241329

1325-
default:
1326-
return false;
1327-
}
1328-
}
1330+
default:
1331+
return false;
1332+
}
1333+
};
13291334

1330-
bool ConstraintGraph::contractEdges() {
13311335
SmallVector<Constraint *, 16> constraints;
1332-
CS.findConstraints(constraints, [&](const Constraint &constraint) {
1333-
// Track how many constraints did contraction algorithm iterated over.
1334-
incrementConstraintsPerContractionCounter();
1335-
return shouldContractEdge(constraint.getKind());
1336-
});
1336+
for (const auto &closure : CS.ClosureTypes) {
1337+
for (const auto &param : closure.second->getParams()) {
1338+
auto paramTy = param.getPlainType()->getAs<TypeVariableType>();
1339+
if (!paramTy)
1340+
continue;
1341+
1342+
// This closure is not currently in scope.
1343+
if (!CS.TypeVariables.count(paramTy))
1344+
break;
1345+
1346+
// Nothing to contract here since outside parameter
1347+
// is already bound to a concrete type.
1348+
if (CS.getFixedType(paramTy))
1349+
continue;
1350+
1351+
for (auto *constraint : (*this)[paramTy].getConstraints()) {
1352+
// Track how many constraints did contraction algorithm iterated over.
1353+
incrementConstraintsPerContractionCounter();
1354+
1355+
if (shouldContractEdge(constraint->getKind()))
1356+
constraints.push_back(constraint);
1357+
}
1358+
}
1359+
}
13371360

13381361
bool didContractEdges = false;
13391362
for (auto *constraint : constraints) {

validation-test/Sema/type_checker_perf/fast/rdar29358447.swift.gyb

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)