Skip to content

Commit 48ccef7

Browse files
committed
[NFC] Store the list of opened pack parameters as a flat array in
opened generic environments Finding these is very hot for these environments, so doing it once is a pretty nice win in both speed and code complexity. I'm not actually using this yet.
1 parent 09018a8 commit 48ccef7

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

include/swift/AST/GenericEnvironment.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
129129
/// generic signature.
130130
ArrayRef<Type> getContextTypes() const;
131131

132+
/// Retrieve the array of opened pack parameters for this opened-element
133+
/// environment. This is parallel to the array of element parameters,
134+
/// i.e. the innermost generic context.
135+
MutableArrayRef<Type> getOpenedPackParams();
136+
ArrayRef<Type> getOpenedPackParams() const;
137+
132138
/// Get the nested type storage, allocating it if required.
133139
NestedTypeStorage &getOrCreateNestedTypeStorage();
134140

@@ -201,6 +207,9 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
201207
/// Retrieve the UUID for an opened element environment.
202208
UUID getOpenedElementUUID() const;
203209

210+
/// Return the number of opened pack parameters.
211+
unsigned getNumOpenedPackParams() const;
212+
204213
void forEachPackElementArchetype(
205214
llvm::function_ref<void(ElementArchetypeType*)> function) const;
206215

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5164,10 +5164,12 @@ GenericEnvironment::forOpenedElement(GenericSignature signature,
51645164

51655165
// Allocate and construct the new environment.
51665166
unsigned numGenericParams = signature.getGenericParams().size();
5167+
unsigned numOpenedParams = signature.getInnermostGenericParams().size();
51675168
size_t bytes = totalSizeToAlloc<OpaqueEnvironmentData,
51685169
OpenedExistentialEnvironmentData,
5169-
OpenedElementEnvironmentData, Type>(
5170-
0, 0, 1, numGenericParams);
5170+
OpenedElementEnvironmentData,
5171+
Type>(
5172+
0, 0, 1, numGenericParams + numOpenedParams);
51715173
void *mem = ctx.Allocate(bytes, alignof(GenericEnvironment));
51725174
auto *genericEnv = new (mem) GenericEnvironment(signature,
51735175
uuid, shapeClass,

lib/AST/GenericEnvironment.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ size_t GenericEnvironment::numTrailingObjects(
6363
}
6464

6565
size_t GenericEnvironment::numTrailingObjects(OverloadToken<Type>) const {
66-
return getGenericParams().size();
66+
return getGenericParams().size()
67+
+ (getKind() == Kind::OpenedElement ? getNumOpenedPackParams() : 0);
6768
}
6869

6970
/// Retrieve the array containing the context types associated with the
@@ -82,6 +83,21 @@ ArrayRef<Type> GenericEnvironment::getContextTypes() const {
8283
getGenericParams().size());
8384
}
8485

86+
unsigned GenericEnvironment::getNumOpenedPackParams() const {
87+
assert(getKind() == Kind::OpenedElement);
88+
return getGenericSignature().getInnermostGenericParams().size();
89+
}
90+
91+
MutableArrayRef<Type> GenericEnvironment::getOpenedPackParams() {
92+
auto begin = getTrailingObjects<Type>() + getGenericParams().size();
93+
return MutableArrayRef<Type>(begin, getNumOpenedPackParams());
94+
}
95+
96+
ArrayRef<Type> GenericEnvironment::getOpenedPackParams() const {
97+
auto begin = getTrailingObjects<Type>() + getGenericParams().size();
98+
return ArrayRef<Type>(begin, getNumOpenedPackParams());
99+
}
100+
85101
TypeArrayView<GenericTypeParamType>
86102
GenericEnvironment::getGenericParams() const {
87103
return getGenericSignature().getGenericParams();
@@ -230,6 +246,16 @@ GenericEnvironment::GenericEnvironment(GenericSignature signature,
230246
// Clear out the memory that holds the context types.
231247
std::uninitialized_fill(getContextTypes().begin(), getContextTypes().end(),
232248
Type());
249+
250+
// Fill in the array of opened pack parameters.
251+
auto openedPacksBuffer = getOpenedPackParams();
252+
unsigned i = 0;
253+
for (auto param : signature.getGenericParams()) {
254+
if (!param->isParameterPack()) continue;
255+
if (!signature->haveSameShape(param, shapeClass)) continue;
256+
openedPacksBuffer[i++] = param;
257+
}
258+
assert(i == openedPacksBuffer.size());
233259
}
234260

235261
void GenericEnvironment::addMapping(GenericParamKey key,

0 commit comments

Comments
 (0)