Skip to content

Commit 38fe069

Browse files
committed
Sema: Factor out clearScore() and replayScore()
1 parent c8db9f5 commit 38fe069

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5465,6 +5465,15 @@ class ConstraintSystem {
54655465
/// Primitive form of the above. Records a change in the trail.
54665466
void increaseScore(ScoreKind kind, unsigned value);
54675467

5468+
/// Apply the score from a partial solution. Records the change in the
5469+
/// trail.
5470+
void replayScore(const Score &score);
5471+
5472+
/// Temporarily zero out the score, and record this in the trail so that
5473+
/// we restore the score when the scope ends. Used when solving a
5474+
/// ConjunctionStep.
5475+
void clearScore();
5476+
54685477
/// Determine whether this solution is guaranteed to be worse than the best
54695478
/// solution found so far.
54705479
bool worseThanBestSolution() const;

lib/Sema/CSRanking.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,27 @@ void ConstraintSystem::increaseScore(ScoreKind kind,
146146
increaseScore(kind, value);
147147
}
148148

149+
void ConstraintSystem::replayScore(const Score &score) {
150+
if (solverState) {
151+
for (unsigned i = 0; i < NumScoreKinds; ++i) {
152+
if (unsigned value = score.Data[i])
153+
recordChange(
154+
SolverTrail::Change::IncreasedScore(ScoreKind(i), value));
155+
}
156+
}
157+
CurrentScore += score;
158+
}
159+
160+
void ConstraintSystem::clearScore() {
161+
for (unsigned i = 0; i < NumScoreKinds; ++i) {
162+
if (unsigned value = CurrentScore.Data[i]) {
163+
recordChange(
164+
SolverTrail::Change::DecreasedScore(ScoreKind(i), value));
165+
}
166+
}
167+
CurrentScore = Score();
168+
}
169+
149170
bool ConstraintSystem::worseThanBestSolution() const {
150171
if (getASTContext().TypeCheckerOpts.DisableConstraintSolverPerformanceHacks)
151172
return false;

lib/Sema/CSSolver.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,8 @@ Solution ConstraintSystem::finalize() {
271271

272272
void ConstraintSystem::replaySolution(const Solution &solution,
273273
bool shouldIncreaseScore) {
274-
if (shouldIncreaseScore) {
275-
// Update the score. We do this instead of operator+= because we
276-
// want to record the increments in the trail.
277-
auto solutionScore = solution.getFixedScore();
278-
for (unsigned i = 0; i < NumScoreKinds; ++i) {
279-
if (unsigned value = solutionScore.Data[i])
280-
increaseScore(ScoreKind(i), value);
281-
}
282-
}
274+
if (shouldIncreaseScore)
275+
replayScore(solution.getFixedScore());
283276

284277
// Assign fixed types to the type variables solved by this solution.
285278
for (auto binding : solution.typeBindings) {

lib/Sema/CSStep.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -885,13 +885,7 @@ bool ConjunctionStep::attempt(const ConjunctionElement &element) {
885885

886886
// Make sure that element is solved in isolation
887887
// by dropping all scoring information.
888-
for (unsigned i = 0; i < NumScoreKinds; ++i) {
889-
if (unsigned value = CS.CurrentScore.Data[i]) {
890-
CS.recordChange(
891-
SolverTrail::Change::DecreasedScore(ScoreKind(i), value));
892-
}
893-
}
894-
CS.CurrentScore = Score();
888+
CS.clearScore();
895889

896890
// Reset the scope counter to avoid "too complex" failures
897891
// when closure has a lot of elements in the body.

0 commit comments

Comments
 (0)