-
-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: optimize some conversions of unsigned ints to NSString (#2863)
- Loading branch information
1 parent
a4ee85b
commit c0ff306
Showing
13 changed files
with
104 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#import <Foundation/Foundation.h> | ||
|
||
// 2 for the 0x prefix, plus 16 for the hex value, plus 1 for the null terminator | ||
#define SENTRY_HEX_ADDRESS_LENGTH 19 | ||
|
||
static inline NSString * | ||
sentry_snprintfHexAddress(uint64_t value) | ||
{ | ||
char buffer[SENTRY_HEX_ADDRESS_LENGTH]; | ||
snprintf(buffer, SENTRY_HEX_ADDRESS_LENGTH, "0x%016llx", value); | ||
NSString *nsString = [NSString stringWithCString:buffer encoding:NSASCIIStringEncoding]; | ||
return nsString; | ||
} | ||
|
||
static inline NSString * | ||
sentry_stringForUInt64(uint64_t value) | ||
{ | ||
int bufferSize = snprintf(NULL, 0, "%llu", value) + 1; | ||
char *buffer = (char *)malloc(bufferSize); | ||
snprintf(buffer, bufferSize, "%llu", value); | ||
NSString *nsString = [NSString stringWithCString:buffer encoding:NSASCIIStringEncoding]; | ||
free(buffer); | ||
return nsString; | ||
} | ||
|
||
static inline NSString * | ||
sentry_formatHexAddress(NSNumber *value) | ||
{ | ||
/* | ||
* We observed a 41% speedup by using snprintf vs +[NSString stringWithFormat:]. In a trial | ||
* using a profile, we observed the +[NSString stringWithFormat:] using 282ms of CPU time, vs | ||
* 164ms of CPU time for snprintf. There is also an assumed space improvement due to not needing | ||
* to allocate as many instances of NSString, like for the format string literal, instead only | ||
* using stack-bound C strings. | ||
*/ | ||
return sentry_snprintfHexAddress([value unsignedLongLongValue]); | ||
} | ||
|
||
static inline NSString * | ||
sentry_formatHexAddressUInt64(uint64_t value) | ||
{ | ||
return sentry_snprintfHexAddress(value); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import XCTest | ||
|
||
final class SentryFormatterTests: XCTestCase { | ||
func testFormatHexAddress() { | ||
for (input, expected) in [ | ||
(0x000000008e902bf0, "0x000000008e902bf0"), | ||
(0x000000008fd09c40, "0x000000008fd09c40"), | ||
(0x00000000945b1c00, "0x00000000945b1c00") | ||
] { | ||
XCTAssertEqual(sentry_formatHexAddress(input as NSNumber), expected) | ||
} | ||
} | ||
|
||
func testStringForUInt64() { | ||
for (input, expected) in [ | ||
(0, "0"), | ||
(1, "1"), | ||
(123_456, "123456"), | ||
(UInt64.max, "18446744073709551615") | ||
] { | ||
XCTAssertEqual(sentry_stringForUInt64(UInt64(input)), expected) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters