Skip to content

Commit b0f91ac

Browse files
authored
Merge pull request #68226 from xedin/experimental-no-stack-solution
[ConstraintSystem] Remove all stack allocation from `Solution`
2 parents bc28171 + 26e9a90 commit b0f91ac

File tree

3 files changed

+50
-17
lines changed

3 files changed

+50
-17
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,11 +1458,11 @@ class Solution {
14581458

14591459
/// The list of fixes that need to be applied to the initial expression
14601460
/// to make the solution work.
1461-
llvm::SmallVector<ConstraintFix *, 4> Fixes;
1461+
std::vector<ConstraintFix *> Fixes;
14621462

14631463
/// For locators associated with call expressions, the trailing closure
14641464
/// matching rule and parameter bindings that were applied.
1465-
llvm::SmallMapVector<ConstraintLocator *, MatchCallArgumentResult, 4>
1465+
llvm::MapVector<ConstraintLocator *, MatchCallArgumentResult>
14661466
argumentMatchingChoices;
14671467

14681468
/// The set of disjunction choices used to arrive at this solution,
@@ -1485,14 +1485,14 @@ class Solution {
14851485
PackExpansionEnvironments;
14861486

14871487
/// The pack expansion environment that can open a given pack element.
1488-
llvm::SmallMapVector<PackElementExpr *, PackExpansionExpr *, 2>
1488+
llvm::MapVector<PackElementExpr *, PackExpansionExpr *>
14891489
PackEnvironments;
14901490

14911491
/// The locators of \c Defaultable constraints whose defaults were used.
1492-
llvm::SmallPtrSet<ConstraintLocator *, 2> DefaultedConstraints;
1492+
llvm::DenseSet<ConstraintLocator *> DefaultedConstraints;
14931493

14941494
/// Implicit value conversions applied for a given locator.
1495-
SmallVector<std::pair<ConstraintLocator *, ConversionRestrictionKind>, 2>
1495+
std::vector<std::pair<ConstraintLocator *, ConversionRestrictionKind>>
14961496
ImplicitValueConversions;
14971497

14981498
/// The node -> type mappings introduced by this solution.
@@ -1517,19 +1517,19 @@ class Solution {
15171517

15181518
/// Maps case label items to information tracked about them as they are
15191519
/// being solved.
1520-
llvm::SmallMapVector<const CaseLabelItem *, CaseLabelItemInfo, 4>
1520+
llvm::MapVector<const CaseLabelItem *, CaseLabelItemInfo>
15211521
caseLabelItems;
15221522

15231523
/// A map of expressions to the ExprPatterns that they are being solved as
15241524
/// a part of.
1525-
llvm::SmallMapVector<Expr *, ExprPattern *, 2> exprPatterns;
1525+
llvm::MapVector<Expr *, ExprPattern *> exprPatterns;
15261526

15271527
/// The set of parameters that have been inferred to be 'isolated'.
1528-
llvm::SmallVector<ParamDecl *, 2> isolatedParams;
1528+
std::vector<ParamDecl *> isolatedParams;
15291529

15301530
/// The set of closures that have been inferred to be "isolated by
15311531
/// preconcurrency".
1532-
llvm::SmallVector<const ClosureExpr *, 2> preconcurrencyClosures;
1532+
std::vector<const ClosureExpr *> preconcurrencyClosures;
15331533

15341534
/// The set of functions that have been transformed by a result builder.
15351535
llvm::MapVector<AnyFunctionRef, AppliedBuilderTransform>

lib/Sema/CSSolver.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ Solution ConstraintSystem::finalize() {
131131
if (solverState && solverState->PartialSolutionScope) {
132132
firstFixIndex = solverState->PartialSolutionScope->numFixes;
133133
}
134-
solution.Fixes.append(Fixes.begin() + firstFixIndex, Fixes.end());
134+
135+
for (const auto &fix :
136+
llvm::make_range(Fixes.begin() + firstFixIndex, Fixes.end()))
137+
solution.Fixes.push_back(fix);
135138

136139
// Remember all the disjunction choices we made.
137140
for (auto &choice : DisjunctionChoices) {
@@ -199,11 +202,18 @@ Solution ConstraintSystem::finalize() {
199202
}
200203

201204
solution.targets = targets;
202-
solution.caseLabelItems = caseLabelItems;
203-
solution.exprPatterns = exprPatterns;
204-
solution.isolatedParams.append(isolatedParams.begin(), isolatedParams.end());
205-
solution.preconcurrencyClosures.append(preconcurrencyClosures.begin(),
206-
preconcurrencyClosures.end());
205+
206+
for (const auto &item : caseLabelItems)
207+
solution.caseLabelItems.insert(item);
208+
209+
for (const auto &pattern : exprPatterns)
210+
solution.exprPatterns.insert(pattern);
211+
212+
for (const auto &param : isolatedParams)
213+
solution.isolatedParams.push_back(param);
214+
215+
for (const auto &closure : preconcurrencyClosures)
216+
solution.preconcurrencyClosures.push_back(closure);
207217

208218
for (const auto &transformed : resultBuilderTransformed) {
209219
solution.resultBuilderTransformed.insert(transformed);
@@ -231,7 +241,8 @@ Solution ConstraintSystem::finalize() {
231241
solution.PackExpansionEnvironments.insert(env);
232242
}
233243

234-
solution.PackEnvironments = PackEnvironments;
244+
for (const auto &packEnv : PackEnvironments)
245+
solution.PackEnvironments.insert(packEnv);
235246

236247
return solution;
237248
}

lib/Sema/ConstraintSystem.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4186,13 +4186,35 @@ Type Solution::simplifyTypeForCodeCompletion(Type Ty) const {
41864186
return Ty->getRValueType();
41874187
}
41884188

4189+
template <typename T>
4190+
static inline size_t size_in_bytes(const T &x) {
4191+
return (x.size() * (sizeof(typename T::key_type) + sizeof(unsigned))) +
4192+
(x.size() * (sizeof(typename T::value_type)));
4193+
}
4194+
41894195
size_t Solution::getTotalMemory() const {
41904196
return sizeof(*this) + typeBindings.getMemorySize() +
41914197
overloadChoices.getMemorySize() +
41924198
ConstraintRestrictions.getMemorySize() +
4193-
llvm::capacity_in_bytes(Fixes) + DisjunctionChoices.getMemorySize() +
4199+
(Fixes.size() * sizeof(void *)) + DisjunctionChoices.getMemorySize() +
41944200
OpenedTypes.getMemorySize() + OpenedExistentialTypes.getMemorySize() +
4201+
OpenedPackExpansionTypes.getMemorySize() +
4202+
PackExpansionEnvironments.getMemorySize() +
4203+
size_in_bytes(PackEnvironments) +
41954204
(DefaultedConstraints.size() * sizeof(void *)) +
4205+
ImplicitCallAsFunctionRoots.getMemorySize() +
4206+
nodeTypes.getMemorySize() +
4207+
keyPathComponentTypes.getMemorySize() +
4208+
size_in_bytes(KeyPaths) +
4209+
(contextualTypes.size() * sizeof(ASTNode)) +
4210+
size_in_bytes(targets) +
4211+
size_in_bytes(caseLabelItems) +
4212+
size_in_bytes(exprPatterns) +
4213+
(isolatedParams.size() * sizeof(void *)) +
4214+
(preconcurrencyClosures.size() * sizeof(void *)) +
4215+
size_in_bytes(resultBuilderTransformed) +
4216+
size_in_bytes(appliedPropertyWrappers) +
4217+
size_in_bytes(argumentLists) +
41964218
ImplicitCallAsFunctionRoots.getMemorySize();
41974219
}
41984220

0 commit comments

Comments
 (0)