You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SR-13459 type metadata accessor for private swift-native class should be a single memory access
Issue Description:
On Apple platforms, non-generic class metadata still has to be registered with the ObjC runtime upon first use, just in case it escapes into Objective-C-land. The current strategy to do this is to send a dummy message to it, which new enough ObjC runtimes provide in the form of an objc_opt_self function (equivalent to [theClass self]). However, even this optimized function is still slower than checking a static (once-initialized) flag (see SR-13459).
While the compiler is generally very good about optimizing repeated accesses to metadata, there are (apparently) cases where the dummy function call to objc_opt_self is still unnecessarily expensive. This could be remedied by guarding it with an (unsynchronized) guard variable in the metadata accessor:
if (!isMyClassRegisteredWithObjC.load(std::memory_order::relaxed)) {
objc_opt_self(MyClass);
isMyClassRegisteredWithObjC.store(true, std::memory_order::relaxed);
}
Additional Detail from JIRA
md5: d1188f5ba806808d599c07bf84d7d805
relates to:
Issue Description:
On Apple platforms, non-generic class metadata still has to be registered with the ObjC runtime upon first use, just in case it escapes into Objective-C-land. The current strategy to do this is to send a dummy message to it, which new enough ObjC runtimes provide in the form of an
objc_opt_self
function (equivalent to[theClass self]
). However, even this optimized function is still slower than checking a static (once-initialized) flag (see SR-13459).While the compiler is generally very good about optimizing repeated accesses to metadata, there are (apparently) cases where the dummy function call to
objc_opt_self
is still unnecessarily expensive. This could be remedied by guarding it with an (unsynchronized) guard variable in the metadata accessor:In fact, if we want to expose a bit of ObjC's ABI, we could check the very flag in an Objective-C class that checks for initialization, or do something similar like "does the data pointer still point to read-only data". But I'm not sure if Apple wants to sign up for that.
The main caveat I can think of is larger code size, so it's possible we don't want to do this in -Osize mode.
The text was updated successfully, but these errors were encountered: