Skip to content

Commit 5530298

Browse files
committed
[Constraint solver] Measure “expression too complex” only at decision points.
Polling the timers used to measure “expression too complex” is not *that* cheap. When the constraint solver is doing a lot of work, the overhead can add up. Reduce the number of places where we poll the timer down to main decision points (i.e., before exploring a disjunction or binding a new type variable), taking the timing overhead from ~3.7% —> ~1.7%.
1 parent 0af09d3 commit 5530298

File tree

1 file changed

+9
-26
lines changed

1 file changed

+9
-26
lines changed

lib/Sema/CSSolver.cpp

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -562,10 +562,6 @@ bool ConstraintSystem::tryTypeVariableBindings(
562562
auto &tc = getTypeChecker();
563563
++solverState->NumTypeVariablesBound;
564564

565-
// If we've already explored a lot of potential solutions, bail.
566-
if (getExpressionTooComplex(solutions))
567-
return true;
568-
569565
for (unsigned tryCount = 0; !anySolved && !bindings.empty(); ++tryCount) {
570566
// Try each of the bindings in turn.
571567
++solverState->NumTypeVariableBindings;
@@ -1459,15 +1455,7 @@ ConstraintSystem::solveImpl(Expr *&expr,
14591455

14601456
// If there are no solutions let's mark system as unsolved,
14611457
// and solved otherwise even if there are multiple solutions still present.
1462-
1463-
// There was a Swift 3 bug that allowed us to return Solved if we
1464-
// had found at least one solution before deciding an expression was
1465-
// "too complex". Maintain that behavior, but for Swift > 3 return
1466-
// Unsolved in these cases.
1467-
auto tooComplex = getExpressionTooComplex(solutions);
1468-
auto unsolved = tooComplex || solutions.empty();
1469-
1470-
return unsolved ? SolutionKind::Unsolved : SolutionKind::Solved;
1458+
return solutions.empty() ? SolutionKind::Unsolved : SolutionKind::Solved;
14711459
}
14721460

14731461
bool ConstraintSystem::solve(Expr *const expr,
@@ -1498,8 +1486,8 @@ bool ConstraintSystem::solve(Expr *const expr,
14981486
if (!retainAllSolutions())
14991487
filterSolutions(solutions, state.ExprWeights);
15001488

1501-
// We fail if there is no solution.
1502-
return solutions.empty();
1489+
// We fail if there is no solution or the expression was too complex.
1490+
return solutions.empty() || getExpressionTooComplex(solutions);
15031491
}
15041492

15051493
bool ConstraintSystem::solveRec(SmallVectorImpl<Solution> &solutions,
@@ -2042,10 +2030,6 @@ bool ConstraintSystem::solveForDisjunctionChoices(
20422030
break;
20432031
}
20442032

2045-
// If the expression was deemed "too complex", stop now and salvage.
2046-
if (getExpressionTooComplex(solutions))
2047-
break;
2048-
20492033
// Try to solve the system with this option in the disjunction.
20502034
SolverScope scope(*this);
20512035
++solverState->NumDisjunctionTerms;
@@ -2116,6 +2100,10 @@ bool ConstraintSystem::solveSimplified(
21162100

21172101
auto bestBindings = determineBestBindings();
21182102

2103+
// If we've already explored a lot of potential solutions, bail.
2104+
if (getExpressionTooComplex(solutions))
2105+
return true;
2106+
21192107
// If we have a binding that does not involve type variables, and is
21202108
// not fully bound, or we have no disjunction to attempt instead,
21212109
// go ahead and try the bindings for this type variable.
@@ -2127,13 +2115,8 @@ bool ConstraintSystem::solveSimplified(
21272115
}
21282116

21292117
if (disjunction) {
2130-
bool foundSolution = solveForDisjunctionChoices(disjunction, solutions,
2131-
allowFreeTypeVariables);
2132-
2133-
// If we are exiting due to an expression that is too complex, do
2134-
// not allow our caller to continue as if we have been successful.
2135-
auto tooComplex = getExpressionTooComplex(solutions);
2136-
return tooComplex || !foundSolution;
2118+
return !solveForDisjunctionChoices(disjunction, solutions,
2119+
allowFreeTypeVariables);
21372120
}
21382121

21392122
// If there are no disjunctions we can't solve this system unless we have

0 commit comments

Comments
 (0)