Skip to content

Commit c20daa0

Browse files
committed
AST: Use separate caching for ProtocolDecl::getAllInheritedProtocols()
1 parent 273c4b2 commit c20daa0

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

include/swift/AST/Decl.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
600600
IsComputingSemanticMembers : 1
601601
);
602602

603-
SWIFT_INLINE_BITFIELD_FULL(ProtocolDecl, NominalTypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1+1+8,
603+
SWIFT_INLINE_BITFIELD_FULL(ProtocolDecl, NominalTypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+8,
604604
/// Whether the \c RequiresClass bit is valid.
605605
RequiresClassValid : 1,
606606

@@ -624,9 +624,12 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
624624
/// because they could not be imported from Objective-C).
625625
HasMissingRequirements : 1,
626626

627-
/// Whether we've computed the inherited protocols list yet.
627+
/// Whether we've computed the InheritedProtocolsRequest.
628628
InheritedProtocolsValid : 1,
629629

630+
/// Whether we've computed the AllInheritedProtocolsRequest.
631+
AllInheritedProtocolsValid : 1,
632+
630633
/// Whether we have computed a requirement signature.
631634
HasRequirementSignature : 1,
632635

@@ -5191,6 +5194,7 @@ class ProtocolDecl final : public NominalTypeDecl {
51915194

51925195
ArrayRef<PrimaryAssociatedTypeName> PrimaryAssociatedTypeNames;
51935196
ArrayRef<ProtocolDecl *> InheritedProtocols;
5197+
ArrayRef<ProtocolDecl *> AllInheritedProtocols;
51945198
ArrayRef<AssociatedTypeDecl *> AssociatedTypes;
51955199
ArrayRef<ValueDecl *> ProtocolRequirements;
51965200

@@ -5267,6 +5271,7 @@ class ProtocolDecl final : public NominalTypeDecl {
52675271
friend class ExistentialConformsToSelfRequest;
52685272
friend class HasSelfOrAssociatedTypeRequirementsRequest;
52695273
friend class InheritedProtocolsRequest;
5274+
friend class AllInheritedProtocolsRequest;
52705275
friend class PrimaryAssociatedTypesRequest;
52715276
friend class ProtocolRequirementsRequest;
52725277

@@ -5406,6 +5411,13 @@ class ProtocolDecl final : public NominalTypeDecl {
54065411
Bits.ProtocolDecl.InheritedProtocolsValid = true;
54075412
}
54085413

5414+
bool areAllInheritedProtocolsValid() const {
5415+
return Bits.ProtocolDecl.AllInheritedProtocolsValid;
5416+
}
5417+
void setAllInheritedProtocolsValid() {
5418+
Bits.ProtocolDecl.AllInheritedProtocolsValid = true;
5419+
}
5420+
54095421
bool areProtocolRequirementsValid() const {
54105422
return Bits.ProtocolDecl.ProtocolRequirementsValid;
54115423
}

include/swift/AST/NameLookupRequests.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ class InheritedProtocolsRequest
202202
class AllInheritedProtocolsRequest
203203
: public SimpleRequest<
204204
AllInheritedProtocolsRequest, ArrayRef<ProtocolDecl *>(ProtocolDecl *),
205-
RequestFlags::Cached> {
205+
RequestFlags::SeparatelyCached> {
206206
public:
207207
using SimpleRequest::SimpleRequest;
208208

@@ -216,6 +216,8 @@ class AllInheritedProtocolsRequest
216216
public:
217217
// Caching
218218
bool isCached() const { return true; }
219+
std::optional<ArrayRef<ProtocolDecl *>> getCachedResult() const;
220+
void cacheResult(ArrayRef<ProtocolDecl *> value) const;
219221
};
220222

221223
class ProtocolRequirementsRequest

lib/AST/Decl.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6622,13 +6622,14 @@ ProtocolDecl::ProtocolDecl(DeclContext *DC, SourceLoc ProtocolLoc,
66226622
Bits.ProtocolDecl.RequiresClass = false;
66236623
Bits.ProtocolDecl.ExistentialConformsToSelfValid = false;
66246624
Bits.ProtocolDecl.ExistentialConformsToSelf = false;
6625-
Bits.ProtocolDecl.InheritedProtocolsValid = 0;
6625+
Bits.ProtocolDecl.InheritedProtocolsValid = false;
6626+
Bits.ProtocolDecl.AllInheritedProtocolsValid = false;
66266627
Bits.ProtocolDecl.HasMissingRequirements = false;
66276628
Bits.ProtocolDecl.KnownProtocol = 0;
6628-
Bits.ProtocolDecl.HasAssociatedTypes = 0;
6629-
Bits.ProtocolDecl.HasLazyAssociatedTypes = 0;
6630-
Bits.ProtocolDecl.HasRequirementSignature = 0;
6631-
Bits.ProtocolDecl.HasLazyRequirementSignature = 0;
6629+
Bits.ProtocolDecl.HasAssociatedTypes = false;
6630+
Bits.ProtocolDecl.HasLazyAssociatedTypes = false;
6631+
Bits.ProtocolDecl.HasRequirementSignature = false;
6632+
Bits.ProtocolDecl.HasLazyRequirementSignature = false;
66326633
Bits.ProtocolDecl.ProtocolRequirementsValid = false;
66336634
setTrailingWhereClause(TrailingWhere);
66346635
}
@@ -6662,6 +6663,11 @@ ArrayRef<ProtocolDecl *> ProtocolDecl::getInheritedProtocols() const {
66626663
}
66636664

66646665
ArrayRef<ProtocolDecl *> ProtocolDecl::getAllInheritedProtocols() const {
6666+
// Avoid evaluator overhead because we call this from Symbol::compare()
6667+
// in the Requirement Machine.
6668+
if (Bits.ProtocolDecl.AllInheritedProtocolsValid)
6669+
return AllInheritedProtocols;
6670+
66656671
auto *mutThis = const_cast<ProtocolDecl *>(this);
66666672
return evaluateOrDefault(getASTContext().evaluator,
66676673
AllInheritedProtocolsRequest{mutThis},

lib/AST/NameLookupRequests.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,29 @@ void InheritedProtocolsRequest::writeDependencySink(
113113
}
114114
}
115115

116+
//----------------------------------------------------------------------------//
117+
// AllInheritedProtocolsRequest computation.
118+
//----------------------------------------------------------------------------//
119+
120+
std::optional<ArrayRef<ProtocolDecl *>>
121+
AllInheritedProtocolsRequest::getCachedResult() const {
122+
auto proto = std::get<0>(getStorage());
123+
if (!proto->areAllInheritedProtocolsValid())
124+
return std::nullopt;
125+
126+
return proto->AllInheritedProtocols;
127+
}
128+
129+
void AllInheritedProtocolsRequest::cacheResult(ArrayRef<ProtocolDecl *> PDs) const {
130+
auto proto = std::get<0>(getStorage());
131+
proto->AllInheritedProtocols = PDs;
132+
proto->setAllInheritedProtocolsValid();
133+
}
134+
135+
//----------------------------------------------------------------------------//
136+
// ProtocolRequirementsRequest computation.
137+
//----------------------------------------------------------------------------//
138+
116139
std::optional<ArrayRef<ValueDecl *>>
117140
ProtocolRequirementsRequest::getCachedResult() const {
118141
auto proto = std::get<0>(getStorage());

0 commit comments

Comments
 (0)