@@ -354,25 +354,85 @@ class SimpleLockingCacheEntryBase {
354
354
// / A key value as provided to the concurrent map.
355
355
class MetadataCacheKey {
356
356
const void * const *Data;
357
- uint32_t Length;
357
+ uint16_t NumKeyParameters;
358
+ uint16_t NumWitnessTables;
358
359
uint32_t Hash;
359
360
361
+ // / Compare two witness tables, which may involving checking the
362
+ // / contents of their conformance descriptors.
363
+ static int compareWitnessTables (const WitnessTable *awt,
364
+ const WitnessTable *bwt) {
365
+ if (awt == bwt)
366
+ return 0 ;
367
+
368
+ auto *aDescription = awt->Description ;
369
+ auto *bDescription = bwt->Description ;
370
+ if (aDescription == bDescription)
371
+ return 0 ;
372
+
373
+ if (!aDescription->isSynthesizedNonUnique () ||
374
+ !bDescription->isSynthesizedNonUnique ())
375
+ return comparePointers (aDescription, bDescription);
376
+
377
+ auto aType = aDescription->getCanonicalTypeMetadata ();
378
+ auto bType = bDescription->getCanonicalTypeMetadata ();
379
+ if (!aType || !bType)
380
+ return comparePointers (aDescription, bDescription);
381
+
382
+ if (int result = comparePointers (aType, bType))
383
+ return result;
384
+
385
+ return comparePointers (aDescription->getProtocol (),
386
+ bDescription->getProtocol ());
387
+ }
388
+
389
+ // / Compare the content from two keys.
390
+ static int compareContent (const void * const *adata,
391
+ const void * const *bdata,
392
+ unsigned numKeyParameters,
393
+ unsigned numWitnessTables) {
394
+ // Compare generic arguments for key parameters.
395
+ for (unsigned i = 0 ; i != numKeyParameters; ++i) {
396
+ if (auto result = comparePointers (*adata++, *bdata++))
397
+ return result;
398
+ }
399
+
400
+ // Compare witness tables.
401
+ for (unsigned i = 0 ; i != numWitnessTables; ++i) {
402
+ if (auto result =
403
+ compareWitnessTables ((const WitnessTable *)*adata++,
404
+ (const WitnessTable *)*bdata++))
405
+ return result;
406
+ }
407
+
408
+ return 0 ;
409
+ }
410
+
360
411
public:
361
- MetadataCacheKey (const void * const *data, size_t size)
362
- : Data(data), Length(size), Hash(computeHash()) {}
363
- MetadataCacheKey (const void * const *data, size_t size, uint32_t hash)
364
- : Data(data), Length(size), Hash(hash) {}
412
+ MetadataCacheKey (uint16_t numKeyParams,
413
+ uint16_t numWitnessTables,
414
+ const void * const *data)
415
+ : Data(data), NumKeyParameters(numKeyParams),
416
+ NumWitnessTables (numWitnessTables), Hash(computeHash()) { }
417
+
418
+ MetadataCacheKey (uint16_t numKeyParams,
419
+ uint16_t numWitnessTables,
420
+ const void * const *data,
421
+ uint32_t hash)
422
+ : Data(data), NumKeyParameters(numKeyParams),
423
+ NumWitnessTables(numWitnessTables), Hash(hash) {}
365
424
366
425
bool operator ==(MetadataCacheKey rhs) const {
426
+ // Compare the hashes.
427
+ if (hash () != rhs.hash ()) return false ;
428
+
367
429
// Compare the sizes.
368
- unsigned asize = size (), bsize = rhs.size () ;
369
- if (asize != bsize ) return false ;
430
+ if (NumKeyParameters ! = rhs.NumKeyParameters ) return false ;
431
+ if (NumWitnessTables != rhs. NumWitnessTables ) return false ;
370
432
371
433
// Compare the content.
372
- auto abegin = begin (), bbegin = rhs.begin ();
373
- for (unsigned i = 0 ; i < asize; ++i)
374
- if (abegin[i] != bbegin[i]) return false ;
375
- return true ;
434
+ return compareContent (begin (), rhs.begin (), NumKeyParameters,
435
+ NumWitnessTables) == 0 ;
376
436
}
377
437
378
438
int compare (const MetadataCacheKey &rhs) const {
@@ -381,38 +441,43 @@ class MetadataCacheKey {
381
441
return hashComparison;
382
442
}
383
443
384
- // Compare the sizes.
385
- if (auto sizeComparison = compareIntegers (size (), rhs.size ())) {
386
- return sizeComparison;
444
+ // Compare the # of key parameters.
445
+ if (auto keyParamsComparison =
446
+ compareIntegers (NumKeyParameters, rhs.NumKeyParameters )) {
447
+ return keyParamsComparison;
387
448
}
388
449
389
- // Compare the content.
390
- auto lbegin = begin (), rbegin = rhs.begin ();
391
- for (unsigned i = 0 , e = size (); i != e; ++i) {
392
- if (auto ptrComparison = comparePointers (lbegin[i], rbegin[i]))
393
- return ptrComparison;
450
+ // Compare the # of witness tables.
451
+ if (auto witnessTablesComparison =
452
+ compareIntegers (NumWitnessTables, rhs.NumWitnessTables )) {
453
+ return witnessTablesComparison;
394
454
}
395
455
396
- // Equal.
397
- return 0 ;
456
+ // Compare the content.
457
+ return compareContent (begin (), rhs.begin (), NumKeyParameters,
458
+ NumWitnessTables);
398
459
}
399
460
461
+ uint16_t numKeyParameters () const { return NumKeyParameters; }
462
+ uint16_t numWitnessTables () const { return NumWitnessTables; }
463
+
400
464
uint32_t hash () const {
401
465
return Hash;
402
466
}
403
467
404
468
const void * const *begin () const { return Data; }
405
- const void * const *end () const { return Data + Length ; }
406
- unsigned size () const { return Length ; }
469
+ const void * const *end () const { return Data + size () ; }
470
+ unsigned size () const { return NumKeyParameters + NumWitnessTables ; }
407
471
408
472
private:
409
473
uint32_t computeHash () const {
410
- size_t H = 0x56ba80d1 * Length ;
411
- for (unsigned i = 0 ; i < Length; i++ ) {
474
+ size_t H = 0x56ba80d1 * NumKeyParameters ;
475
+ for (unsigned index = 0 ; index != NumKeyParameters; ++index ) {
412
476
H = (H >> 10 ) | (H << ((sizeof (size_t ) * 8 ) - 10 ));
413
- H ^= (reinterpret_cast <size_t >(Data[i ])
414
- ^ (reinterpret_cast <size_t >(Data[i ]) >> 19 ));
477
+ H ^= (reinterpret_cast <size_t >(Data[index ])
478
+ ^ (reinterpret_cast <size_t >(Data[index ]) >> 19 ));
415
479
}
480
+
416
481
H *= 0x27d4eb2d ;
417
482
418
483
// Rotate right by 10 and then truncate to 32 bits.
@@ -1270,7 +1335,7 @@ class VariadicMetadataCacheEntryBase :
1270
1335
using OverloadToken = typename TrailingObjects::template OverloadToken<T>;
1271
1336
1272
1337
size_t numTrailingObjects (OverloadToken<const void *>) const {
1273
- return KeyLength ;
1338
+ return NumKeyParameters + NumWitnessTables ;
1274
1339
}
1275
1340
1276
1341
template <class ... Args>
@@ -1284,7 +1349,8 @@ class VariadicMetadataCacheEntryBase :
1284
1349
// These are arranged to fit into the tail-padding of the superclass.
1285
1350
1286
1351
// / These are set during construction and never changed.
1287
- const uint16_t KeyLength;
1352
+ const uint16_t NumKeyParameters;
1353
+ const uint16_t NumWitnessTables;
1288
1354
const uint32_t Hash;
1289
1355
1290
1356
// / Valid if TrackingInfo.getState() >= PrivateMetadataState::Abstract.
@@ -1300,14 +1366,17 @@ class VariadicMetadataCacheEntryBase :
1300
1366
1301
1367
public:
1302
1368
VariadicMetadataCacheEntryBase (const MetadataCacheKey &key)
1303
- : KeyLength(key.size()), Hash(key.hash()) {
1369
+ : NumKeyParameters(key.numKeyParameters()),
1370
+ NumWitnessTables (key.numWitnessTables()),
1371
+ Hash(key.hash()) {
1304
1372
memcpy (this ->template getTrailingObjects <const void *>(),
1305
1373
key.begin (), key.size () * sizeof (const void *));
1306
1374
}
1307
1375
1308
1376
MetadataCacheKey getKey () const {
1309
- return MetadataCacheKey (this ->template getTrailingObjects <const void *>(),
1310
- KeyLength, Hash);
1377
+ return MetadataCacheKey (NumKeyParameters, NumWitnessTables,
1378
+ this ->template getTrailingObjects <const void *>(),
1379
+ Hash);
1311
1380
}
1312
1381
1313
1382
intptr_t getKeyIntValueForDump () const {
0 commit comments