Skip to content

Commit 96a9326

Browse files
committed
Use the Swift runtime's (faster) class check in the stdlib instead of shimming object_getClass()
1 parent f8e4c75 commit 96a9326

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

stdlib/public/core/Builtin.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,11 @@ internal func _branchHint(_ actual: Bool, expected: Bool) -> Bool {
323323
@usableFromInline
324324
@_silgen_name("_swift_objcClassUsesNativeSwiftReferenceCounting")
325325
internal func _usesNativeSwiftReferenceCounting(_ theClass: AnyClass) -> Bool
326+
327+
/// Returns the class of a non-tagged-pointer Objective-C object
328+
@_effects(readonly)
329+
@_silgen_name("_swift_classOfObjCHeapObject")
330+
internal func _swift_classOfObjCHeapObject(_ object: AnyObject) -> AnyClass
326331
#else
327332
@inlinable
328333
@inline(__always)

stdlib/public/core/StringBridge.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,6 @@ private var kCFStringEncodingUTF8 : _swift_shims_CFStringEncoding {
125125
@inline(__always) get { return 0x8000100 }
126126
}
127127

128-
@_effects(readonly)
129-
private func _unsafeAddressOfCocoaStringClass(_ str: _CocoaString) -> UInt {
130-
return _swift_stdlib_unsafeAddressOfClass(str)
131-
}
132-
133128
internal enum _KnownCocoaString {
134129
case storage
135130
case shared
@@ -148,7 +143,7 @@ internal enum _KnownCocoaString {
148143
}
149144
#endif
150145

151-
switch _unsafeAddressOfCocoaStringClass(str) {
146+
switch unsafeBitCast(_swift_classOfObjCHeapObject(str), to: UInt.self) {
152147
case unsafeBitCast(__StringStorage.self, to: UInt.self):
153148
self = .storage
154149
case unsafeBitCast(__SharedStringStorage.self, to: UInt.self):

stdlib/public/runtime/SwiftObject.mm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ static Class _swift_getObjCClassOfAllocated(const void *object) {
177177
class_getInstanceSize(cls), mask));
178178
}
179179

180+
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
181+
Class _swift_classOfObjCHeapObject(OpaqueValue *value) {
182+
return _swift_getObjCClassOfAllocated(value);
183+
}
184+
180185
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
181186
NSString *swift_stdlib_getDescription(OpaqueValue *value,
182187
const Metadata *type);

0 commit comments

Comments
 (0)