Avoid unnecessary memory copying.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@32046 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
stefanbidi 2011-02-11 04:06:00 +00:00
parent fff70cdc5c
commit b31524ca61
2 changed files with 62 additions and 58 deletions

View file

@ -1,3 +1,7 @@
2011-02-11 Stefan Bidigaray <stefanbidi@gmail.com>
* Source/NSDateFormatter.m: Avoid multiple memory copy operations.
2011-02-10 18:54 David Chisnall <theraven@gna.org> 2011-02-10 18:54 David Chisnall <theraven@gna.org>
* libs/base/trunk/config/config.joinable.m, * libs/base/trunk/config/config.joinable.m,

View file

@ -84,11 +84,6 @@ static NSDateFormatterBehavior _defaultBehavior = 0;
- (id) init - (id) init
{ {
int length;
unichar buffer[BUFFER_SIZE];
unichar *value = buffer;
NSInteger err;
self = [super init]; self = [super init];
if (self == nil) if (self == nil)
return nil; return nil;
@ -102,20 +97,29 @@ static NSDateFormatterBehavior _defaultBehavior = 0;
/* According to Apple docs, default behavior is NSDateFormatterBehavior10_4 on /* According to Apple docs, default behavior is NSDateFormatterBehavior10_4 on
10.5 and later. Yeah, go figure. */ 10.5 and later. Yeah, go figure. */
#if GS_USE_ICU == 1 #if GS_USE_ICU == 1
err = U_ZERO_ERROR; {
int length;
length = unichar *value;
udat_toPattern (_formatter, 0, value, BUFFER_SIZE, &err); NSZone *z = [self zone];
if (length > BUFFER_SIZE) UErrorCode err = U_ZERO_ERROR;
{
value = NSZoneMalloc ([self zone], sizeof(unichar) * length); length = udat_toPattern (_formatter, 0, NULL, 0, &err);
udat_toPattern (_formatter, 0, value, length, &err); value = NSZoneMalloc (z, sizeof(unichar) * length);
} err = U_ZERO_ERROR;
udat_toPattern (_formatter, 0, value, length, &err);
_dateFormat = [[NSString alloc] initWithCharacters: value length: length]; if (U_SUCCESS(err))
{
if (length > BUFFER_SIZE) _dateFormat = [[NSString allocWithZone: z]
NSZoneFree ([self zone], value); initWithBytesNoCopy: value
length: length * sizeof(unichar)
encoding: NSUnicodeStringEncoding
freeWhenDone: YES];
}
else
{
NSZoneFree (z, value);
}
}
#endif #endif
return self; return self;
@ -325,37 +329,27 @@ static NSDateFormatterBehavior _defaultBehavior = 0;
#if GS_USE_ICU == 1 #if GS_USE_ICU == 1
NSString *result; NSString *result;
int32_t length; int32_t length;
unichar buffer[BUFFER_SIZE]; unichar *string;
unichar *string = buffer; NSZone *z = [self zone];
UDate udate = [date timeIntervalSince1970] * 1000.0;
UErrorCode err = U_ZERO_ERROR; UErrorCode err = U_ZERO_ERROR;
length = udat_format (_formatter, length = udat_format (_formatter, udate, NULL, 0, NULL, &err);
[date timeIntervalSince1970] * 1000.0, string = NSZoneMalloc (z, sizeof(UChar) * (length + 1));
string, err = U_ZERO_ERROR;
BUFFER_SIZE, udat_format (_formatter, udate, string, length, NULL, &err);
NULL, if (U_SUCCESS(err))
&err);
if (U_FAILURE(err))
return nil;
if (length > BUFFER_SIZE)
{ {
string = NSZoneMalloc ([self zone], sizeof(UChar) * length); result = AUTORELEASE([[NSString allocWithZone: z]
udat_format (_formatter, initWithBytesNoCopy: string
[date timeIntervalSince1970] * 1000.0, length: length * sizeof(UChar)
string, encoding: NSUnicodeStringEncoding
length, freeWhenDone: YES]);
NULL, return result;
&err);
if (U_FAILURE(err))
return nil;
} }
result = [NSString stringWithCharacters: string length: length]; NSZoneFree (z, string);
return nil;
if (length > BUFFER_SIZE)
NSZoneFree ([self zone], string);
return result;
#else #else
return nil; return nil;
#endif #endif
@ -998,22 +992,28 @@ static NSDateFormatterBehavior _defaultBehavior = 0;
while (idx < count) while (idx < count)
{ {
int length; int length;
unichar buffer[BUFFER_SIZE]; unichar *value;
unichar *value = buffer; NSString *str;
UErrorCode err = U_ZERO_ERROR; NSZone *z = [self zone];
UErrorCode err = U_ERROR_LIMIT;
length = length = udat_getSymbols (_formatter, symbol, idx, NULL, 0, &err);
udat_getSymbols (_formatter, symbol, idx, value, BUFFER_SIZE, &err); value = NSZoneMalloc (z, sizeof(unichar) * (length + 1));
if (length > BUFFER_SIZE) err = U_ZERO_ERROR;
udat_getSymbols (_formatter, symbol, idx, value, length, &err);
if (U_SUCCESS(err))
{ {
value = NSZoneMalloc ([self zone], sizeof(unichar) * length); str = AUTORELEASE([[NSString allocWithZone: z]
udat_getSymbols (_formatter, symbol, idx, value, length, &err); initWithBytesNoCopy: value
length: length * sizeof(unichar)
encoding: NSUnicodeStringEncoding
freeWhenDone: YES]);
[mArray addObject: str];
}
else
{
NSZoneFree (z, value);
} }
[mArray addObject: [NSString stringWithCharacters: value length: length]];
if (length > BUFFER_SIZE)
NSZoneFree ([self zone], value);
++idx; ++idx;
} }