Skip to content

Commit f7a28a8

Browse files
committed
Strongly reduce the number of _typeByName() calls — cache the Foundation type, remove assertions.
1 parent 1377ab7 commit f7a28a8

File tree

1 file changed

+4
-33
lines changed

1 file changed

+4
-33
lines changed

stdlib/public/core/BridgeObjectiveC.swift

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -725,8 +725,7 @@ public func swift_unboxFromSwiftValueWithType<T>(
725725
// Internal stdlib SPI
726726
@_silgen_name("swift_swiftValueConformsTo")
727727
public func _swiftValueConformsTo<T>(_ type: T.Type) -> Bool {
728-
_assertSwiftValueFlavorIsConsistent()
729-
if let foundationType = _typeByName("Foundation._SwiftValue") {
728+
if let foundationType = _foundationSwiftValueType {
730729
return foundationType is T.Type
731730
} else {
732731
return _SwiftValue.self is T.Type
@@ -750,38 +749,11 @@ extension Optional: _Unwrappable {
750749
}
751750
}
752751

753-
// This is a best-effort tripmine for detecting the situation
754-
// (which should never happen) of Swift._SwiftValue and
755-
// Foundation._SwiftValue/Foundation.NSNull being used
756-
// in the same process.
757-
758-
@usableFromInline
759-
internal enum _SwiftValueFlavor: Equatable {
760-
case stdlib
761-
case foundation
762-
}
763-
764-
@usableFromInline
765-
func _currentSwiftValueFlavor() -> _SwiftValueFlavor {
766-
if _typeByName("Foundation._SwiftValue") as? _NSSwiftValue.Type != nil {
767-
return .foundation
768-
} else {
769-
return .stdlib
770-
}
771-
}
772-
773-
@usableFromInline
774-
internal var _selectedSwiftValueFlavor: _SwiftValueFlavor = _currentSwiftValueFlavor()
775-
776-
@usableFromInline
777-
internal func _assertSwiftValueFlavorIsConsistent() {
778-
assert(_selectedSwiftValueFlavor == _currentSwiftValueFlavor())
779-
}
752+
private let _foundationSwiftValueType = _typeByName("Foundation._SwiftValue") as? _NSSwiftValue.Type
780753

781754
@usableFromInline
782755
internal var _nullPlaceholder: AnyObject {
783-
_assertSwiftValueFlavorIsConsistent()
784-
if let foundationType = _typeByName("Foundation._SwiftValue") as? _NSSwiftValue.Type {
756+
if let foundationType = _foundationSwiftValueType {
785757
return foundationType.null
786758
} else {
787759
return _SwiftValue.null
@@ -790,8 +762,7 @@ internal var _nullPlaceholder: AnyObject {
790762

791763
@usableFromInline
792764
func _makeSwiftValue(_ value: Any) -> AnyObject {
793-
_assertSwiftValueFlavorIsConsistent()
794-
if let foundationType = _typeByName("Foundation._SwiftValue") as? _NSSwiftValue.Type {
765+
if let foundationType = _foundationSwiftValueType {
795766
return foundationType.init(value)
796767
} else {
797768
return _SwiftValue(value)

0 commit comments

Comments
 (0)