Skip to content

Commit c0ccec3

Browse files
authored
Speed up class_getImageName (swiftlang#37913)
When building for Apple OS, dyld_image_path_containing_address is much, much faster than dladdr. Resolves rdar://42137685 Resolves SR-14759
1 parent a1d1b2d commit c0ccec3

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

stdlib/public/runtime/ObjCRuntimeGetImageNameFromClass.mm

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
#include <objc/message.h>
2929
#include <TargetConditionals.h>
3030

31+
#if __has_include(<mach-o/dyld_priv.h>)
32+
#include <mach-o/dyld_priv.h>
33+
#define APPLE_OS_SYSTEM 1
34+
#else
35+
#define APPLE_OS_SYSTEM 0
36+
#endif
37+
3138
// Note: There are more #includes below under "Function patching machinery".
3239
// Those are only relevant to the function patching machinery.
3340

@@ -69,11 +76,17 @@ typedef BOOL (*objc_hook_getImageName)(
6976
const void *descriptor = classAsMetadata->getDescription();
7077
assert(descriptor &&
7178
"all non-artificial Swift classes should have a descriptor");
79+
#if APPLE_OS_SYSTEM
80+
// Use a more efficient internal API when building the system libraries
81+
// for Apple OSes.
82+
*outImageName = dyld_image_path_containing_address(descriptor);
83+
#else
7284
Dl_info imageInfo = {};
7385
if (!dladdr(descriptor, &imageInfo))
7486
return NO;
7587
*outImageName = imageInfo.dli_fname;
76-
return imageInfo.dli_fname != nullptr;
88+
#endif
89+
return *outImageName != nullptr;
7790
}
7891

7992
return NO;

0 commit comments

Comments
 (0)