@@ -268,12 +268,39 @@ namespace {
268
268
};
269
269
} // end anonymous namespace
270
270
271
- using GenericMetadataCache = MetadataCache<GenericCacheEntry, false >;
272
- using LazyGenericMetadataCache = Lazy<GenericMetadataCache>;
271
+ namespace {
272
+ class GenericMetadataCache : public MetadataCache <GenericCacheEntry, false > {
273
+ public:
274
+ uint16_t NumKeyParameters;
275
+ uint16_t NumWitnessTables;
276
+
277
+ GenericMetadataCache (const TargetGenericContext<InProcess> &genericContext)
278
+ : NumKeyParameters(0 ), NumWitnessTables(0 ) {
279
+ // Count up the # of key parameters and # of witness tables.
280
+
281
+ // Find key generic parameters.
282
+ for (const auto &gp : genericContext.getGenericParams ()) {
283
+ if (gp.hasKeyArgument ())
284
+ ++NumKeyParameters;
285
+ }
286
+
287
+ // Find witness tables.
288
+ for (const auto &req : genericContext.getGenericRequirements ()) {
289
+ if (req.Flags .hasKeyArgument () &&
290
+ req.getKind () == GenericRequirementKind::Protocol)
291
+ ++NumWitnessTables;
292
+ }
293
+ }
294
+ };
295
+
296
+ using LazyGenericMetadataCache = Lazy<GenericMetadataCache>;
297
+ }
273
298
274
299
// / Fetch the metadata cache for a generic metadata structure.
275
300
static GenericMetadataCache &getCache (
276
- const TypeGenericContextDescriptorHeader &generics) {
301
+ const TypeContextDescriptor &description) {
302
+ auto &generics = description.getFullGenericContextHeader ();
303
+
277
304
// Keep this assert even if you change the representation above.
278
305
static_assert (sizeof (LazyGenericMetadataCache) <=
279
306
sizeof (GenericMetadataInstantiationCache::PrivateData),
@@ -282,7 +309,7 @@ static GenericMetadataCache &getCache(
282
309
auto lazyCache =
283
310
reinterpret_cast <LazyGenericMetadataCache*>(
284
311
generics.getInstantiationCache ()->PrivateData );
285
- return lazyCache->get ( );
312
+ return lazyCache->getWithInit (*description. getGenericContext () );
286
313
}
287
314
288
315
// / Fetch the metadata cache for a generic metadata structure,
@@ -527,9 +554,11 @@ swift::swift_getGenericMetadata(MetadataRequest request,
527
554
auto &generics = description->getFullGenericContextHeader ();
528
555
size_t numGenericArgs = generics.Base .NumKeyArguments ;
529
556
530
- auto key = MetadataCacheKey (description, arguments, numGenericArgs);
531
- auto result =
532
- getCache (generics).getOrInsert (key, request, description, arguments);
557
+ auto &cache = getCache (*description);
558
+ assert (numGenericArgs == cache.NumKeyParameters + cache.NumWitnessTables );
559
+ auto key = MetadataCacheKey (cache.NumKeyParameters , cache.NumWitnessTables ,
560
+ arguments);
561
+ auto result = cache.getOrInsert (key, request, description, arguments);
533
562
534
563
return result.second ;
535
564
}
@@ -4337,11 +4366,15 @@ static Result performOnMetadataCache(const Metadata *metadata,
4337
4366
auto genericArgs =
4338
4367
reinterpret_cast <const void * const *>(
4339
4368
description->getGenericArguments (metadata));
4369
+ auto &cache = getCache (*description);
4340
4370
size_t numGenericArgs = generics.Base .NumKeyArguments ;
4341
- auto key = MetadataCacheKey (description, genericArgs, numGenericArgs);
4371
+ assert (numGenericArgs == cache.NumKeyParameters + cache.NumWitnessTables );
4372
+ (void )numGenericArgs;
4373
+ auto key = MetadataCacheKey (cache.NumKeyParameters , cache.NumWitnessTables ,
4374
+ genericArgs);
4342
4375
4343
4376
return std::move (callbacks).forGenericMetadata (metadata, description,
4344
- getCache (generics) , key);
4377
+ cache , key);
4345
4378
}
4346
4379
4347
4380
bool swift::addToMetadataQueue (MetadataCompletionQueueEntry *queueEntry,
0 commit comments