Skip to content

Speed up class_getImageName #37913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion stdlib/public/runtime/ObjCRuntimeGetImageNameFromClass.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
#include <objc/message.h>
#include <TargetConditionals.h>

#if __has_include(<mach-o/dyld_priv.h>)
#include <mach-o/dyld_priv.h>
#define APPLE_OS_SYSTEM 1
#else
#define APPLE_OS_SYSTEM 0
#endif

// Note: There are more #includes below under "Function patching machinery".
// Those are only relevant to the function patching machinery.

Expand Down Expand Up @@ -69,11 +76,17 @@ typedef BOOL (*objc_hook_getImageName)(
const void *descriptor = classAsMetadata->getDescription();
assert(descriptor &&
"all non-artificial Swift classes should have a descriptor");
#if APPLE_OS_SYSTEM
// Use a more efficient internal API when building the system libraries
// for Apple OSes.
*outImageName = dyld_image_path_containing_address(descriptor);
#else
Dl_info imageInfo = {};
if (!dladdr(descriptor, &imageInfo))
return NO;
*outImageName = imageInfo.dli_fname;
return imageInfo.dli_fname != nullptr;
#endif
return *outImageName != nullptr;
}

return NO;
Expand Down