From c6e59a9280c6c8dff8056d565ea0ace4c2be6c19 Mon Sep 17 00:00:00 2001 From: Curtis Hard Date: Tue, 24 Dec 2019 09:27:06 +0000 Subject: [PATCH] More perf increases --- .../IJSVG/IJSVG/Source/Commands/IJSVGCommand.m | 3 ++- .../IJSVG/Source/Parsing/IJSVGCommandParser.h | 3 ++- .../IJSVG/Source/Parsing/IJSVGCommandParser.m | 18 +++++++++++------- .../IJSVG/IJSVG/Source/Utils/IJSVGUtils.m | 6 ++++-- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Framework/IJSVG/IJSVG/Source/Commands/IJSVGCommand.m b/Framework/IJSVG/IJSVG/Source/Commands/IJSVGCommand.m index 52a048f..2b32e43 100644 --- a/Framework/IJSVG/IJSVG/Source/Commands/IJSVGCommand.m +++ b/Framework/IJSVG/IJSVG/Source/Commands/IJSVGCommand.m @@ -124,7 +124,8 @@ requiredParameters = [self.class requiredParameterCount]; NSInteger sets = 0; IJSVGPathDataSequence* sequence = [self.class pathDataSequence]; - parameters = IJSVGParsePathDataStreamSequence(str, dataStream, sequence, requiredParameters, &sets); + parameters = IJSVGParsePathDataStreamSequence(str.UTF8String, str.length, + dataStream, sequence, requiredParameters, &sets); if (sets <= 1) { CGFloat* subParams = [self parametersFromIndexOffset:0]; diff --git a/Framework/IJSVG/IJSVG/Source/Parsing/IJSVGCommandParser.h b/Framework/IJSVG/IJSVG/Source/Parsing/IJSVGCommandParser.h index 6a88315..2eeb269 100644 --- a/Framework/IJSVG/IJSVG/Source/Parsing/IJSVGCommandParser.h +++ b/Framework/IJSVG/IJSVG/Source/Parsing/IJSVGCommandParser.h @@ -33,7 +33,8 @@ IJSVGPathDataStream* IJSVGPathDataStreamCreate(NSUInteger floatCount, NSUInteger void IJSVGPathDataStreamRelease(IJSVGPathDataStream* buffer); IJSVGPathDataSequence* IJSVGPathDataSequenceCreateWithType(IJSVGPathDataSequence type, NSInteger length); -CGFloat* _Nullable IJSVGParsePathDataStreamSequence(NSString* string, IJSVGPathDataStream* dataStream, IJSVGPathDataSequence* _Nullable sequence, +CGFloat* _Nullable IJSVGParsePathDataStreamSequence(const char* commandChars, NSInteger commandCharLength, + IJSVGPathDataStream* dataStream, IJSVGPathDataSequence* _Nullable sequence, NSInteger commandLength, NSInteger* _Nullable commandsFound); @end diff --git a/Framework/IJSVG/IJSVG/Source/Parsing/IJSVGCommandParser.m b/Framework/IJSVG/IJSVG/Source/Parsing/IJSVGCommandParser.m index 4ad1c1f..96d5c92 100644 --- a/Framework/IJSVG/IJSVG/Source/Parsing/IJSVGCommandParser.m +++ b/Framework/IJSVG/IJSVG/Source/Parsing/IJSVGCommandParser.m @@ -51,8 +51,9 @@ void IJSVGPathDataStreamRelease(IJSVGPathDataStream* buffer) free(buffer); }; -CGFloat* _Nullable IJSVGParsePathDataStreamSequence(NSString* string, IJSVGPathDataStream* dataStream, - IJSVGPathDataSequence* _Nullable sequence, NSInteger commandLength, NSInteger* _Nullable commandsFound) +CGFloat* _Nullable IJSVGParsePathDataStreamSequence(const char* commandChars, NSInteger commandCharLength, + IJSVGPathDataStream* dataStream, IJSVGPathDataSequence* _Nullable sequence, + NSInteger commandLength, NSInteger* _Nullable commandsFound) { // if no command length, its completely pointless function, // so just return null and set commandsFound to 0, if we dont @@ -71,12 +72,12 @@ CGFloat* _Nullable IJSVGParsePathDataStreamSequence(NSString* string, IJSVGPathD NSInteger i = 0; NSInteger counter = 0; - const char* cString = string.UTF8String; + const char* cString = commandChars; const char* validChars = "eE+-."; // this is much faster then doing strlen as it doesnt need // to compute the length - NSInteger sLength = string.length; + NSInteger sLength = commandCharLength; NSInteger sLengthMinusOne = sLength - 1; bool isDecimal = false; @@ -151,17 +152,20 @@ CGFloat* _Nullable IJSVGParsePathDataStreamSequence(NSString* string, IJSVGPathD sizeof(CGFloat) * dataStream->floatCount); } - // add the float + // add the float - for performance reasons, we can simply set the + // null value of the end of the string instead of nulling out + // with memset \0 - huzzah! + dataStream->charBuffer[bufferCount] = '\0'; dataStream->floatBuffer[counter++] = IJSVGParseFloat(dataStream->charBuffer); - // memory clean and counter resets - memset(dataStream->charBuffer, '\0', sizeof(char) * bufferCount); + // reset isDecimal = false; bufferCount = 0; } i++; } + // set commands found - only if there is one if (commandsFound != NULL) { *commandsFound = (NSInteger)round(counter / commandLength); } diff --git a/Framework/IJSVG/IJSVG/Source/Utils/IJSVGUtils.m b/Framework/IJSVG/IJSVG/Source/Utils/IJSVGUtils.m index 31a864e..e386992 100644 --- a/Framework/IJSVG/IJSVG/Source/Utils/IJSVGUtils.m +++ b/Framework/IJSVG/IJSVG/Source/Utils/IJSVGUtils.m @@ -436,7 +436,8 @@ CGFloat degrees_to_radians(CGFloat degrees) size:(NSInteger*)length { IJSVGPathDataStream* stream = IJSVGPathDataStreamCreateDefault(); - CGFloat* floats = IJSVGParsePathDataStreamSequence(string, stream, NULL, 1, length); + CGFloat* floats = IJSVGParsePathDataStreamSequence(string.UTF8String, string.length, + stream, NULL, 1, length); IJSVGPathDataStreamRelease(stream); return floats; } @@ -445,7 +446,8 @@ CGFloat degrees_to_radians(CGFloat degrees) { IJSVGPathDataStream* stream = IJSVGPathDataStreamCreate(4, IJSVG_DATA_STREAM_DEFAULT_BUFFER_COUNT_CHAR); - CGFloat* floats = IJSVGParsePathDataStreamSequence(string, stream, NULL, 1, NULL); + CGFloat* floats = IJSVGParsePathDataStreamSequence(string.UTF8String, + string.length, stream, NULL, 1, NULL); IJSVGPathDataStreamRelease(stream); return floats; }