From 27365f946b3e2268cd82601c4ccd3f6f65f9cf32 Mon Sep 17 00:00:00 2001 From: William Morriss Date: Fri, 13 Jul 2018 19:00:18 -0700 Subject: [PATCH 1/4] speed up BTCHexFromData by 85% --- CoreBitcoin/BTCData.m | 46 +++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/CoreBitcoin/BTCData.m b/CoreBitcoin/BTCData.m index 85e2bd67..254e362b 100644 --- a/CoreBitcoin/BTCData.m +++ b/CoreBitcoin/BTCData.m @@ -183,20 +183,36 @@ static inline int BTCFairCoinFlip() { return [[NSData alloc] initWithBytesNoCopy:buf length:len/2]; } - -NSString* BTCHexFromDataWithFormat(NSData* data, const char* format) { - if (!data) return nil; - - NSUInteger length = data.length; - if (length == 0) return @""; - - NSMutableData* resultdata = [NSMutableData dataWithLength:length * 2]; - char *dest = resultdata.mutableBytes; - unsigned const char *src = data.bytes; - for (int i = 0; i < length; ++i) { - sprintf(dest + i*2, format, (unsigned int)(src[i])); +static char hexValues[16] = { + '0', '1', '2', '3', + '4', '5', '6', '7', + '8', '9', 'a', 'b', + 'c', 'd', 'e', 'f', +}; + +static char upperHexValues[16] = { + '0', '1', '2', '3', + '4', '5', '6', '7', + '8', '9', 'A', 'B', + 'C', 'D', 'E', 'F', +}; + +static NSString* BTCHexFromDataWithCharset(NSData* data, const char* charset) { + size_t inLength = data.length; + size_t length = inLength * 2; + uint8_t *outString = malloc(length); + const unsigned char *bytes = data.bytes; + for (uint32_t i = 0; i < inLength; i++) { + unsigned char byte = bytes[i]; + outString[i*2] = charset[(byte & 0xF0) >> 4]; + outString[i*2+1] = charset[(byte & 0x0F)]; } - return [[NSString alloc] initWithData:resultdata encoding:NSASCIIStringEncoding]; + return [[NSString alloc] + initWithBytesNoCopy:outString + length:length + encoding:NSUTF8StringEncoding + freeWhenDone:YES + ]; } NSString* BTCHexStringFromData(NSData* data) { // deprecated @@ -208,11 +224,11 @@ static inline int BTCFairCoinFlip() { } NSString* BTCHexFromData(NSData* data) { - return BTCHexFromDataWithFormat(data, "%02x"); + return BTCHexFromDataWithCharset(data, hexValues); } NSString* BTCUppercaseHexFromData(NSData* data) { - return BTCHexFromDataWithFormat(data, "%02X"); + return BTCHexFromDataWithCharset(data, upperHexValues); } From ee9ff9d4aef6b517c4855f1f41221e9387d9242a Mon Sep 17 00:00:00 2001 From: William Morriss Date: Fri, 13 Jul 2018 19:12:28 -0700 Subject: [PATCH 2/4] const char arrays --- CoreBitcoin/BTCData.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CoreBitcoin/BTCData.m b/CoreBitcoin/BTCData.m index 254e362b..c2058336 100644 --- a/CoreBitcoin/BTCData.m +++ b/CoreBitcoin/BTCData.m @@ -183,14 +183,14 @@ static inline int BTCFairCoinFlip() { return [[NSData alloc] initWithBytesNoCopy:buf length:len/2]; } -static char hexValues[16] = { +static const char hexValues[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', }; -static char upperHexValues[16] = { +static const char upperHexValues[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', From ba7075027a7b9679b74116aa084cd411d3ae533b Mon Sep 17 00:00:00 2001 From: William Morriss Date: Fri, 13 Jul 2018 19:14:34 -0700 Subject: [PATCH 3/4] no need to mask --- CoreBitcoin/BTCData.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CoreBitcoin/BTCData.m b/CoreBitcoin/BTCData.m index c2058336..f7cde485 100644 --- a/CoreBitcoin/BTCData.m +++ b/CoreBitcoin/BTCData.m @@ -204,8 +204,8 @@ static inline int BTCFairCoinFlip() { const unsigned char *bytes = data.bytes; for (uint32_t i = 0; i < inLength; i++) { unsigned char byte = bytes[i]; - outString[i*2] = charset[(byte & 0xF0) >> 4]; - outString[i*2+1] = charset[(byte & 0x0F)]; + outString[i*2] = charset[byte >> 4]; + outString[i*2+1] = charset[byte & 0x0F]; } return [[NSString alloc] initWithBytesNoCopy:outString From 37b1f0b5a5272402692ec697d5bd8260c828455a Mon Sep 17 00:00:00 2001 From: William Morriss Date: Fri, 13 Jul 2018 19:22:35 -0700 Subject: [PATCH 4/4] ascii encoding is somewhat faster --- CoreBitcoin/BTCData.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CoreBitcoin/BTCData.m b/CoreBitcoin/BTCData.m index f7cde485..a7fb6b4d 100644 --- a/CoreBitcoin/BTCData.m +++ b/CoreBitcoin/BTCData.m @@ -210,7 +210,7 @@ static inline int BTCFairCoinFlip() { return [[NSString alloc] initWithBytesNoCopy:outString length:length - encoding:NSUTF8StringEncoding + encoding:NSASCIIStringEncoding freeWhenDone:YES ]; }