Skip to content

Commit 5c1d6f6

Browse files
authored
Replace calls to CFStringCreateWithCString with CFSTR macro (swiftlang#4717)
This would save performance by removing unnecessary calls to CFStringCreateWithCString. Note: Windows cannot CFSTR to private symbols, so CFXMLInterface is excluded from changes.
1 parent 01f6389 commit 5c1d6f6

File tree

5 files changed

+11
-9
lines changed

5 files changed

+11
-9
lines changed

CoreFoundation/Base.subproj/CFPlatform.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,11 @@ CF_PRIVATE CFStringRef _CFProcessNameString(void) {
278278
static CFStringRef __CFProcessNameString = NULL;
279279
if (!__CFProcessNameString) {
280280
const char *processName = *_CFGetProgname();
281-
if (!processName) processName = "";
282-
CFStringRef newStr = CFStringCreateWithCString(kCFAllocatorSystemDefault, processName, kCFPlatformInterfaceStringEncoding);
281+
CFStringRef newStr;
282+
if (processName)
283+
newStr = CFStringCreateWithCString(kCFAllocatorSystemDefault, processName, kCFPlatformInterfaceStringEncoding);
284+
else
285+
newStr = CFSTR("");
283286
#pragma GCC diagnostic push
284287
#pragma GCC diagnostic ignored "-Wdeprecated"
285288
if (!OSAtomicCompareAndSwapPtrBarrier(NULL, (void *) newStr, (void * volatile *)& __CFProcessNameString)) {
@@ -392,7 +395,7 @@ CF_PRIVATE CFStringRef _CFStringCreateHostName(void) {
392395
char myName[CFMaxHostNameSize];
393396

394397
// return @"" instead of nil a la CFUserName() and Ali Ozer
395-
if (0 != gethostname(myName, CFMaxHostNameSize)) myName[0] = '\0';
398+
if (0 != gethostname(myName, CFMaxHostNameSize)) return CFSTR("");
396399
return CFStringCreateWithCString(kCFAllocatorSystemDefault, myName, kCFPlatformInterfaceStringEncoding);
397400
}
398401

CoreFoundation/Base.subproj/CFRuntime.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ bool (*__CFObjCIsCollectable)(void *) = NULL;
274274
// The constant string class reference is set at link time to _NSCFConstantString
275275
void *__CFConstantStringClassReferencePtr = &_CF_CONSTANT_STRING_SWIFT_CLASS;
276276
#else
277-
#if !__CONSTANT_CFSTRINGS__
277+
#ifndef __CONSTANT_CFSTRINGS__
278278
// Compiler uses this symbol name; must match compiler built-in decl, so we use 'int'
279279
#if TARGET_RT_64_BIT
280280
int __CFConstantStringClassReference[24] = {0};

CoreFoundation/Locale.subproj/CFDateFormatter.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ static CFMutableStringRef __createISO8601FormatString(CFISO8601DateFormatOptions
979979
}
980980

981981
CFDateFormatterRef CFDateFormatterCreateISO8601Formatter(CFAllocatorRef allocator, CFISO8601DateFormatOptions formatOptions) {
982-
CFStringRef localeStr = CFStringCreateWithCString(kCFAllocatorSystemDefault, "en_US_POSIX", kCFStringEncodingUTF8);
982+
CFStringRef localeStr = CFSTR("en_US_POSIX");
983983
CFLocaleRef locale = CFLocaleCreate(kCFAllocatorSystemDefault, localeStr);
984984
CFDateFormatterRef ISO8601Formatter = __CreateCFDateFormatter(allocator, locale, kCFDateFormatterNoStyle, kCFDateFormatterNoStyle, kCFBooleanTrue); // dateStyle and timeStyle are not relevant for ISO8601
985985

@@ -991,7 +991,6 @@ CFDateFormatterRef CFDateFormatterCreateISO8601Formatter(CFAllocatorRef allocato
991991
}
992992
}
993993

994-
CFRelease(localeStr);
995994
CFRelease(locale);
996995

997996
return ISO8601Formatter;

CoreFoundation/Preferences.subproj/CFPreferences.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ CF_PRIVATE CFArrayRef _CFPreferencesCreateDomainList(CFStringRef userName, CFSt
558558
return NULL;
559559
}
560560
if (hostName == kCFPreferencesAnyHost) {
561-
suffix = CFStringCreateWithCString(prefAlloc, ".plist", kCFStringEncodingASCII);
561+
suffix = CFSTR(".plist");
562562
} else if (hostName == kCFPreferencesCurrentHost) {
563563
CFStringRef hostID = _CFPreferencesGetByHostIdentifierString();
564564
suffix = CFStringCreateWithFormat(prefAlloc, NULL, CFSTR(".%@.plist"), hostID);

CoreFoundation/URL.subproj/CFURL.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -4058,9 +4058,9 @@ static CFStringRef WindowsPathToURLPath(CFStringRef path, CFAllocatorRef alloc,
40584058
CFArrayRef urlComponents;
40594059
CFStringRef str;
40604060

4061-
if (CFStringGetLength(path) == 0) return CFStringCreateWithCString(alloc, "", kCFStringEncodingASCII);
4061+
if (CFStringGetLength(path) == 0) return CFSTR("");
40624062
urlComponents = WindowsPathToURLComponents(path, alloc, isDir, isAbsolute);
4063-
if (!urlComponents) return CFStringCreateWithCString(alloc, "", kCFStringEncodingASCII);
4063+
if (!urlComponents) return CFSTR("");
40644064

40654065
// WindowsPathToURLComponents already added percent escapes for us; no need to add them again here.
40664066
str = CFStringCreateByCombiningStrings(alloc, urlComponents, CFSTR("/"));

0 commit comments

Comments
 (0)