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
This commit is contained in:
Richard Frith-MacDonald 2016-07-01 15:22:02 +00:00
parent d725240fbd
commit 197fdeaecb
2 changed files with 38 additions and 27 deletions

View file

@ -1,3 +1,8 @@
2016-07-01 Richard Frith-Macdonald <rfm@gnu.org>
* 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 <rfm@gnu.org> 2016-06-30 Richard Frith-Macdonald <rfm@gnu.org>
* Source/GSICUString.m: Re-implement the function to let ICU access * Source/GSICUString.m: Re-implement the function to let ICU access

View file

@ -40,7 +40,10 @@ static const NSUInteger chunkSize = 32;
static int64_t static int64_t
UTextNSStringNativeLength(UText *ut) 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) UTextNSStringAccess(UText *ut, int64_t nativeIndex, UBool forward)
{ {
NSString *str = (NSString*)ut->p; NSString *str = (NSString*)ut->p;
NSUInteger length = [str length]; NSUInteger length = (-1 == ut->c) ? [str length] : ut->c;
NSUInteger nativeStart = ut->chunkNativeStart; NSUInteger nativeStart = ut->chunkNativeStart;
NSUInteger nativeLimit = ut->chunkNativeLimit; NSUInteger nativeLimit = ut->chunkNativeLimit;
NSRange r; NSRange r;
@ -153,9 +156,10 @@ UTextNSMutableStringReplace(UText *ut,
} }
else else
{ {
replacement = [replacement initWithCharactersNoCopy: (unichar*)replacementText replacement = [replacement
length: replacmentLength initWithCharactersNoCopy: (unichar*)replacementText
freeWhenDone: NO]; length: replacmentLength
freeWhenDone: NO];
} }
[str replaceCharactersInRange: r withString: replacement]; [str replaceCharactersInRange: r withString: replacement];
@ -185,34 +189,35 @@ UTextNSStringExtract(UText *ut,
int32_t destCapacity, int32_t destCapacity,
UErrorCode *status) UErrorCode *status)
{ {
NSString *str;
NSUInteger length;
NSRange r;
/* If we're loading no characters, we are expected to return the number of /* If we're loading no characters, we are expected to return the number of
* characters that we could load if requested. * characters that we could load if requested.
*/ */
if (destCapacity == 0) if (destCapacity <= 0)
{ {
return nativeLimit - nativeStart; return nativeLimit - nativeStart;
} }
str = (NSString*)ut->p; else
length = [str length];
if (nativeLimit > length)
{ {
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) UErrorCode *status)
{ {
NSMutableString *str = (NSMutableString*)ut->p; NSMutableString *str = (NSMutableString*)ut->p;
NSUInteger length = [str length]; NSUInteger length = (-1 == ut->c) ? [str length] : ut->c;
NSRange r; NSRange r;
NSString *substr; NSString *substr;
@ -374,7 +379,7 @@ UTextInitWithNSMutableString(UText *txt, NSMutableString *str)
txt->pFuncs = &NSMutableStringFuncs; txt->pFuncs = &NSMutableStringFuncs;
txt->chunkContents = txt->pExtra; txt->chunkContents = txt->pExtra;
txt->nativeIndexingLimit = INT32_MAX; txt->nativeIndexingLimit = INT32_MAX;
txt->c = -1; // Need to fetch length every time
txt->providerProperties = 1<<UTEXT_PROVIDER_WRITABLE; txt->providerProperties = 1<<UTEXT_PROVIDER_WRITABLE;
return txt; return txt;
@ -395,6 +400,7 @@ UTextInitWithNSString(UText *txt, NSString *str)
txt->pFuncs = &NSStringFuncs; txt->pFuncs = &NSStringFuncs;
txt->chunkContents = txt->pExtra; txt->chunkContents = txt->pExtra;
txt->nativeIndexingLimit = INT32_MAX; txt->nativeIndexingLimit = INT32_MAX;
txt->c = [str length];
return txt; return txt;
} }