Skip to content

[5.1] Avoid the overhead of looking up the current CFAllocator in String bridging #24649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions stdlib/public/SwiftShims/CoreFoundationShims.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ _swift_shims_CFIndex _swift_stdlib_CFStringGetLength(
SWIFT_RUNTIME_STDLIB_API
__attribute__((ns_returns_retained))
_swift_shims_CFStringRef _Nonnull _swift_stdlib_CFStringCreateWithSubstring(
_swift_shims_CFAllocatorRef _Nullable alloc,
const void * _Nullable unused,
_swift_shims_CFStringRef _Nonnull str, _swift_shims_CFRange range);

SWIFT_RUNTIME_STDLIB_API
Expand All @@ -90,13 +90,13 @@ _swift_shims_UniChar _swift_stdlib_CFStringGetCharacterAtIndex(
SWIFT_RUNTIME_STDLIB_API
__attribute__((ns_returns_retained))
_swift_shims_CFStringRef _Nonnull _swift_stdlib_CFStringCreateCopy(
_swift_shims_CFAllocatorRef _Nullable alloc,
const void * _Nullable unused,
_swift_shims_CFStringRef _Nonnull theString);

SWIFT_RUNTIME_STDLIB_API
__attribute__((ns_returns_retained))
_swift_shims_CFStringRef _Nonnull _swift_stdlib_CFStringCreateWithBytes(
_swift_shims_CFAllocatorRef _Nullable alloc,
const void * _Nullable unused,
const __swift_uint8_t *_Nonnull bytes, _swift_shims_CFIndex numBytes,
_swift_shims_CFStringEncoding encoding,
_swift_shims_Boolean isExternalRepresentation);
Expand Down
24 changes: 19 additions & 5 deletions stdlib/public/core/StringBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -266,18 +266,32 @@ extension String {
}
}

@_effects(releasenone)
private func _createCFString(
_ ptr: UnsafePointer<UInt8>,
_ count: Int,
_ encoding: UInt32
) -> AnyObject {
return _swift_stdlib_CFStringCreateWithBytes(
nil, //ignored in the shim for perf reasons
ptr,
count,
kCFStringEncodingUTF8,
0
) as AnyObject
}

extension String {
@_effects(releasenone)
public // SPI(Foundation)
func _bridgeToObjectiveCImpl() -> AnyObject {
if _guts.isSmall {
return _guts.asSmall.withUTF8 { bufPtr in
// TODO(String bridging): worth isASCII check for different encoding?
return _swift_stdlib_CFStringCreateWithBytes(
nil, bufPtr.baseAddress._unsafelyUnwrappedUnchecked,
return _createCFString(
bufPtr.baseAddress._unsafelyUnwrappedUnchecked,
bufPtr.count,
kCFStringEncodingUTF8, 0)
as AnyObject
kCFStringEncodingUTF8
)
}
}
if _guts._object.isImmortal {
Expand Down
17 changes: 11 additions & 6 deletions stdlib/public/stubs/FoundationHelpers.mm
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,13 @@ static CFRange cast(_swift_shims_CFRange value) {

_swift_shims_CFStringRef
swift::_swift_stdlib_CFStringCreateWithSubstring(
_swift_shims_CFAllocatorRef alloc,
const void *unused,
_swift_shims_CFStringRef str,
_swift_shims_CFRange range) {
return cast(CFStringCreateWithSubstring(cast(alloc), cast(str), cast(range)));
assert(unused == NULL);
return cast(CFStringCreateWithSubstring(kCFAllocatorSystemDefault,
cast(str),
cast(range)));
}

_swift_shims_CFComparisonResult
Expand Down Expand Up @@ -108,17 +111,19 @@ static CFRange cast(_swift_shims_CFRange value) {
}

_swift_shims_CFStringRef
swift::_swift_stdlib_CFStringCreateCopy(_swift_shims_CFAllocatorRef alloc,
swift::_swift_stdlib_CFStringCreateCopy(const void *unused,
_swift_shims_CFStringRef theString) {
return cast(CFStringCreateCopy(cast(alloc), cast(theString)));
assert(unused == NULL);
return cast(CFStringCreateCopy(kCFAllocatorSystemDefault, cast(theString)));
}

_swift_shims_CFStringRef
swift::_swift_stdlib_CFStringCreateWithBytes(
_swift_shims_CFAllocatorRef _Nullable alloc, const uint8_t *bytes,
const void *unused, const uint8_t *bytes,
_swift_shims_CFIndex numBytes, _swift_shims_CFStringEncoding encoding,
_swift_shims_Boolean isExternalRepresentation) {
return cast(CFStringCreateWithBytes(cast(alloc), bytes, numBytes,
assert(unused == NULL);
return cast(CFStringCreateWithBytes(kCFAllocatorSystemDefault, bytes, numBytes,
cast(encoding),
isExternalRepresentation));
}
Expand Down