Skip to content

Commit 62502ef

Browse files
committed
[Constraint graph] Restrict connected components to requested type variables.
The API of the connected-components algorithm asks clients to provide the set of type variables of interest. However, the connected components algorithm itself was operating across the entire set of type variables, then narrowing the result down to the type variables of interest. Instead, only perform connected components on those type variables of interest, so that we are only doing work proportional to the subgraph we're working in.
1 parent e6e6307 commit 62502ef

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

lib/Sema/ConstraintGraph.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,6 @@ unsigned ConstraintGraph::computeConnectedComponents(
526526
// Track those type variables that the caller cares about.
527527
llvm::SmallPtrSet<TypeVariableType *, 4> typeVarSubset(typeVars.begin(),
528528
typeVars.end());
529-
typeVars.clear();
530529

531530
// Initialize the components with component == # of type variables,
532531
// a sentinel value indicating that we have yet to assign a component to
@@ -538,9 +537,7 @@ unsigned ConstraintGraph::computeConnectedComponents(
538537
// what component it is in.
539538
llvm::DenseSet<Constraint *> visitedConstraints;
540539
unsigned numComponents = 0;
541-
for (unsigned i = 0; i != numTypeVariables; ++i) {
542-
auto typeVar = TypeVariables[i];
543-
540+
for (auto typeVar : typeVars) {
544541
// Look up the node for this type variable.
545542
auto nodeAndIndex = lookupNode(typeVar);
546543

@@ -576,6 +573,10 @@ unsigned ConstraintGraph::computeConnectedComponents(
576573
// are the only components and type variables we want to report.
577574
SmallVector<bool, 4> componentHasUnboundTypeVar(numComponents, false);
578575
for (unsigned i = 0; i != numTypeVariables; ++i) {
576+
// If we didn't look at this type variable, there's nothing to do.
577+
if (components[i] == numTypeVariables)
578+
continue;
579+
579580
// If this type variable has a fixed type, skip it.
580581
if (CS.getFixedType(TypeVariables[i]))
581582
continue;
@@ -592,6 +593,10 @@ unsigned ConstraintGraph::computeConnectedComponents(
592593
SmallVector<unsigned, 4> componentRenumbering(numComponents, 0);
593594
numComponents = 0;
594595
for (unsigned i = 0, n = componentHasUnboundTypeVar.size(); i != n; ++i) {
596+
// If we didn't look at this type variable, there's nothing to do.
597+
if (components[i] == numTypeVariables)
598+
continue;
599+
595600
// Skip components that have no unbound type variables.
596601
if (!componentHasUnboundTypeVar[i])
597602
continue;
@@ -601,8 +606,13 @@ unsigned ConstraintGraph::computeConnectedComponents(
601606

602607
// Copy over the type variables in the live components and remap
603608
// component numbers.
609+
typeVars.clear();
604610
unsigned outIndex = 0;
605611
for (unsigned i = 0, n = TypeVariables.size(); i != n; ++i) {
612+
// If we didn't look at this type variable, there's nothing to do.
613+
if (components[i] == numTypeVariables)
614+
continue;
615+
606616
// Skip type variables in dead components.
607617
if (!componentHasUnboundTypeVar[components[i]])
608618
continue;

0 commit comments

Comments
 (0)