Another small optimisation for string creation ...

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@38540 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2015-05-25 11:47:13 +00:00
parent 1fbfe0cdf6
commit 493a170d6a
2 changed files with 35 additions and 26 deletions

View file

@ -458,6 +458,7 @@ literalIsEqualInternal(NXConstantString *s, GSStr o)
*/ */
@interface GSPlaceholderString : NSString @interface GSPlaceholderString : NSString
{ {
NSZone *myZone;
} }
@end @end
@ -1125,6 +1126,13 @@ createTinyString(const char *str, int length)
* on the initialisation method used. * on the initialisation method used.
*/ */
@implementation GSPlaceholderString @implementation GSPlaceholderString
+ (id) allocWithZone: (NSZone*)z
{
GSPlaceholderString *o = NSAllocateObject(self, 0, z);
o->myZone = z;
return o;
}
+ (void) initialize + (void) initialize
{ {
setup(NO); setup(NO);
@ -1327,7 +1335,7 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned,
#if GS_WITH_GC #if GS_WITH_GC
chars = NSAllocateCollectable(length, 0); chars = NSAllocateCollectable(length, 0);
#else #else
chars = NSZoneMalloc([self zone], length); chars = NSZoneMalloc(myZone, length);
#endif #endif
memcpy(chars, bytes, length); memcpy(chars, bytes, length);
} }
@ -1413,13 +1421,13 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned,
*/ */
if (GSPrivateIsCollectable(chars.c) == NO) if (GSPrivateIsCollectable(chars.c) == NO)
{ {
me = newCInline(length, [self zone]); me = newCInline(length, myZone);
memcpy(me->_contents.c, chars.c, length); memcpy(me->_contents.c, chars.c, length);
NSZoneFree(NSZoneFromPointer(chars.c), chars.c); NSZoneFree(NSZoneFromPointer(chars.c), chars.c);
return (id)me; return (id)me;
} }
#endif #endif
me = (GSStr)NSAllocateObject(GSCBufferStringClass, 0, [self zone]); me = (GSStr)NSAllocateObject(GSCBufferStringClass, 0, myZone);
me->_contents.c = chars.c; me->_contents.c = chars.c;
me->_count = length; me->_count = length;
me->_flags.wide = 0; me->_flags.wide = 0;
@ -1444,8 +1452,7 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned,
return nil; // Invalid encoding return nil; // Invalid encoding
} }
if (GSToUnicode(&u, &l, chars.c, length, encoding, if (GSToUnicode(&u, &l, chars.c, length, encoding, myZone, 0) == NO)
[self zone], 0) == NO)
{ {
if (flag == YES && chars.c != 0) if (flag == YES && chars.c != 0)
{ {
@ -1475,7 +1482,7 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned,
if (isASCII == YES if (isASCII == YES
|| (internalEncoding == NSISOLatin1StringEncoding && isLatin1 == YES)) || (internalEncoding == NSISOLatin1StringEncoding && isLatin1 == YES))
{ {
me = (GSStr)newCInline(length, [self zone]); me = (GSStr)newCInline(length, myZone);
while (length-- > 0) while (length-- > 0)
{ {
me->_contents.c[length] = chars.u[length]; me->_contents.c[length] = chars.u[length];
@ -1493,14 +1500,13 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned,
*/ */
if (GSPrivateIsCollectable(chars.u) == NO) if (GSPrivateIsCollectable(chars.u) == NO)
{ {
me = newUInline(length, [self zone]); me = newUInline(length, myZone);
memcpy(me->_contents.u, chars.u, length * sizeof(unichar)); memcpy(me->_contents.u, chars.u, length * sizeof(unichar));
NSZoneFree(NSZoneFromPointer(chars.u), chars.u); NSZoneFree(NSZoneFromPointer(chars.u), chars.u);
return (id)me; return (id)me;
} }
#endif #endif
me = (GSStr)NSAllocateObject(GSUnicodeBufferStringClass, me = (GSStr)NSAllocateObject(GSUnicodeBufferStringClass, 0, myZone);
0, [self zone]);
me->_contents.u = chars.u; me->_contents.u = chars.u;
me->_count = length; me->_count = length;
me->_flags.wide = 1; me->_flags.wide = 1;
@ -1598,12 +1604,12 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned,
*/ */
if (f->_flags.wide == 1) if (f->_flags.wide == 1)
{ {
me = (GSStr)newUInline(f->_count, [self zone]); me = (GSStr)newUInline(f->_count, myZone);
memcpy(me->_contents.u, f->_contents.u, f->_count*sizeof(unichar)); memcpy(me->_contents.u, f->_contents.u, f->_count*sizeof(unichar));
} }
else else
{ {
me = (GSStr)newCInline(f->_count, [self zone]); me = (GSStr)newCInline(f->_count, myZone);
memcpy(me->_contents.c, f->_contents.c, f->_count); memcpy(me->_contents.c, f->_contents.c, f->_count);
} }
@ -1645,7 +1651,7 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned,
* For a GSCString subclass, or an 8-bit GSMutableString, * For a GSCString subclass, or an 8-bit GSMutableString,
* we can copy the bytes directly into an inline string. * we can copy the bytes directly into an inline string.
*/ */
me = (GSStr)newCInline(length, [self zone]); me = (GSStr)newCInline(length, myZone);
memcpy(me->_contents.c, ((GSStr)string)->_contents.c, length); memcpy(me->_contents.c, ((GSStr)string)->_contents.c, length);
} }
else if (GSObjCIsKindOf(c, GSUnicodeStringClass) == YES else if (GSObjCIsKindOf(c, GSUnicodeStringClass) == YES
@ -1655,7 +1661,7 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned,
* For a GSUnicodeString subclass, or a 16-bit GSMutableString, * For a GSUnicodeString subclass, or a 16-bit GSMutableString,
* we can copy the bytes directly into an inline string. * we can copy the bytes directly into an inline string.
*/ */
me = (GSStr)newUInline(length, [self zone]); me = (GSStr)newUInline(length, myZone);
memcpy(me->_contents.u, ((GSStr)string)->_contents.u, memcpy(me->_contents.u, ((GSStr)string)->_contents.u,
length*sizeof(unichar)); length*sizeof(unichar));
} }
@ -1665,7 +1671,7 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned,
* For a string with an unknown class, we can initialise by * For a string with an unknown class, we can initialise by
* having the string copy its content directly into our buffer. * having the string copy its content directly into our buffer.
*/ */
me = (GSStr)newUInline(length, [self zone]); me = (GSStr)newUInline(length, myZone);
[string getCharacters: me->_contents.u]; [string getCharacters: me->_contents.u];
} }
return (id)me; return (id)me;
@ -1676,6 +1682,7 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned,
BOOL ascii = YES; BOOL ascii = YES;
NSUInteger length; NSUInteger length;
GSStr me; GSStr me;
char c;
if (0 == bytes) if (0 == bytes)
{ {
@ -1687,37 +1694,39 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned,
{ {
bytes = &bytes[3]; bytes = &bytes[3];
} }
if (0 == bytes[0])
{
return (id)@"";
}
for (length = 0; bytes[length]; length++) length = 0;
while ((c = bytes[length]))
{ {
if (bytes[length] > 127) length++;
if (c > 127)
{ {
ascii = NO; ascii = NO;
while (bytes[length])
{
length++;
}
break;
} }
} }
if (YES == ascii) if (YES == ascii)
{ {
me = (GSStr)newCInline(length, [self zone]); me = (GSStr)newCInline(length, myZone);
memcpy(me->_contents.c, bytes, length); memcpy(me->_contents.c, bytes, length);
return (id)me; return (id)me;
} }
else else
{ {
const unsigned char *b = (const unsigned char*)bytes; const unsigned char *b = (const unsigned char*)bytes;
NSZone *z = [self zone];
unichar *u = 0; unichar *u = 0;
unsigned l = 0; unsigned l = 0;
if (GSToUnicode(&u, &l, b, length, NSUTF8StringEncoding, z, 0) == NO) if (GSToUnicode(&u, &l, b, length, NSUTF8StringEncoding, myZone, 0) == NO)
{ {
return nil; // Invalid data return nil; // Invalid data
} }
me = (GSStr)NSAllocateObject(GSUnicodeBufferStringClass, 0, z); me = (GSStr)NSAllocateObject(GSUnicodeBufferStringClass, 0, myZone);
me->_contents.u = u; me->_contents.u = u;
me->_count = l; me->_count = l;
me->_flags.wide = 1; me->_flags.wide = 1;

View file

@ -790,7 +790,7 @@ GSICUCollatorOpen(NSStringCompareOptions mask, NSLocale *locale)
* Set up infrastructure for placeholder strings. * Set up infrastructure for placeholder strings.
*/ */
defaultPlaceholderString = (GSPlaceholderString*) defaultPlaceholderString = (GSPlaceholderString*)
NSAllocateObject(GSPlaceholderStringClass, 0, NSDefaultMallocZone()); [GSPlaceholderStringClass allocWithZone: NSDefaultMallocZone()];
placeholderMap = NSCreateMapTable(NSNonOwnedPointerMapKeyCallBacks, placeholderMap = NSCreateMapTable(NSNonOwnedPointerMapKeyCallBacks,
NSNonRetainedObjectMapValueCallBacks, 0); NSNonRetainedObjectMapValueCallBacks, 0);
placeholderLock = [NSLock new]; placeholderLock = [NSLock new];
@ -852,7 +852,7 @@ GSICUCollatorOpen(NSStringCompareOptions mask, NSLocale *locale)
* There is no placeholder object for this zone, so we * There is no placeholder object for this zone, so we
* create a new one and use that. * create a new one and use that.
*/ */
obj = (id)NSAllocateObject(GSPlaceholderStringClass, 0, z); obj = (id)[GSPlaceholderStringClass allocWithZone: z];
NSMapInsert(placeholderMap, (void*)z, (void*)obj); NSMapInsert(placeholderMap, (void*)z, (void*)obj);
} }
[placeholderLock unlock]; [placeholderLock unlock];