Skip to content

Commit 9669120

Browse files
committed
Bridged Strings should have some different/additional overrides for performance
1 parent 4d041a3 commit 9669120

File tree

8 files changed

+344
-111
lines changed

8 files changed

+344
-111
lines changed

stdlib/public/SwiftShims/CoreFoundationShims.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,28 @@ _swift_shims_CFStringRef _Nonnull _swift_stdlib_CFStringCreateWithBytes(
104104
SWIFT_RUNTIME_STDLIB_API
105105
const char *_Nullable _swift_stdlib_CFStringGetCStringPtr(
106106
_swift_shims_CFStringRef _Nonnull theString,
107-
108107
_swift_shims_CFStringEncoding encoding);
109108

110109
SWIFT_RUNTIME_STDLIB_API
111110
_swift_shims_CFStringRef _Nonnull _swift_stdlib_objcDebugDescription(
112111
id _Nonnull nsObject);
112+
113+
SWIFT_RUNTIME_STDLIB_API
114+
_swift_shims_CFComparisonResult _swift_stdlib_CFStringCompare(
115+
_swift_shims_CFStringRef _Nonnull string,
116+
_swift_shims_CFStringRef _Nonnull string2);
117+
118+
SWIFT_RUNTIME_STDLIB_API
119+
__swift_uint8_t _swift_stdlib_isNSString(id _Nonnull obj);
120+
121+
SWIFT_RUNTIME_STDLIB_API
122+
_swift_shims_CFHashCode _swift_stdlib_CFStringHashNSString(id _Nonnull obj);
123+
124+
SWIFT_RUNTIME_STDLIB_API
125+
_swift_shims_CFHashCode
126+
_swift_stdlib_CFStringHashCString(const _swift_shims_UInt8 * _Nonnull bytes,
127+
_swift_shims_CFIndex length);
128+
113129
#endif // __OBJC2__
114130

115131
#ifdef __cplusplus

stdlib/public/core/StringBridge.swift

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,30 @@ internal func _cocoaStringSubscript(
6969
return _swift_stdlib_CFStringGetCharacterAtIndex(cfSelf, position)
7070
}
7171

72+
@_effects(readonly)
73+
internal func _cocoaStringCompare(
74+
_ string: _CocoaString, _ other: _CocoaString
75+
) -> Int {
76+
let cfSelf: _swift_shims_CFStringRef = string
77+
let cfOther: _swift_shims_CFStringRef = other
78+
return _swift_stdlib_CFStringCompare(cfSelf, cfOther)
79+
}
80+
81+
@_effects(readonly)
82+
internal func _cocoaHashString(
83+
_ string: _CocoaString
84+
) -> UInt {
85+
return _swift_stdlib_CFStringHashNSString(string)
86+
}
87+
88+
@_effects(readonly)
89+
internal func _cocoaHashASCIIBytes(
90+
_ bytes: UnsafePointer<UInt8>,
91+
length: Int
92+
) -> UInt {
93+
return _swift_stdlib_CFStringHashCString(bytes, length)
94+
}
95+
7296
//
7397
// Conversion from NSString to Swift's native representation
7498
//
@@ -225,36 +249,6 @@ public class __SwiftNativeNSString {
225249
deinit {}
226250
}
227251

228-
/// A shadow for the "core operations" of NSString.
229-
///
230-
/// Covers a set of operations everyone needs to implement in order to
231-
/// be a useful `NSString` subclass.
232-
@objc
233-
public protocol _NSStringCore : _NSCopying /* _NSFastEnumeration */ {
234-
235-
// The following methods should be overridden when implementing an
236-
// NSString subclass.
237-
238-
@objc(length)
239-
var length: Int { get }
240-
241-
@objc(characterAtIndex:)
242-
func character(at index: Int) -> UInt16
243-
244-
// We also override the following methods for efficiency.
245-
246-
@objc(getCharacters:range:)
247-
func getCharacters(
248-
_ buffer: UnsafeMutablePointer<UInt16>,
249-
range aRange: _SwiftNSRange)
250-
251-
@objc(_fastCharacterContents)
252-
func _fastCharacterContents() -> UnsafePointer<UInt16>?
253-
254-
@objc(_fastCStringContents)
255-
func _fastCStringContents() -> UnsafePointer<CChar>?
256-
}
257-
258252
// Called by the SwiftObject implementation to get the description of a value
259253
// as an NSString.
260254
@_silgen_name("swift_stdlib_getDescription")
@@ -264,15 +258,11 @@ public func _getDescription<T>(_ x: T) -> AnyObject {
264258

265259
#else // !_runtime(_ObjC)
266260

267-
@_fixed_layout // FIXME(sil-serialize-all)
268-
public class __SwiftNativeNSString {
269-
@usableFromInline // FIXME(sil-serialize-all)
261+
internal class __SwiftNativeNSString {
270262
internal init() {}
271263
deinit {}
272264
}
273265

274-
public protocol _NSStringCore: class {}
275-
276266
#endif
277267

278268
// Special-case Index <-> Offset converters for bridging and use in accelerating

stdlib/public/core/StringGuts.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ extension _StringGuts {
5555
self.init(_StringObject(immortal: bufPtr, isASCII: isASCII))
5656
}
5757

58-
@inlinable @inline(__always)
58+
@inline(__always)
5959
internal init(_ storage: _StringStorage) {
6060
self.init(_StringObject(storage))
6161
}

stdlib/public/core/StringObject.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ internal struct _StringObject {
8181
@usableFromInline @_frozen
8282
internal enum Variant {
8383
case immortal(UInt)
84-
case native(_AbstractStringStorage)
84+
case native(AnyObject)
8585
case bridged(_CocoaString)
8686

8787
@inlinable @inline(__always)
@@ -1029,7 +1029,6 @@ extension _StringObject {
10291029
}
10301030
}
10311031

1032-
@inlinable
10331032
internal var nativeStorage: _StringStorage {
10341033
@inline(__always) get {
10351034
#if arch(i386) || arch(arm)
@@ -1187,7 +1186,7 @@ extension _StringObject {
11871186
#endif
11881187
}
11891188

1190-
@inlinable @inline(__always)
1189+
@inline(__always)
11911190
internal init(_ storage: _StringStorage) {
11921191
#if arch(i386) || arch(arm)
11931192
self.init(

0 commit comments

Comments
 (0)