From 197fdeaecbb4a931577b700131f64ef2acca5569 Mon Sep 17 00:00:00 2001 From: Richard Frith-MacDonald Date: Fri, 1 Jul 2016 15:22:02 +0000 Subject: [PATCH] Optimisation for ICU access to immutable NSString objects ... we don't need to call -length every time because we can keep the length in the UText state. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@39956 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 5 ++++ Source/GSICUString.m | 60 ++++++++++++++++++++++++-------------------- 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4dbdb3989..c901deb52 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2016-07-01 Richard Frith-Macdonald + + * Source/GSICUString.m: For immutable strings, cache the string + length in the UText structure to avoid repeated calls to -length + 2016-06-30 Richard Frith-Macdonald * Source/GSICUString.m: Re-implement the function to let ICU access diff --git a/Source/GSICUString.m b/Source/GSICUString.m index 98580a4cb..05dfe2fe5 100644 --- a/Source/GSICUString.m +++ b/Source/GSICUString.m @@ -40,7 +40,10 @@ static const NSUInteger chunkSize = 32; static int64_t UTextNSStringNativeLength(UText *ut) { - return [(NSString*)ut->p length]; + /* For constant strings the length is stored in ut->c, but for mutable + * strings this is set to -1 and we must check the length every time. + */ + return (-1 == ut->c) ? [(NSString*)ut->p length] : ut->c; } @@ -52,7 +55,7 @@ UBool UTextNSStringAccess(UText *ut, int64_t nativeIndex, UBool forward) { NSString *str = (NSString*)ut->p; - NSUInteger length = [str length]; + NSUInteger length = (-1 == ut->c) ? [str length] : ut->c; NSUInteger nativeStart = ut->chunkNativeStart; NSUInteger nativeLimit = ut->chunkNativeLimit; NSRange r; @@ -153,9 +156,10 @@ UTextNSMutableStringReplace(UText *ut, } else { - replacement = [replacement initWithCharactersNoCopy: (unichar*)replacementText - length: replacmentLength - freeWhenDone: NO]; + replacement = [replacement + initWithCharactersNoCopy: (unichar*)replacementText + length: replacmentLength + freeWhenDone: NO]; } [str replaceCharactersInRange: r withString: replacement]; @@ -185,34 +189,35 @@ UTextNSStringExtract(UText *ut, int32_t destCapacity, UErrorCode *status) { - NSString *str; - NSUInteger length; - NSRange r; - /* If we're loading no characters, we are expected to return the number of * characters that we could load if requested. */ - if (destCapacity == 0) + if (destCapacity <= 0) { return nativeLimit - nativeStart; } - str = (NSString*)ut->p; - length = [str length]; - if (nativeLimit > length) + else { - nativeLimit = length; + NSString *str = (NSString*)ut->p; + NSUInteger length = (-1 == ut->c) ? [str length] : ut->c; + NSRange r; + + if (nativeLimit > length) + { + nativeLimit = length; + } + r = NSMakeRange(nativeStart, nativeLimit - nativeStart ); + if (destCapacity < r.length) + { + r.length = destCapacity; + } + [str getCharacters: dest range: r]; + if (destCapacity > r.length) + { + dest[r.length] = 0; + } + return r.length; } - r = NSMakeRange(nativeStart, nativeLimit - nativeStart ); - if (destCapacity < r.length) - { - r.length = destCapacity; - } - [str getCharacters: dest range: r]; - if (destCapacity > r.length) - { - dest[r.length] = 0; - } - return r.length; } /** @@ -226,7 +231,7 @@ void UTextNSStringCopy(UText *ut, UErrorCode *status) { NSMutableString *str = (NSMutableString*)ut->p; - NSUInteger length = [str length]; + NSUInteger length = (-1 == ut->c) ? [str length] : ut->c; NSRange r; NSString *substr; @@ -374,7 +379,7 @@ UTextInitWithNSMutableString(UText *txt, NSMutableString *str) txt->pFuncs = &NSMutableStringFuncs; txt->chunkContents = txt->pExtra; txt->nativeIndexingLimit = INT32_MAX; - + txt->c = -1; // Need to fetch length every time txt->providerProperties = 1<pFuncs = &NSStringFuncs; txt->chunkContents = txt->pExtra; txt->nativeIndexingLimit = INT32_MAX; + txt->c = [str length]; return txt; }