@@ -1377,12 +1377,25 @@ id swift_dynamicCastObjCProtocolConditional(id object,
1377
1377
return object;
1378
1378
}
1379
1379
1380
+ // Check whether the current ObjC runtime supports lazy realization. If it does,
1381
+ // then we can avoid forcing realization of classes before we use them.
1382
+ static bool objcSupportsLazyRealization () {
1383
+ #if OBJC_SUPPORTSLAZYREALIZATION_DEFINED
1384
+ return SWIFT_LAZY_CONSTANT (_objc_supportsLazyRealization ());
1385
+ #else
1386
+ return false ;
1387
+ #endif
1388
+ }
1389
+
1380
1390
void swift::swift_instantiateObjCClass (const ClassMetadata *_c) {
1381
1391
static const objc_image_info ImageInfo = {0 , 0 };
1382
1392
1383
- // Ensure the superclass is realized.
1384
1393
Class c = class_const_cast (_c);
1385
- [class_getSuperclass (c) class ];
1394
+
1395
+ if (!objcSupportsLazyRealization ()) {
1396
+ // Ensure the superclass is realized.
1397
+ [class_getSuperclass (c) class ];
1398
+ }
1386
1399
1387
1400
// Register the class.
1388
1401
Class registered = objc_readClassPair (c, &ImageInfo);
@@ -1392,14 +1405,16 @@ id swift_dynamicCastObjCProtocolConditional(id object,
1392
1405
}
1393
1406
1394
1407
Class swift::swift_getInitializedObjCClass (Class c) {
1395
- // Used when we have class metadata and we want to ensure a class has been
1396
- // initialized by the Objective-C runtime. We need to do this because the
1397
- // class "c" might be valid metadata, but it hasn't been initialized yet.
1398
- // Send a message that's likely not to be overridden to minimize potential
1399
- // side effects. Ignore the return value in case it is overridden to
1400
- // return something different. See
1401
- // https://github.com/apple/swift/issues/52863 for an example.
1402
- [c self ];
1408
+ if (!objcSupportsLazyRealization ()) {
1409
+ // Used when we have class metadata and we want to ensure a class has been
1410
+ // initialized by the Objective-C runtime. We need to do this because the
1411
+ // class "c" might be valid metadata, but it hasn't been initialized yet.
1412
+ // Send a message that's likely not to be overridden to minimize potential
1413
+ // side effects. Ignore the return value in case it is overridden to
1414
+ // return something different. See
1415
+ // https://github.com/apple/swift/issues/52863 for an example.
1416
+ [c self ];
1417
+ }
1403
1418
return c;
1404
1419
}
1405
1420
0 commit comments