Skip to content

Commit cdd2928

Browse files
committed
[Runtime] Generalize SubstGenericParametersFromMetadata slightly.
Only use the metadata and context information during initial setup, not later on. NFC just yet.
1 parent 600b59b commit cdd2928

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,7 @@ buildDescriptorPath(const ContextDescriptor *context) const {
12901290
if (!context)
12911291
return 0;
12921292

1293-
// Add the parent's contributino to the descriptor path.
1293+
// Add the parent's contribution to the descriptor path.
12941294
unsigned numKeyGenericParamsInParent =
12951295
buildDescriptorPath(context->Parent.get());
12961296

@@ -1301,15 +1301,18 @@ buildDescriptorPath(const ContextDescriptor *context) const {
13011301
// Count the number of key generic params at this level.
13021302
unsigned numKeyGenericParamsHere = 0;
13031303
bool hasNonKeyGenericParams = false;
1304-
for (const auto &genericParam : getLocalGenericParams(context)) {
1304+
auto localGenericParams = getLocalGenericParams(context);
1305+
for (const auto &genericParam : localGenericParams) {
13051306
if (genericParam.hasKeyArgument())
13061307
++numKeyGenericParamsHere;
13071308
else
13081309
hasNonKeyGenericParams = true;
13091310
}
13101311

13111312
// Form the path element.
1312-
descriptorPath.push_back(PathElement{context, numKeyGenericParamsInParent,
1313+
descriptorPath.push_back(PathElement{localGenericParams,
1314+
context->getNumGenericParams(),
1315+
numKeyGenericParamsInParent,
13131316
numKeyGenericParamsHere,
13141317
hasNonKeyGenericParams});
13151318
return numKeyGenericParamsInParent + numKeyGenericParamsHere;
@@ -1335,18 +1338,17 @@ SubstGenericParametersFromMetadata::operator()(
13351338

13361339
/// Retrieve the descriptor path element at this depth.
13371340
auto &pathElement = descriptorPath[depth];
1338-
auto currentContext = pathElement.context;
13391341

13401342
// Check whether the index is clearly out of bounds.
1341-
if (index >= currentContext->getNumGenericParams())
1343+
if (index >= pathElement.numTotalGenericParams)
13421344
return nullptr;
13431345

13441346
// Compute the flat index.
13451347
unsigned flatIndex = pathElement.numKeyGenericParamsInParent;
13461348
if (pathElement.hasNonKeyGenericParams > 0) {
13471349
// We have non-key generic parameters at this level, so the index needs to
13481350
// be checked more carefully.
1349-
auto genericParams = getLocalGenericParams(currentContext);
1351+
auto genericParams = pathElement.localGenericParams;
13501352

13511353
// Make sure that the requested parameter itself has a key argument.
13521354
if (!genericParams[index].hasKeyArgument())
@@ -1362,7 +1364,7 @@ SubstGenericParametersFromMetadata::operator()(
13621364
flatIndex += index;
13631365
}
13641366

1365-
return base->getGenericArgs()[flatIndex];
1367+
return (const Metadata *)genericArgs[flatIndex];
13661368
}
13671369

13681370
const WitnessTable *
@@ -1371,8 +1373,7 @@ SubstGenericParametersFromMetadata::operator()(const Metadata *type,
13711373
// On first access, compute the descriptor path.
13721374
setup();
13731375

1374-
return (const WitnessTable *)base->getGenericArgs()[
1375-
index + numKeyGenericParameters];
1376+
return (const WitnessTable *)genericArgs[index + numKeyGenericParameters];
13761377
}
13771378

13781379
const Metadata *SubstGenericParametersFromWrittenArgs::operator()(

stdlib/public/runtime/Private.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,16 @@ class TypeInfo {
243243
class SWIFT_RUNTIME_LIBRARY_VISIBILITY SubstGenericParametersFromMetadata {
244244
const Metadata *base;
245245

246+
/// The generic arguments.
247+
const void * const *genericArgs;
248+
246249
/// An element in the descriptor path.
247250
struct PathElement {
248-
/// The context described by this path element.
249-
const ContextDescriptor *context;
251+
/// The generic parameters local to this element.
252+
ArrayRef<GenericParamDescriptor> localGenericParams;
253+
254+
/// The total number of generic parameters.
255+
unsigned numTotalGenericParams;
250256

251257
/// The number of key parameters in the parent.
252258
unsigned numKeyGenericParamsInParent;
@@ -277,7 +283,9 @@ class TypeInfo {
277283
public:
278284
/// Produce substitutions entirely from the given metadata.
279285
explicit SubstGenericParametersFromMetadata(const Metadata *base)
280-
: base(base) { }
286+
: base(base),
287+
genericArgs(base ? (const void * const *)base->getGenericArgs()
288+
: nullptr) { }
281289

282290
const Metadata *operator()(unsigned depth, unsigned index) const;
283291
const WitnessTable *operator()(const Metadata *type, unsigned index) const;

0 commit comments

Comments
 (0)