Skip to content

Commit da267bf

Browse files
committed
[Constraint system] Switch TypeVariables to a SetVector.
There were a few places where we wanted fast testing to see whether a particular type variable is currently of interest. Instead of building local hash tables in those places, keep type variables in a SetVector for efficient testing.
1 parent aaf4796 commit da267bf

File tree

8 files changed

+25
-33
lines changed

8 files changed

+25
-33
lines changed

lib/Sema/CSSolver.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Solution ConstraintSystem::finalize() {
7777
}
7878
}
7979

80-
for (auto tv : TypeVariables) {
80+
for (auto tv : getTypeVariables()) {
8181
if (getFixedType(tv))
8282
continue;
8383

@@ -95,7 +95,7 @@ Solution ConstraintSystem::finalize() {
9595
}
9696

9797
// For each of the type variables, get its fixed type.
98-
for (auto tv : TypeVariables) {
98+
for (auto tv : getTypeVariables()) {
9999
solution.typeBindings[tv] = simplifyType(tv)->reconstituteSugar(false);
100100
}
101101

@@ -198,12 +198,9 @@ void ConstraintSystem::applySolution(const Solution &solution) {
198198
CurrentScore += solution.getFixedScore();
199199

200200
// Assign fixed types to the type variables solved by this solution.
201-
llvm::SmallPtrSet<TypeVariableType *, 4>
202-
knownTypeVariables(TypeVariables.begin(), TypeVariables.end());
203201
for (auto binding : solution.typeBindings) {
204202
// If we haven't seen this type variable before, record it now.
205-
if (knownTypeVariables.insert(binding.first).second)
206-
TypeVariables.push_back(binding.first);
203+
addTypeVariable(binding.first);
207204

208205
// If we don't already have a fixed type for this type variable,
209206
// assign the fixed type from the solution.
@@ -475,7 +472,8 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
475472
ConstraintSystem::SolverScope::~SolverScope() {
476473
// Erase the end of various lists.
477474
cs.resolvedOverloadSets = resolvedOverloadSets;
478-
truncate(cs.TypeVariables, numTypeVariables);
475+
while (cs.TypeVariables.size() > numTypeVariables)
476+
cs.TypeVariables.pop_back();
479477

480478
// Restore bindings.
481479
cs.restoreTypeVariableBindings(cs.solverState->savedBindings.size() -

lib/Sema/CSStep.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ ComponentStep::Scope::Scope(ComponentStep &component)
3232
TypeVars = std::move(CS.TypeVariables);
3333

3434
for (auto *typeVar : component.TypeVars)
35-
CS.TypeVariables.push_back(typeVar);
35+
CS.addTypeVariable(typeVar);
3636

3737
auto &workList = CS.InactiveConstraints;
3838
workList.splice(workList.end(), *component.Constraints);
@@ -92,7 +92,7 @@ void SplitterStep::computeFollowupSteps(
9292
CG.optimize();
9393

9494
// Compute the connected components of the constraint graph.
95-
auto components = CG.computeConnectedComponents(CS.TypeVariables);
95+
auto components = CG.computeConnectedComponents(CS.getTypeVariables());
9696
unsigned numComponents = components.size();
9797
if (numComponents < 2) {
9898
steps.push_back(llvm::make_unique<ComponentStep>(
@@ -106,10 +106,10 @@ void SplitterStep::computeFollowupSteps(
106106
CG.verify();
107107

108108
log << "---Constraint graph---\n";
109-
CG.print(CS.TypeVariables, log);
109+
CG.print(CS.getTypeVariables(), log);
110110

111111
log << "---Connected components---\n";
112-
CG.printConnectedComponents(CS.TypeVariables, log);
112+
CG.printConnectedComponents(CS.getTypeVariables(), log);
113113
}
114114

115115
// Take the orphaned constraints, because they'll go into a component now.

lib/Sema/CSStep.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ class ComponentStep final : public SolverStep {
342342
ConstraintSystem &CS;
343343
ConstraintSystem::SolverScope *SolverScope;
344344

345-
std::vector<TypeVariableType *> TypeVars;
345+
SetVector<TypeVariableType *> TypeVars;
346346
ConstraintSystem::SolverScope *PrevPartialScope = nullptr;
347347

348348
// The component this scope is associated with.

lib/Sema/ConstraintGraph.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -378,18 +378,6 @@ llvm::TinyPtrVector<Constraint *> ConstraintGraph::gatherConstraints(
378378
llvm::function_ref<bool(Constraint *)> acceptConstraintFn) {
379379
llvm::TinyPtrVector<Constraint *> constraints;
380380

381-
// Local function to test whether the given type variable is in the current
382-
// interest set for the solver.
383-
SmallPtrSet<TypeVariableType *, 4> interestingTypeVars;
384-
auto isInterestingTypeVar = [&](TypeVariableType *typeVar) {
385-
if (interestingTypeVars.empty()) {
386-
interestingTypeVars.insert(CS.TypeVariables.begin(),
387-
CS.TypeVariables.end());
388-
}
389-
390-
return interestingTypeVars.count(typeVar) > 0;
391-
};
392-
393381
// Whether we should consider this constraint at all.
394382
auto rep = CS.getRepresentative(typeVar);
395383
auto shouldConsiderConstraint = [&](Constraint *constraint) {
@@ -399,7 +387,7 @@ llvm::TinyPtrVector<Constraint *> ConstraintGraph::gatherConstraints(
399387
if (constraint->getKind() == ConstraintKind::OneWayBind) {
400388
auto lhsTypeVar =
401389
constraint->getFirstType()->castTo<TypeVariableType>();
402-
if (!isInterestingTypeVar(lhsTypeVar))
390+
if (!CS.isActiveTypeVariable(lhsTypeVar))
403391
return false;
404392

405393
SmallVector<TypeVariableType *, 2> rhsTypeVars;
@@ -1295,7 +1283,7 @@ void ConstraintGraph::dump() {
12951283
void ConstraintGraph::dump(llvm::raw_ostream &out) {
12961284
llvm::SaveAndRestore<bool>
12971285
debug(CS.getASTContext().LangOpts.DebugConstraintSolver, true);
1298-
print(CS.TypeVariables, out);
1286+
print(CS.getTypeVariables(), out);
12991287
}
13001288

13011289
void ConstraintGraph::printConnectedComponents(
@@ -1334,7 +1322,7 @@ void ConstraintGraph::printConnectedComponents(
13341322
void ConstraintGraph::dumpConnectedComponents() {
13351323
llvm::SaveAndRestore<bool>
13361324
debug(CS.getASTContext().LangOpts.DebugConstraintSolver, true);
1337-
printConnectedComponents(CS.TypeVariables, llvm::dbgs());
1325+
printConnectedComponents(CS.getTypeVariables(), llvm::dbgs());
13381326
}
13391327

13401328
#pragma mark Verification of graph invariants

lib/Sema/ConstraintGraph.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ class ConstraintGraph {
337337
ConstraintSystem &CS;
338338

339339
/// The type variables in this graph, in stable order.
340-
SmallVector<TypeVariableType *, 4> TypeVariables;
340+
std::vector<TypeVariableType *> TypeVariables;
341341

342342
/// Constraints that are "orphaned" because they contain no type variables.
343343
SmallVector<Constraint *, 4> OrphanedConstraints;

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ bool ConstraintSystem::hasFreeTypeVariables() {
107107
}
108108

109109
void ConstraintSystem::addTypeVariable(TypeVariableType *typeVar) {
110-
TypeVariables.push_back(typeVar);
110+
TypeVariables.insert(typeVar);
111111

112112
// Notify the constraint graph.
113113
(void)CG[typeVar];

lib/Sema/ConstraintSystem.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ class ConstraintSystem {
10551055
/// solution it represents.
10561056
Score CurrentScore;
10571057

1058-
std::vector<TypeVariableType *> TypeVariables;
1058+
llvm::SetVector<TypeVariableType *> TypeVariables;
10591059

10601060
/// Maps expressions to types for choosing a favored overload
10611061
/// type in a disjunction constraint.
@@ -1766,9 +1766,15 @@ class ConstraintSystem {
17661766

17671767
/// Retrieve the set of active type variables.
17681768
ArrayRef<TypeVariableType *> getTypeVariables() const {
1769-
return TypeVariables;
1769+
return TypeVariables.getArrayRef();
17701770
}
1771-
1771+
1772+
/// Whether the given type variable is active in the constraint system at
1773+
/// the moment.
1774+
bool isActiveTypeVariable(TypeVariableType *typeVar) const {
1775+
return TypeVariables.count(typeVar) > 0;
1776+
}
1777+
17721778
TypeBase* getFavoredType(Expr *E) {
17731779
assert(E != nullptr);
17741780
return this->FavoredTypes[E];

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3664,7 +3664,7 @@ void ConstraintSystem::print(raw_ostream &out) {
36643664
}
36653665

36663666
out << "Type Variables:\n";
3667-
for (auto tv : TypeVariables) {
3667+
for (auto tv : getTypeVariables()) {
36683668
out.indent(2);
36693669
tv->getImpl().print(out);
36703670
if (tv->getImpl().canBindToLValue())

0 commit comments

Comments
 (0)