Skip to content

Commit e88c8dc

Browse files
committed
Sema: BindingSet::Defaults can just be a vector and not a map
The only place where we did a lookup, we also iterated over it anyway, and all remaining usages are simplified by downgrading it to a vector.
1 parent 01ca519 commit e88c8dc

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

include/swift/Sema/CSBindings.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ class BindingSet {
380380
/// before transitive ones.
381381
llvm::SmallMapVector<ProtocolDecl *, LiteralRequirement, 2> Literals;
382382

383-
llvm::SmallDenseMap<CanType, Constraint *, 2> Defaults;
383+
llvm::SmallVector<Constraint *, 2> Defaults;
384384

385385
/// The set of transitive protocol requirements inferred through
386386
/// subtype/conversion/equivalence relations with other type variables.
@@ -505,8 +505,8 @@ class BindingSet {
505505
return 1;
506506

507507
auto numDefaultable = llvm::count_if(
508-
Defaults, [](const std::pair<CanType, Constraint *> &entry) {
509-
return entry.second->getKind() == ConstraintKind::Defaultable;
508+
Defaults, [](Constraint *constraint) {
509+
return constraint->getKind() == ConstraintKind::Defaultable;
510510
});
511511

512512
// Short-circuit unviable checks if there are no defaultable bindings.
@@ -518,10 +518,12 @@ class BindingSet {
518518
auto unviable =
519519
llvm::count_if(Bindings, [&](const PotentialBinding &binding) {
520520
auto type = binding.BindingType->getCanonicalType();
521-
auto def = Defaults.find(type);
522-
return def != Defaults.end()
523-
? def->second->getKind() == ConstraintKind::Defaultable
524-
: false;
521+
for (auto *constraint : Defaults) {
522+
if (constraint->getSecondType()->isEqual(type)) {
523+
return constraint->getKind() == ConstraintKind::Defaultable;
524+
}
525+
}
526+
return false;
525527
});
526528

527529
assert(numDefaultable >= unviable);

lib/Sema/CSBindings.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -647,11 +647,11 @@ void BindingSet::inferTransitiveSupertypeBindings() {
647647
addLiteralRequirement(literal.second.getSource());
648648

649649
// Infer transitive defaults.
650-
for (const auto &def : bindings.Defaults) {
651-
if (def.getSecond()->getKind() == ConstraintKind::FallbackType)
650+
for (auto *def : bindings.Defaults) {
651+
if (def->getKind() == ConstraintKind::FallbackType)
652652
continue;
653653

654-
addDefault(def.second);
654+
addDefault(def);
655655
}
656656

657657
// TODO: We shouldn't need this in the future.
@@ -838,11 +838,13 @@ bool BindingSet::finalizeKeyPathBindings() {
838838
(keyPath->getParsedRoot() ||
839839
(fixedRootTy && !fixedRootTy->isTypeVariableOrMember()))) {
840840
auto fallback = llvm::find_if(Defaults, [](const auto &entry) {
841-
return entry.second->getKind() == ConstraintKind::FallbackType;
841+
return entry->getKind() == ConstraintKind::FallbackType;
842842
});
843843
assert(fallback != Defaults.end());
844844
updatedBindings.insert(
845-
{fallback->first, AllowedBindingKind::Exact, fallback->second});
845+
{(*fallback)->getSecondType(),
846+
AllowedBindingKind::Exact,
847+
*fallback});
846848
} else {
847849
updatedBindings.insert(PotentialBinding::forHole(
848850
TypeVar, CS.getConstraintLocator(
@@ -1106,10 +1108,10 @@ bool BindingSet::operator==(const BindingSet &other) {
11061108
if (Defaults.size() != other.Defaults.size())
11071109
return false;
11081110

1109-
for (auto pair : Defaults) {
1110-
auto found = other.Defaults.find(pair.first);
1111-
if (found == other.Defaults.end() ||
1112-
pair.second != found->second)
1111+
for (auto i : indices(Defaults)) {
1112+
auto *x = Defaults[i];
1113+
auto *y = other.Defaults[i];
1114+
if (x != y)
11131115
return false;
11141116
}
11151117

@@ -1271,8 +1273,7 @@ findInferableTypeVars(Type type,
12711273
}
12721274

12731275
void BindingSet::addDefault(Constraint *constraint) {
1274-
auto defaultTy = constraint->getSecondType();
1275-
Defaults.insert({defaultTy->getCanonicalType(), constraint});
1276+
Defaults.push_back(constraint);
12761277
}
12771278

12781279
bool LiteralRequirement::isCoveredBy(Type type, ConstraintSystem &CS) const {
@@ -2507,8 +2508,7 @@ void BindingSet::dump(llvm::raw_ostream &out, unsigned indent) const {
25072508
out << " [defaults: ";
25082509
interleave(
25092510
Defaults,
2510-
[&](const auto &entry) {
2511-
auto *constraint = entry.second;
2511+
[&](Constraint *constraint) {
25122512
auto defaultBinding =
25132513
PrintableBinding::exact(constraint->getSecondType());
25142514
defaultBinding.print(out, PO);

lib/Sema/ConstraintSystem.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5384,8 +5384,7 @@ TypeVarBindingProducer::TypeVarBindingProducer(const BindingSet &bindings)
53845384
if (viableBindings.size() == 1) {
53855385
addBinding(viableBindings.front());
53865386
} else {
5387-
for (const auto &entry : bindings.Defaults) {
5388-
auto *constraint = entry.second;
5387+
for (auto *constraint : bindings.Defaults) {
53895388
Bindings.push_back(getDefaultBinding(constraint));
53905389
}
53915390
}
@@ -5423,8 +5422,7 @@ TypeVarBindingProducer::TypeVarBindingProducer(const BindingSet &bindings)
54235422
{
54245423
bool noBindings = Bindings.empty();
54255424

5426-
for (const auto &entry : bindings.Defaults) {
5427-
auto *constraint = entry.second;
5425+
for (auto *constraint : bindings.Defaults) {
54285426
if (noBindings) {
54295427
// If there are no direct or transitive bindings to attempt
54305428
// let's add defaults to the list right away.

0 commit comments

Comments
 (0)