Skip to content

Commit a824ad6

Browse files
committed
Cache declarations
1 parent c1fccc2 commit a824ad6

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed

lib/AST/ASTContext.cpp

+38-24
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,12 @@ struct ASTContext::Implementation {
343343
/// The declaration of Swift.Optional<T>.None.
344344
EnumElementDecl *OptionalNoneDecl = nullptr;
345345

346+
/// The declaration of Optional<T>.TangentVector.init
347+
ConstructorDecl *OptionalTanInitDecl = nullptr;
348+
349+
/// The declaration of Optional<T>.TangentVector.value
350+
VarDecl *OptionalTanValueDecl = nullptr;
351+
346352
/// The declaration of Swift.Void.
347353
TypeAliasDecl *VoidDecl = nullptr;
348354

@@ -2246,41 +2252,49 @@ void ASTContext::loadObjCMethods(
22462252
}
22472253

22482254
ConstructorDecl *ASTContext::getOptionalTanInitDecl(CanType optionalTanType) {
2249-
auto *optionalTanDecl = optionalTanType.getNominalOrBoundGenericNominal();
2250-
// Look up the `Optional<T>.TangentVector.init` declaration.
2251-
auto initLookup =
2252-
optionalTanDecl->lookupDirect(DeclBaseName::createConstructor());
2253-
ConstructorDecl *constructorDecl = nullptr;
2254-
for (auto *candidate : initLookup) {
2255-
auto candidateModule = candidate->getModuleContext();
2256-
if (candidateModule->getName() == Id_Differentiation ||
2257-
candidateModule->isStdlibModule()) {
2258-
assert(!constructorDecl && "Multiple `Optional.TangentVector.init`s");
2259-
constructorDecl = cast<ConstructorDecl>(candidate);
2255+
if (!getImpl().OptionalTanInitDecl) {
2256+
auto *optionalTanDecl = optionalTanType.getNominalOrBoundGenericNominal();
2257+
// Look up the `Optional<T>.TangentVector.init` declaration.
2258+
auto initLookup =
2259+
optionalTanDecl->lookupDirect(DeclBaseName::createConstructor());
2260+
ConstructorDecl *constructorDecl = nullptr;
2261+
for (auto *candidate : initLookup) {
2262+
auto candidateModule = candidate->getModuleContext();
2263+
if (candidateModule->getName() == Id_Differentiation ||
2264+
candidateModule->isStdlibModule()) {
2265+
assert(!constructorDecl && "Multiple `Optional.TangentVector.init`s");
2266+
constructorDecl = cast<ConstructorDecl>(candidate);
22602267
#ifdef NDEBUG
2261-
break;
2268+
break;
22622269
#endif
2270+
}
22632271
}
2272+
assert(constructorDecl && "No `Optional.TangentVector.init`");
2273+
2274+
getImpl().OptionalTanInitDecl = constructorDecl;
22642275
}
2265-
assert(constructorDecl && "No `Optional.TangentVector.init`");
22662276

2267-
return constructorDecl;
2277+
return getImpl().OptionalTanInitDecl;
22682278
}
22692279

22702280
VarDecl *ASTContext::getOptionalTanValueDecl(CanType optionalTanType) {
2271-
// TODO: Maybe it would be better to have getters / setters here that we
2272-
// can call and hide this implementation detail?
2273-
StructDecl *optStructDecl = optionalTanType.getStructOrBoundGenericStruct();
2274-
assert(optStructDecl && "Unexpected type of Optional.TangentVector");
2281+
if (!getImpl().OptionalTanValueDecl) {
2282+
// TODO: Maybe it would be better to have getters / setters here that we
2283+
// can call and hide this implementation detail?
2284+
StructDecl *optStructDecl = optionalTanType.getStructOrBoundGenericStruct();
2285+
assert(optStructDecl && "Unexpected type of Optional.TangentVector");
22752286

2276-
ArrayRef<VarDecl *> properties = optStructDecl->getStoredProperties();
2277-
assert(properties.size() == 1 && "Unexpected type of Optional.TangentVector");
2278-
VarDecl *wrappedValueVar = properties[0];
2287+
ArrayRef<VarDecl *> properties = optStructDecl->getStoredProperties();
2288+
assert(properties.size() == 1 && "Unexpected type of Optional.TangentVector");
2289+
VarDecl *wrappedValueVar = properties[0];
22792290

2280-
assert(wrappedValueVar->getTypeInContext()->getEnumOrBoundGenericEnum() ==
2281-
getOptionalDecl() && "Unexpected type of Optional.TangentVector");
2291+
assert(wrappedValueVar->getTypeInContext()->getEnumOrBoundGenericEnum() ==
2292+
getOptionalDecl() && "Unexpected type of Optional.TangentVector");
2293+
2294+
getImpl().OptionalTanValueDecl = wrappedValueVar;
2295+
}
22822296

2283-
return wrappedValueVar;
2297+
return getImpl().OptionalTanValueDecl;
22842298
}
22852299

22862300
void ASTContext::loadDerivativeFunctionConfigurations(

0 commit comments

Comments
 (0)