Skip to content

Commit ca12185

Browse files
authored
Merge pull request #85967 from slavapestov/dont-copy-that-floppy
Sema: Avoid copying BindingSets
2 parents e186e5e + 56baeac commit ca12185

File tree

11 files changed

+28
-33
lines changed

11 files changed

+28
-33
lines changed

include/swift/Sema/CSBindings.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,10 @@ class BindingSet {
385385
BindingSet(ConstraintSystem &CS, TypeVariableType *TypeVar,
386386
const PotentialBindings &info);
387387

388+
BindingSet(BindingSet &&other) = default;
389+
390+
BindingSet(const BindingSet &other) = delete;
391+
388392
ConstraintSystem &getConstraintSystem() const { return CS; }
389393

390394
TypeVariableType *getTypeVariable() const { return TypeVar; }
@@ -568,8 +572,11 @@ class BindingSet {
568572
/// requirements down the subtype or equivalence chain.
569573
void inferTransitiveProtocolRequirements();
570574

571-
/// Check whether the given binding set covers any of the
572-
/// literal protocols associated with this type variable.
575+
/// Check whether the given binding set covers any of the literal protocols
576+
/// associated with this type variable. The idea is that if a type variable
577+
/// has a binding like Int and also it has a conformance requirement to
578+
/// ExpressibleByIntegerLitral, we can avoid attempting the default type of
579+
/// that literal literal if we already attempted Int.
573580
void determineLiteralCoverage();
574581

575582
/// Finalize binding computation for key path type variables.

include/swift/Sema/ConstraintSystem.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ Type typeCheckParameterDefault(Expr *&, DeclContext *, Type, bool, bool);
9595

9696
} // end namespace swift
9797

98-
/// Allocate memory within the given constraint system.
99-
void *operator new(size_t bytes, swift::constraints::ConstraintSystem& cs,
100-
size_t alignment = 8);
101-
10298
namespace swift {
10399

104100
/// Specify how we handle the binding of underconstrained (free) type variables
@@ -5270,7 +5266,7 @@ class ConstraintSystem {
52705266

52715267
/// Determine whether given type variable with its set of bindings is viable
52725268
/// to be attempted on the next step of the solver.
5273-
std::optional<BindingSet> determineBestBindings(
5269+
const BindingSet *determineBestBindings(
52745270
llvm::function_ref<void(const BindingSet &)> onCandidate);
52755271

52765272
/// Get bindings for the given type variable based on current
@@ -6200,7 +6196,7 @@ class TypeVarBindingProducer : public BindingProducer<TypeVariableBinding> {
62006196
public:
62016197
using Element = TypeVariableBinding;
62026198

6203-
TypeVarBindingProducer(BindingSet &bindings);
6199+
TypeVarBindingProducer(const BindingSet &bindings);
62046200

62056201
/// Retrieve a set of bindings available in the current state.
62066202
ArrayRef<Binding> getCurrentBindings() const { return Bindings; }

lib/Sema/CSBindings.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ bool BindingSet::operator<(const BindingSet &other) {
11801180
return isPotentiallyIncomplete() < other.isPotentiallyIncomplete();
11811181
}
11821182

1183-
std::optional<BindingSet> ConstraintSystem::determineBestBindings(
1183+
const BindingSet *ConstraintSystem::determineBestBindings(
11841184
llvm::function_ref<void(const BindingSet &)> onCandidate) {
11851185
// Look for potential type variable bindings.
11861186
BindingSet *bestBindings = nullptr;
@@ -1238,10 +1238,7 @@ std::optional<BindingSet> ConstraintSystem::determineBestBindings(
12381238
bestBindings = &bindings;
12391239
}
12401240

1241-
if (!bestBindings)
1242-
return std::nullopt;
1243-
1244-
return std::optional(*bestBindings);
1241+
return bestBindings;
12451242
}
12461243

12471244
/// Find the set of type variables that are inferable from the given type.

lib/Sema/CSStep.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ StepResult ComponentStep::take(bool prevFailed) {
263263
SmallString<64> potentialBindings;
264264
llvm::raw_svector_ostream bos(potentialBindings);
265265

266-
auto bestBindings = CS.determineBestBindings([&](const BindingSet &bindings) {
266+
const auto *bestBindings = CS.determineBestBindings([&](const BindingSet &bindings) {
267267
if (CS.isDebugMode() && bindings.hasViableBindings()) {
268268
bos.indent(CS.solverState->getCurrentIndent() + 2);
269269
bos << "(";

lib/Sema/CSStep.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ class TypeVariableStep final : public BindingStep<TypeVarBindingProducer> {
540540
bool SawFirstLiteralConstraint = false;
541541

542542
public:
543-
TypeVariableStep(BindingContainer &bindings,
543+
TypeVariableStep(const BindingContainer &bindings,
544544
SmallVectorImpl<Solution> &solutions)
545545
: BindingStep(bindings.getConstraintSystem(), {bindings}, solutions),
546546
TypeVar(bindings.getTypeVariable()) {}

lib/Sema/Constraint.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,7 @@ Constraint::getTrailingClosureMatching() const {
11411141

11421142
void *Constraint::operator new(size_t bytes, ConstraintSystem& cs,
11431143
size_t alignment) {
1144-
return ::operator new (bytes, cs, alignment);
1144+
return cs.getAllocator().Allocate(bytes, alignment);
11451145
}
11461146

11471147
// FIXME: Perhaps we should store the Constraint -> PreparedOverload mapping

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5323,7 +5323,7 @@ ConstraintSystem::inferKeyPathLiteralCapability(KeyPathExpr *keyPath) {
53235323
return success(mutability, isSendable);
53245324
}
53255325

5326-
TypeVarBindingProducer::TypeVarBindingProducer(BindingSet &bindings)
5326+
TypeVarBindingProducer::TypeVarBindingProducer(const BindingSet &bindings)
53275327
: BindingProducer(bindings.getConstraintSystem(),
53285328
bindings.getTypeVariable()->getImpl().getLocator()),
53295329
TypeVar(bindings.getTypeVariable()), CanBeNil(bindings.canBeNil()) {

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,6 @@ bool TypeVariableType::Implementation::isTernary() const {
214214
return locator && locator->directlyAt<TernaryExpr>();
215215
}
216216

217-
void *operator new(size_t bytes, ConstraintSystem& cs,
218-
size_t alignment) {
219-
return cs.getAllocator().Allocate(bytes, alignment);
220-
}
221-
222217
bool constraints::computeTupleShuffle(TupleType *fromTuple,
223218
TupleType *toTuple,
224219
SmallVectorImpl<unsigned> &sources) {

unittests/Sema/BindingInferenceTests.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ TEST_F(SemaTest, TestTransitiveProtocolInference) {
196196
cs.getConstraintLocator({}, LocatorPathElt::ContextualType(
197197
CTP_Initialization)));
198198

199-
auto bindings = inferBindings(cs, typeVar);
199+
auto &bindings = inferBindings(cs, typeVar);
200200
ASSERT_TRUE(bindings.getConformanceRequirements().empty());
201201
ASSERT_TRUE(bool(bindings.TransitiveProtocols));
202202
verifyProtocolInferenceResults(*bindings.TransitiveProtocols,
@@ -218,7 +218,7 @@ TEST_F(SemaTest, TestTransitiveProtocolInference) {
218218
cs.addConstraint(ConstraintKind::Conversion, typeVar, GPT1,
219219
cs.getConstraintLocator({}));
220220

221-
auto bindings = inferBindings(cs, typeVar);
221+
auto &bindings = inferBindings(cs, typeVar);
222222
ASSERT_TRUE(bindings.getConformanceRequirements().empty());
223223
ASSERT_TRUE(bool(bindings.TransitiveProtocols));
224224
verifyProtocolInferenceResults(*bindings.TransitiveProtocols,
@@ -281,10 +281,10 @@ TEST_F(SemaTest, TestComplexTransitiveProtocolInference) {
281281
cs.addConstraint(ConstraintKind::Equal, typeVar1, typeVar5, nilLocator);
282282
cs.addConstraint(ConstraintKind::Conversion, typeVar5, typeVar6, nilLocator);
283283

284-
auto bindingsForT1 = inferBindings(cs, typeVar1);
285-
auto bindingsForT2 = inferBindings(cs, typeVar2);
286-
auto bindingsForT3 = inferBindings(cs, typeVar3);
287-
auto bindingsForT5 = inferBindings(cs, typeVar5);
284+
auto &bindingsForT1 = inferBindings(cs, typeVar1);
285+
auto &bindingsForT2 = inferBindings(cs, typeVar2);
286+
auto &bindingsForT3 = inferBindings(cs, typeVar3);
287+
auto &bindingsForT5 = inferBindings(cs, typeVar5);
288288

289289
ASSERT_TRUE(bool(bindingsForT1.TransitiveProtocols));
290290
verifyProtocolInferenceResults(*bindingsForT1.TransitiveProtocols,
@@ -335,7 +335,7 @@ TEST_F(SemaTest, TestTransitiveProtocolInferenceThroughEquivalenceChains) {
335335
cs.addConstraint(ConstraintKind::ConformsTo, typeVar2, protocolTy0, nilLocator);
336336
cs.addConstraint(ConstraintKind::ConformsTo, typeVar3, protocolTy1, nilLocator);
337337

338-
auto bindings = inferBindings(cs, typeVar0);
338+
auto &bindings = inferBindings(cs, typeVar0);
339339

340340
ASSERT_TRUE(bool(bindings.TransitiveProtocols));
341341
verifyProtocolInferenceResults(*bindings.TransitiveProtocols,

unittests/Sema/SemaFixture.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ ProtocolType *SemaTest::createProtocol(llvm::StringRef protocolName,
124124
return ProtocolType::get(PD, parent, Context);
125125
}
126126

127-
BindingSet SemaTest::inferBindings(ConstraintSystem &cs,
128-
TypeVariableType *typeVar) {
127+
const BindingSet &SemaTest::inferBindings(ConstraintSystem &cs,
128+
TypeVariableType *typeVar) {
129129
for (auto *typeVar : cs.getTypeVariables()) {
130130
auto &node = cs.getConstraintGraph()[typeVar];
131131
node.resetBindingSet();

0 commit comments

Comments
 (0)