Skip to content

Commit cfcc852

Browse files
committed
[ConstraintSystem] Convert DefaultConstraints to a set vector to avoid duplicates
It's always been the case that partial solutions introduce some storage duplication when applied back to the constraint system to form a more complete solution with outer context, but the constraint systems used to be small before introduction of result builders (and now multi-statement inference), which make the duplication more visible.
1 parent e6a98d2 commit cfcc852

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2450,7 +2450,7 @@ class ConstraintSystem {
24502450
llvm::SmallMapVector<ASTNode, SmallVector<AppliedPropertyWrapper, 2>, 4> appliedPropertyWrappers;
24512451

24522452
/// The locators of \c Defaultable constraints whose defaults were used.
2453-
std::vector<ConstraintLocator *> DefaultedConstraints;
2453+
llvm::SetVector<ConstraintLocator *> DefaultedConstraints;
24542454

24552455
/// A cache that stores the @dynamicCallable required methods implemented by
24562456
/// types.

lib/Sema/CSBindings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1983,7 +1983,7 @@ bool TypeVariableBinding::attempt(ConstraintSystem &cs) const {
19831983

19841984
// If this was from a defaultable binding note that.
19851985
if (Binding.isDefaultableBinding()) {
1986-
cs.DefaultedConstraints.push_back(srcLocator);
1986+
cs.DefaultedConstraints.insert(srcLocator);
19871987

19881988
if (type->isPlaceholder() && reportHole())
19891989
return true;

lib/Sema/CSSolver.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,7 @@ void ConstraintSystem::applySolution(const Solution &solution) {
259259
}
260260

261261
// Register the defaulted type variables.
262-
DefaultedConstraints.insert(DefaultedConstraints.end(),
263-
solution.DefaultedConstraints.begin(),
262+
DefaultedConstraints.insert(solution.DefaultedConstraints.begin(),
264263
solution.DefaultedConstraints.end());
265264

266265
// Add the node types back.
@@ -406,6 +405,12 @@ void truncate(llvm::SmallMapVector<K, V, N> &map, unsigned newSize) {
406405
map.pop_back();
407406
}
408407

408+
template <typename V>
409+
void truncate(llvm::SetVector<V> &vector, unsigned newSize) {
410+
while (vector.size() > newSize)
411+
vector.pop_back();
412+
}
413+
409414
} // end anonymous namespace
410415

411416
ConstraintSystem::SolverState::SolverState(
@@ -542,8 +547,7 @@ ConstraintSystem::SolverScope::~SolverScope() {
542547
return;
543548

544549
// Erase the end of various lists.
545-
while (cs.TypeVariables.size() > numTypeVariables)
546-
cs.TypeVariables.pop_back();
550+
truncate(cs.TypeVariables, numTypeVariables);
547551

548552
truncate(cs.ResolvedOverloads, numResolvedOverloads);
549553

0 commit comments

Comments
 (0)