Skip to content

Commit c44b49e

Browse files
committed
AST: Cache result of ProtocolDecl::getAssociatedTypeMembers()
1 parent 043bc46 commit c44b49e

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

include/swift/AST/Decl.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ class alignas(1 << DeclAlignInBits) Decl {
511511
IsComputingSemanticMembers : 1
512512
);
513513

514-
SWIFT_INLINE_BITFIELD_FULL(ProtocolDecl, NominalTypeDecl, 1+1+1+1+1+1+1+1+1+8+16,
514+
SWIFT_INLINE_BITFIELD_FULL(ProtocolDecl, NominalTypeDecl, 1+1+1+1+1+1+1+1+1+1+1+8+16,
515515
/// Whether the \c RequiresClass bit is valid.
516516
RequiresClassValid : 1,
517517

@@ -540,6 +540,12 @@ class alignas(1 << DeclAlignInBits) Decl {
540540
/// Whether we have a lazy-loaded requirement signature.
541541
HasLazyRequirementSignature : 1,
542542

543+
/// Whether we have computed the list of associated types.
544+
HasAssociatedTypes : 1,
545+
546+
/// Whether we have a lazy-loaded list of associated types.
547+
HasLazyAssociatedTypes : 1,
548+
543549
: NumPadBits,
544550

545551
/// If this is a compiler-known protocol, this will be a KnownProtocolKind
@@ -4094,6 +4100,7 @@ class ProtocolDecl final : public NominalTypeDecl {
40944100
SourceLoc ProtocolLoc;
40954101

40964102
ArrayRef<ProtocolDecl *> InheritedProtocols;
4103+
ArrayRef<AssociatedTypeDecl *> AssociatedTypes;
40974104

40984105
struct {
40994106
/// The superclass decl and a bit to indicate whether the
@@ -4192,7 +4199,7 @@ class ProtocolDecl final : public NominalTypeDecl {
41924199
/// Retrieve the set of AssociatedTypeDecl members of this protocol; this
41934200
/// saves loading the set of members in cases where there's no possibility of
41944201
/// a protocol having nested types (ObjC protocols).
4195-
llvm::TinyPtrVector<AssociatedTypeDecl *> getAssociatedTypeMembers() const;
4202+
ArrayRef<AssociatedTypeDecl *> getAssociatedTypeMembers() const;
41964203

41974204
/// Returns a protocol requirement with the given name, or nullptr if the
41984205
/// name has multiple overloads, or no overloads at all.

lib/AST/Decl.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4787,7 +4787,9 @@ ProtocolDecl::ProtocolDecl(DeclContext *DC, SourceLoc ProtocolLoc,
47874787
Bits.ProtocolDecl.NumRequirementsInSignature = 0;
47884788
Bits.ProtocolDecl.HasMissingRequirements = false;
47894789
Bits.ProtocolDecl.KnownProtocol = 0;
4790-
setTrailingWhereClause(TrailingWhere);
4790+
Bits.ProtocolDecl.HasAssociatedTypes = 0;
4791+
Bits.ProtocolDecl.HasLazyAssociatedTypes = 0;
4792+
setTrailingWhereClause(TrailingWhere);
47914793
}
47924794

47934795
bool ProtocolDecl::isMarkerProtocol() const {
@@ -4806,17 +4808,23 @@ ArrayRef<ProtocolDecl *> ProtocolDecl::getInheritedProtocols() const {
48064808
{});
48074809
}
48084810

4809-
llvm::TinyPtrVector<AssociatedTypeDecl *>
4811+
ArrayRef<AssociatedTypeDecl *>
48104812
ProtocolDecl::getAssociatedTypeMembers() const {
4811-
llvm::TinyPtrVector<AssociatedTypeDecl *> result;
4813+
if (Bits.ProtocolDecl.HasAssociatedTypes)
4814+
return AssociatedTypes;
4815+
4816+
auto *self = const_cast<ProtocolDecl *>(this);
4817+
self->Bits.ProtocolDecl.HasAssociatedTypes = 1;
48124818

48134819
// Clang-imported protocols never have associated types.
48144820
if (hasClangNode())
4815-
return result;
4821+
return ArrayRef<AssociatedTypeDecl *>();
48164822

48174823
// Deserialized @objc protocols never have associated types.
4818-
if (!getParentSourceFile() && isObjC())
4819-
return result;
4824+
if (getParentSourceFile() == nullptr && isObjC())
4825+
return ArrayRef<AssociatedTypeDecl *>();
4826+
4827+
SmallVector<AssociatedTypeDecl *, 2> result;
48204828

48214829
// Find the associated type declarations.
48224830
for (auto member : getMembers()) {
@@ -4825,7 +4833,8 @@ ProtocolDecl::getAssociatedTypeMembers() const {
48254833
}
48264834
}
48274835

4828-
return result;
4836+
self->AssociatedTypes = getASTContext().AllocateCopy(result);
4837+
return AssociatedTypes;
48294838
}
48304839

48314840
ValueDecl *ProtocolDecl::getSingleRequirement(DeclName name) const {

lib/AST/RequirementMachine/ProtocolGraph.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct ProtocolInfo {
3535
llvm::TinyPtrVector<const ProtocolDecl *> AllInherited;
3636

3737
/// Associated types defined in the protocol itself.
38-
llvm::TinyPtrVector<AssociatedTypeDecl *> AssociatedTypes;
38+
ArrayRef<AssociatedTypeDecl *> AssociatedTypes;
3939

4040
/// Associated types from all inherited protocols, not including duplicates or
4141
/// those defined in the protocol itself. Computed by
@@ -64,7 +64,7 @@ struct ProtocolInfo {
6464
}
6565

6666
ProtocolInfo(ArrayRef<ProtocolDecl *> inherited,
67-
llvm::TinyPtrVector<AssociatedTypeDecl *> &&types,
67+
ArrayRef<AssociatedTypeDecl *> &&types,
6868
ArrayRef<Requirement> reqs)
6969
: Inherited(inherited),
7070
AssociatedTypes(types),

0 commit comments

Comments
 (0)