Skip to content

Commit 1b63b68

Browse files
committed
[AST] Use TinyPtrVector for the list of overridden declarations.
TinyPtrVector is a more-space-efficient SmallVector<_, 1>. Use it.
1 parent 0966cd9 commit 1b63b68

File tree

5 files changed

+30
-27
lines changed

5 files changed

+30
-27
lines changed

include/swift/AST/Decl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2482,7 +2482,7 @@ class ValueDecl : public Decl {
24822482
ValueDecl *getOverriddenDecl() const;
24832483

24842484
/// Retrieve the declarations that this declaration overrides, if any.
2485-
SmallVector<ValueDecl *, 1> getOverriddenDecls() const;
2485+
llvm::TinyPtrVector<ValueDecl *> getOverriddenDecls() const;
24862486

24872487
/// Set the declaration that this declaration overrides.
24882488
void setOverriddenDecl(ValueDecl *overridden) {
@@ -2901,7 +2901,7 @@ class AssociatedTypeDecl : public AbstractTypeParamDecl {
29012901

29022902
/// Retrieve the set of associated types overridden by this associated
29032903
/// type.
2904-
SmallVector<AssociatedTypeDecl *, 1> getOverriddenDecls() const;
2904+
llvm::TinyPtrVector<AssociatedTypeDecl *> getOverriddenDecls() const;
29052905

29062906
SourceLoc getStartLoc() const { return KeywordLoc; }
29072907
SourceRange getSourceRange() const;

include/swift/AST/TypeCheckRequests.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/AST/SimpleRequest.h"
2222
#include "swift/Basic/Statistic.h"
2323
#include "llvm/ADT/Hashing.h"
24+
#include "llvm/ADT/TinyPtrVector.h"
2425

2526
namespace swift {
2627

@@ -126,7 +127,7 @@ class EnumRawTypeRequest :
126127
class OverriddenDeclsRequest
127128
: public SimpleRequest<OverriddenDeclsRequest,
128129
CacheKind::SeparatelyCached,
129-
SmallVector<ValueDecl *, 1>,
130+
llvm::TinyPtrVector<ValueDecl *>,
130131
ValueDecl *> {
131132
public:
132133
using SimpleRequest::SimpleRequest;
@@ -135,19 +136,19 @@ class OverriddenDeclsRequest
135136
friend class SimpleRequest;
136137

137138
// Evaluation.
138-
SmallVector<ValueDecl *, 1> evaluate(Evaluator &evaluator,
139-
ValueDecl *decl) const;
139+
llvm::TinyPtrVector<ValueDecl *> evaluate(Evaluator &evaluator,
140+
ValueDecl *decl) const;
140141

141142
public:
142143
// Cycle handling
143-
SmallVector<ValueDecl *, 1> breakCycle() const { return { }; }
144+
llvm::TinyPtrVector<ValueDecl *> breakCycle() const { return { }; }
144145
void diagnoseCycle(DiagnosticEngine &diags) const;
145146
void noteCycleStep(DiagnosticEngine &diags) const;
146147

147148
// Separate caching.
148149
bool isCached() const { return true; }
149-
Optional<SmallVector<ValueDecl *, 1>> getCachedResult() const;
150-
void cacheResult(SmallVector<ValueDecl *, 1> value) const;
150+
Optional<llvm::TinyPtrVector<ValueDecl *>> getCachedResult() const;
151+
void cacheResult(llvm::TinyPtrVector<ValueDecl *> value) const;
151152
};
152153

153154
/// Determine whether the given declaration is exposed to Objective-C.

lib/AST/ASTContext.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,26 +1595,27 @@ GenericEnvironment *ASTContext::getOrCreateCanonicalGenericEnvironment(
15951595
return env;
15961596
}
15971597

1598-
Optional<SmallVector<ValueDecl *, 1>>
1598+
Optional<llvm::TinyPtrVector<ValueDecl *>>
15991599
OverriddenDeclsRequest::getCachedResult() const {
16001600
auto decl = std::get<0>(getStorage());
16011601
if (!decl->LazySemanticInfo.hasOverriddenComputed)
16021602
return None;
16031603

16041604
// If there are no overridden declarations (the common case), return.
1605-
SmallVector<ValueDecl *, 1> overridden;
1605+
llvm::TinyPtrVector<ValueDecl *> overridden;
16061606
if (!decl->LazySemanticInfo.hasOverridden) return overridden;
16071607

16081608
// Retrieve the set of overrides from the ASTContext.
16091609
ASTContext &ctx = decl->getASTContext();
16101610
auto known = ctx.getImpl().Overrides.find(decl);
16111611
assert(known != ctx.getImpl().Overrides.end());
1612-
overridden.assign(known->second.begin(), known->second.end());
1612+
overridden.insert(overridden.end(),
1613+
known->second.begin(), known->second.end());
16131614
return overridden;
16141615
}
16151616

16161617
void OverriddenDeclsRequest::cacheResult(
1617-
SmallVector<ValueDecl *, 1> value) const {
1618+
llvm::TinyPtrVector<ValueDecl *> value) const {
16181619
auto decl = std::get<0>(getStorage());
16191620
decl->LazySemanticInfo.hasOverriddenComputed = true;
16201621
decl->LazySemanticInfo.hasOverridden = !value.empty();
@@ -1632,7 +1633,8 @@ void OverriddenDeclsRequest::cacheResult(
16321633

16331634
// Record the overrides in the context.
16341635
auto &ctx = decl->getASTContext();
1635-
auto overriddenCopy = ctx.AllocateCopy(value);
1636+
auto overriddenCopy =
1637+
ctx.AllocateCopy(value.operator ArrayRef<ValueDecl *>());
16361638
(void)ctx.getImpl().Overrides.insert({decl, overriddenCopy});
16371639
}
16381640

lib/AST/Decl.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,14 +2022,13 @@ CanType ValueDecl::getOverloadSignatureType() const {
20222022
return CanType();
20232023
}
20242024

2025-
SmallVector<ValueDecl *, 1> ValueDecl::getOverriddenDecls() const {
2025+
llvm::TinyPtrVector<ValueDecl *> ValueDecl::getOverriddenDecls() const {
20262026
ASTContext &ctx = getASTContext();
20272027
return ctx.evaluator(OverriddenDeclsRequest{const_cast<ValueDecl *>(this)});
20282028
}
20292029

20302030
void ValueDecl::setOverriddenDecls(ArrayRef<ValueDecl *> overridden) {
2031-
SmallVector<ValueDecl *, 1> overriddenVec(overridden.begin(),
2032-
overridden.end());
2031+
llvm::TinyPtrVector<ValueDecl *> overriddenVec(overridden);
20332032
OverriddenDeclsRequest request{const_cast<ValueDecl *>(this)};
20342033
request.cacheResult(overriddenVec);
20352034
}
@@ -2892,18 +2891,18 @@ SourceRange AssociatedTypeDecl::getSourceRange() const {
28922891
return SourceRange(KeywordLoc, endLoc);
28932892
}
28942893

2895-
SmallVector<AssociatedTypeDecl *, 1>
2894+
llvm::TinyPtrVector<AssociatedTypeDecl *>
28962895
AssociatedTypeDecl::getOverriddenDecls() const {
28972896
// FIXME: Performance hack because we end up looking at the overridden
28982897
// declarations of an associated type a *lot*.
28992898
OverriddenDeclsRequest request{const_cast<AssociatedTypeDecl *>(this)};
2900-
SmallVector<ValueDecl *, 1> overridden;
2899+
llvm::TinyPtrVector<ValueDecl *> overridden;
29012900
if (auto cached = request.getCachedResult())
29022901
overridden = std::move(*cached);
29032902
else
29042903
overridden = AbstractTypeParamDecl::getOverriddenDecls();
29052904

2906-
SmallVector<AssociatedTypeDecl *, 1> assocTypes;
2905+
llvm::TinyPtrVector<AssociatedTypeDecl *> assocTypes;
29072906
for (auto decl : overridden) {
29082907
assocTypes.push_back(cast<AssociatedTypeDecl>(decl));
29092908
}

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,7 +1534,7 @@ static bool checkSingleOverride(ValueDecl *override, ValueDecl *base) {
15341534
/// Minimize the set of overridden associated types, eliminating any
15351535
/// associated types that are overridden by other associated types.
15361536
static void minimizeOverriddenAssociatedTypes(
1537-
SmallVectorImpl<ValueDecl *> &assocTypes) {
1537+
llvm::TinyPtrVector<ValueDecl *> &assocTypes) {
15381538
// Mark associated types that are "worse" than some other associated type,
15391539
// because they come from an inherited protocol.
15401540
bool anyWorse = false;
@@ -1562,9 +1562,10 @@ static void minimizeOverriddenAssociatedTypes(
15621562
// Copy in the associated types that aren't worse than any other associated
15631563
// type.
15641564
unsigned nextIndex = 0;
1565-
for (unsigned i : indices(assocTypes)) {
1565+
MutableArrayRef<ValueDecl *> buffer = assocTypes;
1566+
for (unsigned i : indices(buffer)) {
15661567
if (worseThanAny[i]) continue;
1567-
assocTypes[nextIndex++] = assocTypes[i];
1568+
buffer[nextIndex++] = buffer[i];
15681569
}
15691570

15701571
assocTypes.erase(assocTypes.begin() + nextIndex, assocTypes.end());
@@ -1580,11 +1581,11 @@ static int compareSimilarAssociatedTypes(ValueDecl *const *lhs,
15801581

15811582
/// Compute the set of associated types that are overridden by the given
15821583
/// associated type.
1583-
static SmallVector<ValueDecl *, 1>
1584+
static llvm::TinyPtrVector<ValueDecl *>
15841585
computeOverriddenAssociatedTypes(AssociatedTypeDecl *assocType) {
15851586
// Find associated types with the given name in all of the inherited
15861587
// protocols.
1587-
SmallVector<ValueDecl *, 1> overriddenAssocTypes;
1588+
llvm::TinyPtrVector<ValueDecl *> overriddenAssocTypes;
15881589
auto proto = assocType->getProtocol();
15891590
proto->walkInheritedProtocols([&](ProtocolDecl *inheritedProto) {
15901591
if (proto == inheritedProto) return TypeWalker::Action::Continue;
@@ -1619,7 +1620,7 @@ computeOverriddenAssociatedTypes(AssociatedTypeDecl *assocType) {
16191620
return overriddenAssocTypes;
16201621
}
16211622

1622-
SmallVector<ValueDecl *, 1>
1623+
llvm::TinyPtrVector<ValueDecl *>
16231624
OverriddenDeclsRequest::evaluate(Evaluator &evaluator, ValueDecl *decl) const {
16241625
// For an associated type, compute the (minimized) set of overridden
16251626
// declarations.
@@ -1687,7 +1688,7 @@ OverriddenDeclsRequest::evaluate(Evaluator &evaluator, ValueDecl *decl) const {
16871688
return { };
16881689
}
16891690

1690-
return SmallVector<ValueDecl *, 1>{baseAccessor};
1691+
return llvm::TinyPtrVector<ValueDecl *>{baseAccessor};
16911692
}
16921693

16931694
// Only initializers and declarations marked with the 'override' declaration
@@ -1723,5 +1724,5 @@ OverriddenDeclsRequest::evaluate(Evaluator &evaluator, ValueDecl *decl) const {
17231724
return { };
17241725
}
17251726

1726-
return SmallVector<ValueDecl *, 1>{matches.front().Decl};
1727+
return llvm::TinyPtrVector<ValueDecl *>{matches.front().Decl};
17271728
}

0 commit comments

Comments
 (0)