mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[AST] Use cached deserialized decl in getOpaqueResultTypeDecl
Mangling and looking up the opaque result type decl
for serialized decls is a fairly expensive
operation. Instead, fallthrough to the request
which will have a cached value set by deserialization.
This shaves ~30ms off the cached completion for:
```swift
import SwiftUI
struct V: View {
var body: some View {
Table(#^CC^#
}
}
```
This commit is contained in:
@@ -2758,6 +2758,10 @@ private:
|
||||
/// output a null pointer.
|
||||
unsigned noDynamicallyReplacedDecl : 1;
|
||||
|
||||
/// Whether the OpaqueResultTypeRequest request was evaluated and produced
|
||||
/// a null pointer.
|
||||
unsigned noOpaqueResultType : 1;
|
||||
|
||||
/// Whether the "isFinal" bit has been computed yet.
|
||||
unsigned isFinalComputed : 1;
|
||||
|
||||
@@ -2785,7 +2789,7 @@ private:
|
||||
friend class InterfaceTypeRequest;
|
||||
friend class CheckRedeclarationRequest;
|
||||
friend class ActorIsolationRequest;
|
||||
friend class DynamicallyReplacedDeclRequest;
|
||||
friend class OpaqueResultTypeRequest;
|
||||
friend class ApplyAccessNoteRequest;
|
||||
|
||||
friend class Decl;
|
||||
|
||||
@@ -2295,21 +2295,22 @@ public:
|
||||
|
||||
/// Builds an opaque result type for a declaration.
|
||||
class OpaqueResultTypeRequest
|
||||
: public SimpleRequest<OpaqueResultTypeRequest,
|
||||
OpaqueTypeDecl *(ValueDecl *),
|
||||
RequestFlags::Cached> {
|
||||
: public SimpleRequest<
|
||||
OpaqueResultTypeRequest, OpaqueTypeDecl *(ValueDecl *),
|
||||
RequestFlags::SeparatelyCached | RequestFlags::SplitCached> {
|
||||
public:
|
||||
using SimpleRequest::SimpleRequest;
|
||||
|
||||
private:
|
||||
friend SimpleRequest;
|
||||
|
||||
OpaqueTypeDecl *
|
||||
evaluate(Evaluator &evaluator, ValueDecl *VD) const;
|
||||
OpaqueTypeDecl *evaluate(Evaluator &evaluator, ValueDecl *VD) const;
|
||||
|
||||
public:
|
||||
// Caching.
|
||||
// Split caching.
|
||||
bool isCached() const { return true; }
|
||||
std::optional<OpaqueTypeDecl *> getCachedResult() const;
|
||||
void cacheResult(OpaqueTypeDecl *result) const;
|
||||
};
|
||||
|
||||
/// Determines if a function declaration is 'static'.
|
||||
|
||||
@@ -262,7 +262,7 @@ SWIFT_REQUEST(TypeChecker, OpaqueReadOwnershipRequest,
|
||||
NoLocationInfo)
|
||||
SWIFT_REQUEST(TypeChecker, OpaqueResultTypeRequest,
|
||||
OpaqueTypeDecl *(ValueDecl *),
|
||||
Cached, NoLocationInfo)
|
||||
SeparatelyCached | SplitCached, NoLocationInfo)
|
||||
SWIFT_REQUEST(TypeChecker, OperatorPrecedenceGroupRequest,
|
||||
PrecedenceGroupDecl *(PrecedenceGroupDecl *),
|
||||
Cached, NoLocationInfo)
|
||||
|
||||
@@ -3887,21 +3887,6 @@ TypeRepr *ValueDecl::getOpaqueResultTypeRepr() const {
|
||||
}
|
||||
|
||||
OpaqueTypeDecl *ValueDecl::getOpaqueResultTypeDecl() const {
|
||||
if (getOpaqueResultTypeRepr() == nullptr) {
|
||||
if (!isa<VarDecl>(this) &&
|
||||
!isa<FuncDecl>(this) &&
|
||||
!isa<SubscriptDecl>(this))
|
||||
return nullptr;
|
||||
auto file = cast<FileUnit>(getDeclContext()->getModuleScopeContext());
|
||||
// Don't look up when the decl is from source, otherwise a cycle will happen.
|
||||
if (file->getKind() == FileUnitKind::SerializedAST) {
|
||||
Mangle::ASTMangler mangler;
|
||||
auto name = mangler.mangleOpaqueTypeDecl(this);
|
||||
return file->lookupOpaqueResultType(name);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return evaluateOrDefault(getASTContext().evaluator,
|
||||
OpaqueResultTypeRequest{const_cast<ValueDecl *>(this)},
|
||||
nullptr);
|
||||
|
||||
@@ -385,6 +385,28 @@ void DynamicallyReplacedDeclRequest::cacheResult(ValueDecl *result) const {
|
||||
decl->getASTContext().evaluator.cacheNonEmptyOutput(*this, std::move(result));
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------//
|
||||
// OpaqueResultTypeRequest caching.
|
||||
//----------------------------------------------------------------------------//
|
||||
|
||||
std::optional<OpaqueTypeDecl *>
|
||||
OpaqueResultTypeRequest::getCachedResult() const {
|
||||
auto *decl = std::get<0>(getStorage());
|
||||
if (decl->LazySemanticInfo.noOpaqueResultType)
|
||||
return std::optional(nullptr);
|
||||
|
||||
return decl->getASTContext().evaluator.getCachedNonEmptyOutput(*this);
|
||||
}
|
||||
|
||||
void OpaqueResultTypeRequest::cacheResult(OpaqueTypeDecl *result) const {
|
||||
auto *decl = std::get<0>(getStorage());
|
||||
if (!result) {
|
||||
decl->LazySemanticInfo.noOpaqueResultType = 1;
|
||||
return;
|
||||
}
|
||||
decl->getASTContext().evaluator.cacheNonEmptyOutput(*this, std::move(result));
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------//
|
||||
// ApplyAccessNoteRequest computation.
|
||||
//----------------------------------------------------------------------------//
|
||||
|
||||
@@ -42,7 +42,9 @@ OpaqueTypeDecl *
|
||||
OpaqueResultTypeRequest::evaluate(Evaluator &evaluator,
|
||||
ValueDecl *originatingDecl) const {
|
||||
auto *repr = originatingDecl->getOpaqueResultTypeRepr();
|
||||
assert(repr && "Declaration does not have an opaque result type");
|
||||
if (!repr)
|
||||
return nullptr;
|
||||
|
||||
auto *dc = originatingDecl->getInnermostDeclContext();
|
||||
auto &ctx = dc->getASTContext();
|
||||
|
||||
|
||||
@@ -3936,14 +3936,17 @@ public:
|
||||
if (var->hasStorage())
|
||||
AddAttribute(new (ctx) HasStorageAttr(/*isImplicit:*/true));
|
||||
|
||||
if (opaqueReturnTypeID) {
|
||||
auto opaqueReturnType = MF.getDeclChecked(opaqueReturnTypeID);
|
||||
if (!opaqueReturnType)
|
||||
return opaqueReturnType.takeError();
|
||||
{
|
||||
OpaqueTypeDecl *opaqueDecl = nullptr;
|
||||
if (opaqueReturnTypeID) {
|
||||
auto opaqueReturnType = MF.getDeclChecked(opaqueReturnTypeID);
|
||||
if (!opaqueReturnType)
|
||||
return opaqueReturnType.takeError();
|
||||
|
||||
ctx.evaluator.cacheOutput(
|
||||
OpaqueResultTypeRequest{var},
|
||||
cast<OpaqueTypeDecl>(opaqueReturnType.get()));
|
||||
opaqueDecl = cast<OpaqueTypeDecl>(opaqueReturnType.get());
|
||||
}
|
||||
ctx.evaluator.cacheOutput(OpaqueResultTypeRequest{var},
|
||||
std::move(opaqueDecl));
|
||||
}
|
||||
|
||||
// If this is a lazy property, record its backing storage.
|
||||
@@ -4357,14 +4360,17 @@ public:
|
||||
fn->setIsObjC(isObjC);
|
||||
fn->setForcedStaticDispatch(hasForcedStaticDispatch);
|
||||
|
||||
if (opaqueReturnTypeID) {
|
||||
auto declOrError = MF.getDeclChecked(opaqueReturnTypeID);
|
||||
if (!declOrError)
|
||||
return declOrError.takeError();
|
||||
{
|
||||
OpaqueTypeDecl *opaqueDecl = nullptr;
|
||||
if (opaqueReturnTypeID) {
|
||||
auto declOrError = MF.getDeclChecked(opaqueReturnTypeID);
|
||||
if (!declOrError)
|
||||
return declOrError.takeError();
|
||||
|
||||
ctx.evaluator.cacheOutput(
|
||||
OpaqueResultTypeRequest{fn},
|
||||
cast<OpaqueTypeDecl>(declOrError.get()));
|
||||
opaqueDecl = cast<OpaqueTypeDecl>(declOrError.get());
|
||||
}
|
||||
ctx.evaluator.cacheOutput(OpaqueResultTypeRequest{fn},
|
||||
std::move(opaqueDecl));
|
||||
}
|
||||
|
||||
if (!isAccessor)
|
||||
@@ -5204,16 +5210,19 @@ public:
|
||||
subscript->setOverriddenDecl(cast_or_null<SubscriptDecl>(overridden.get()));
|
||||
if (subscript->getOverriddenDecl())
|
||||
AddAttribute(new (ctx) OverrideAttr(SourceLoc()));
|
||||
|
||||
if (opaqueReturnTypeID) {
|
||||
Decl *opaqueReturnType;
|
||||
UNWRAP(MF.getDeclChecked(opaqueReturnTypeID), opaqueReturnType);
|
||||
|
||||
ctx.evaluator.cacheOutput(
|
||||
OpaqueResultTypeRequest{subscript},
|
||||
cast<OpaqueTypeDecl>(opaqueReturnType));
|
||||
{
|
||||
OpaqueTypeDecl *opaqueDecl = nullptr;
|
||||
if (opaqueReturnTypeID) {
|
||||
Decl *opaqueReturnType;
|
||||
UNWRAP(MF.getDeclChecked(opaqueReturnTypeID), opaqueReturnType);
|
||||
|
||||
opaqueDecl = cast<OpaqueTypeDecl>(opaqueReturnType);
|
||||
}
|
||||
ctx.evaluator.cacheOutput(OpaqueResultTypeRequest{subscript},
|
||||
std::move(opaqueDecl));
|
||||
}
|
||||
|
||||
|
||||
return subscript;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user