Skip to content

Commit a2bb46e

Browse files
authored
Merge pull request #24649 from Catfish-Man/thread-specific-doh-5.1
[5.1] Avoid the overhead of looking up the current CFAllocator in String bridging
2 parents fcd0a6d + a26ab69 commit a2bb46e

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

stdlib/public/SwiftShims/CoreFoundationShims.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ _swift_shims_CFIndex _swift_stdlib_CFStringGetLength(
8080
SWIFT_RUNTIME_STDLIB_API
8181
__attribute__((ns_returns_retained))
8282
_swift_shims_CFStringRef _Nonnull _swift_stdlib_CFStringCreateWithSubstring(
83-
_swift_shims_CFAllocatorRef _Nullable alloc,
83+
const void * _Nullable unused,
8484
_swift_shims_CFStringRef _Nonnull str, _swift_shims_CFRange range);
8585

8686
SWIFT_RUNTIME_STDLIB_API
@@ -90,13 +90,13 @@ _swift_shims_UniChar _swift_stdlib_CFStringGetCharacterAtIndex(
9090
SWIFT_RUNTIME_STDLIB_API
9191
__attribute__((ns_returns_retained))
9292
_swift_shims_CFStringRef _Nonnull _swift_stdlib_CFStringCreateCopy(
93-
_swift_shims_CFAllocatorRef _Nullable alloc,
93+
const void * _Nullable unused,
9494
_swift_shims_CFStringRef _Nonnull theString);
9595

9696
SWIFT_RUNTIME_STDLIB_API
9797
__attribute__((ns_returns_retained))
9898
_swift_shims_CFStringRef _Nonnull _swift_stdlib_CFStringCreateWithBytes(
99-
_swift_shims_CFAllocatorRef _Nullable alloc,
99+
const void * _Nullable unused,
100100
const __swift_uint8_t *_Nonnull bytes, _swift_shims_CFIndex numBytes,
101101
_swift_shims_CFStringEncoding encoding,
102102
_swift_shims_Boolean isExternalRepresentation);

stdlib/public/core/StringBridge.swift

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,18 +261,32 @@ extension String {
261261
}
262262
}
263263

264+
@_effects(releasenone)
265+
private func _createCFString(
266+
_ ptr: UnsafePointer<UInt8>,
267+
_ count: Int,
268+
_ encoding: UInt32
269+
) -> AnyObject {
270+
return _swift_stdlib_CFStringCreateWithBytes(
271+
nil, //ignored in the shim for perf reasons
272+
ptr,
273+
count,
274+
kCFStringEncodingUTF8,
275+
0
276+
) as AnyObject
277+
}
278+
264279
extension String {
265280
@_effects(releasenone)
266281
public // SPI(Foundation)
267282
func _bridgeToObjectiveCImpl() -> AnyObject {
268283
if _guts.isSmall {
269284
return _guts.asSmall.withUTF8 { bufPtr in
270-
// TODO(String bridging): worth isASCII check for different encoding?
271-
return _swift_stdlib_CFStringCreateWithBytes(
272-
nil, bufPtr.baseAddress._unsafelyUnwrappedUnchecked,
285+
return _createCFString(
286+
bufPtr.baseAddress._unsafelyUnwrappedUnchecked,
273287
bufPtr.count,
274-
kCFStringEncodingUTF8, 0)
275-
as AnyObject
288+
kCFStringEncodingUTF8
289+
)
276290
}
277291
}
278292
if _guts._object.isImmortal {

stdlib/public/stubs/FoundationHelpers.mm

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,13 @@ static CFRange cast(_swift_shims_CFRange value) {
7777

7878
_swift_shims_CFStringRef
7979
swift::_swift_stdlib_CFStringCreateWithSubstring(
80-
_swift_shims_CFAllocatorRef alloc,
80+
const void *unused,
8181
_swift_shims_CFStringRef str,
8282
_swift_shims_CFRange range) {
83-
return cast(CFStringCreateWithSubstring(cast(alloc), cast(str), cast(range)));
83+
assert(unused == NULL);
84+
return cast(CFStringCreateWithSubstring(kCFAllocatorSystemDefault,
85+
cast(str),
86+
cast(range)));
8487
}
8588

8689
_swift_shims_CFComparisonResult
@@ -108,17 +111,19 @@ static CFRange cast(_swift_shims_CFRange value) {
108111
}
109112

110113
_swift_shims_CFStringRef
111-
swift::_swift_stdlib_CFStringCreateCopy(_swift_shims_CFAllocatorRef alloc,
114+
swift::_swift_stdlib_CFStringCreateCopy(const void *unused,
112115
_swift_shims_CFStringRef theString) {
113-
return cast(CFStringCreateCopy(cast(alloc), cast(theString)));
116+
assert(unused == NULL);
117+
return cast(CFStringCreateCopy(kCFAllocatorSystemDefault, cast(theString)));
114118
}
115119

116120
_swift_shims_CFStringRef
117121
swift::_swift_stdlib_CFStringCreateWithBytes(
118-
_swift_shims_CFAllocatorRef _Nullable alloc, const uint8_t *bytes,
122+
const void *unused, const uint8_t *bytes,
119123
_swift_shims_CFIndex numBytes, _swift_shims_CFStringEncoding encoding,
120124
_swift_shims_Boolean isExternalRepresentation) {
121-
return cast(CFStringCreateWithBytes(cast(alloc), bytes, numBytes,
125+
assert(unused == NULL);
126+
return cast(CFStringCreateWithBytes(kCFAllocatorSystemDefault, bytes, numBytes,
122127
cast(encoding),
123128
isExternalRepresentation));
124129
}

0 commit comments

Comments
 (0)