Skip to content

Commit 90952fb

Browse files
committed
AST: Push memoization down from OpaqueTypeArchetypeType::get() to GenericEnvironment::forOpaqueType()
1 parent 68514b1 commit 90952fb

File tree

2 files changed

+28
-31
lines changed

2 files changed

+28
-31
lines changed

include/swift/AST/GenericEnvironment.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
190190
/// Create a new generic environment for an opaque type with the given set of
191191
/// outer substitutions.
192192
static GenericEnvironment *forOpaqueType(
193-
OpaqueTypeDecl *opaque, SubstitutionMap subs, AllocationArena arena);
193+
OpaqueTypeDecl *opaque, SubstitutionMap subs);
194194

195195
/// Create a new generic environment for an opened existential.
196196
///

lib/AST/ASTContext.cpp

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4694,26 +4694,7 @@ OpaqueTypeArchetypeType *OpaqueTypeArchetypeType::getNew(
46944694

46954695
Type OpaqueTypeArchetypeType::get(
46964696
OpaqueTypeDecl *Decl, Type interfaceType, SubstitutionMap Substitutions) {
4697-
// TODO: We could attempt to preserve type sugar in the substitution map.
4698-
// Currently archetypes are assumed to be always canonical in many places,
4699-
// though, so doing so would require fixing those places.
4700-
Substitutions = Substitutions.getCanonical();
4701-
4702-
auto &ctx = Decl->getASTContext();
4703-
4704-
// Look for an opaque archetype environment in the appropriate arena.
4705-
auto properties = getOpaqueTypeArchetypeProperties(Substitutions);
4706-
auto arena = getArena(properties);
4707-
auto &environments
4708-
= ctx.getImpl().getArena(arena).OpaqueArchetypeEnvironments;
4709-
GenericEnvironment *env = environments[{Decl, Substitutions}];
4710-
4711-
// Create the environment if it's missing.
4712-
if (!env) {
4713-
env = GenericEnvironment::forOpaqueType(Decl, Substitutions, arena);
4714-
environments[{Decl, Substitutions}] = env;
4715-
}
4716-
4697+
auto *env = GenericEnvironment::forOpaqueType(Decl, Substitutions);
47174698
return env->getOrCreateArchetypeFromInterfaceType(interfaceType);
47184699
}
47194700

@@ -4940,18 +4921,34 @@ GenericEnvironment *GenericEnvironment::forPrimary(GenericSignature signature) {
49404921
/// Create a new generic environment for an opaque type with the given set of
49414922
/// outer substitutions.
49424923
GenericEnvironment *GenericEnvironment::forOpaqueType(
4943-
OpaqueTypeDecl *opaque, SubstitutionMap subs, AllocationArena arena) {
4924+
OpaqueTypeDecl *opaque, SubstitutionMap subs) {
4925+
// TODO: We could attempt to preserve type sugar in the substitution map.
4926+
// Currently archetypes are assumed to be always canonical in many places,
4927+
// though, so doing so would require fixing those places.
4928+
subs = subs.getCanonical();
4929+
49444930
auto &ctx = opaque->getASTContext();
49454931

4946-
// Allocate and construct the new environment.
4947-
auto signature = opaque->getOpaqueInterfaceGenericSignature();
4948-
unsigned numGenericParams = signature.getGenericParams().size();
4949-
size_t bytes = totalSizeToAlloc<OpaqueEnvironmentData,
4950-
OpenedExistentialEnvironmentData,
4951-
OpenedElementEnvironmentData, Type>(
4952-
1, 0, 0, numGenericParams);
4953-
void *mem = ctx.Allocate(bytes, alignof(GenericEnvironment), arena);
4954-
auto env = new (mem) GenericEnvironment(signature, opaque, subs);
4932+
auto properties = getOpaqueTypeArchetypeProperties(subs);
4933+
auto arena = getArena(properties);
4934+
auto &environments
4935+
= ctx.getImpl().getArena(arena).OpaqueArchetypeEnvironments;
4936+
GenericEnvironment *env = environments[{opaque, subs}];
4937+
4938+
if (!env) {
4939+
// Allocate and construct the new environment.
4940+
auto signature = opaque->getOpaqueInterfaceGenericSignature();
4941+
unsigned numGenericParams = signature.getGenericParams().size();
4942+
size_t bytes = totalSizeToAlloc<OpaqueEnvironmentData,
4943+
OpenedExistentialEnvironmentData,
4944+
OpenedElementEnvironmentData, Type>(
4945+
1, 0, 0, numGenericParams);
4946+
void *mem = ctx.Allocate(bytes, alignof(GenericEnvironment), arena);
4947+
env = new (mem) GenericEnvironment(signature, opaque, subs);
4948+
4949+
environments[{opaque, subs}] = env;
4950+
}
4951+
49554952
return env;
49564953
}
49574954

0 commit comments

Comments
 (0)