Performance optimisations - mimimise calls to malloc and avoid method despatch

overhead on deaalloc.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@7925 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 2000-10-29 14:52:33 +00:00
parent cf395d3ea6
commit 379795ebe4
3 changed files with 102 additions and 36 deletions

View file

@ -1,3 +1,11 @@
2000-10-29 Richard Frith-Macdonald <rfm@gnu.org>
* Source/GSString.m: New inline string classes added to avoid calls
to malloc in some cases. Changed all deallocation methods to call
NSDeallocateObject() directy for efficiency.
* Source/NSString.m: Use new inline string classes for creation of
strings where we know the length of the data in advaance.
2000-10-28 Mirko Viviani <mirko.viviani@rccr.cremona.it> 2000-10-28 Mirko Viviani <mirko.viviani@rccr.cremona.it>
* Headers/gnustep/base/NSBundle.h: added ivar. * Headers/gnustep/base/NSBundle.h: added ivar.

View file

@ -58,7 +58,16 @@
@end @end
/* /*
* GSCSubString - concrete subclass of GSCString, that relys on the * GSCInlineString - concrete subclass of GSCString, that expects the
* characterData to appear in memory immediately after the object itsself.
*/
@interface GSCInlineString : GSCString
{
}
@end
/*
* GSCSubString - concrete subclass of GSCString, that relies on the
* data stored in a GSCString object. * data stored in a GSCString object.
*/ */
@interface GSCSubString : GSCString @interface GSCSubString : GSCString
@ -76,6 +85,15 @@
} }
@end @end
/*
* GSUInlineString - concrete subclass of GSUString, that expects the
* characterData to appear in memory immediately after the object itsself.
*/
@interface GSUInlineString : GSUString
{
}
@end
/* /*
* GSUSubString - concrete subclass of GSUString, that relys on the * GSUSubString - concrete subclass of GSUString, that relys on the
* data stored in a GSUString object. * data stored in a GSUString object.
@ -160,9 +178,11 @@ static Class NSDataClass = 0;
static Class NSStringClass = 0; static Class NSStringClass = 0;
static Class GSStringClass = 0; static Class GSStringClass = 0;
static Class GSCStringClass = 0; static Class GSCStringClass = 0;
static Class GSCInlineStringClass = 0;
static Class GSCSubStringClass = 0; static Class GSCSubStringClass = 0;
static Class GSUStringClass = 0; static Class GSUStringClass = 0;
static Class GSUSubStringClass = 0; static Class GSUSubStringClass = 0;
static Class GSUInlineStringClass = 0;
static Class GSMStringClass = 0; static Class GSMStringClass = 0;
static Class NXConstantStringClass = 0; static Class NXConstantStringClass = 0;
@ -193,6 +213,8 @@ setup()
GSStringClass = [GSString class]; GSStringClass = [GSString class];
GSCStringClass = [GSCString class]; GSCStringClass = [GSCString class];
GSUStringClass = [GSUString class]; GSUStringClass = [GSUString class];
GSCInlineStringClass = [GSCInlineString class];
GSUInlineStringClass = [GSUInlineString class];
GSCSubStringClass = [GSCSubString class]; GSCSubStringClass = [GSCSubString class];
GSUSubStringClass = [GSUSubString class]; GSUSubStringClass = [GSUSubString class];
GSMStringClass = [GSMString class]; GSMStringClass = [GSMString class];
@ -1204,7 +1226,7 @@ transmute(ivars self, NSString *aString)
NSZoneFree(NSZoneFromPointer(_contents.c), _contents.c); NSZoneFree(NSZoneFromPointer(_contents.c), _contents.c);
_contents.c = 0; _contents.c = 0;
} }
[super dealloc]; NSDeallocateObject(self);
} }
- (id) initWithCharactersNoCopy: (unichar*)chars - (id) initWithCharactersNoCopy: (unichar*)chars
@ -1300,17 +1322,10 @@ transmute(ivars self, NSString *aString)
{ {
if (NSShouldRetainWithZone(self, z) == NO) if (NSShouldRetainWithZone(self, z) == NO)
{ {
GSCString *obj; NSString *obj;
obj = (GSCString*)NSCopyObject(self, 0, z); obj = (NSString*)NSAllocateObject(GSCInlineStringClass, _count, z);
if (_contents.c != 0) obj = [obj initWithCString: _contents.c length: _count];
{
unsigned char *tmp;
tmp = NSZoneMalloc(z, _count);
memcpy(tmp, _contents.c, _count);
obj->_contents.c = tmp;
}
return obj; return obj;
} }
else else
@ -1483,11 +1498,26 @@ transmute(ivars self, NSString *aString)
@implementation GSCInlineString
- (id) initWithCString: (const char*)chars length: (unsigned)length
{
_count = length;
_contents.c = (unsigned char*)&self[1];
memcpy(_contents.c, chars, length);
_flags.wide = 0;
return self;
}
- (void) dealloc
{
NSDeallocateObject(self);
}
@end
@implementation GSCSubString @implementation GSCSubString
- (void) dealloc - (void) dealloc
{ {
RELEASE(_parent); RELEASE(_parent);
[super dealloc]; NSDeallocateObject(self);
} }
@end @end
@ -1559,17 +1589,10 @@ transmute(ivars self, NSString *aString)
{ {
if (NSShouldRetainWithZone(self, z) == NO) if (NSShouldRetainWithZone(self, z) == NO)
{ {
GSUString *obj; NSString *obj;
obj = (GSUString*)NSCopyObject(self, 0, z); obj = (NSString*)NSAllocateObject(GSUInlineStringClass, _count*2, z);
if (_contents.u != 0) obj = [obj initWithCharacters: _contents.u length: _count];
{
unichar *tmp;
tmp = NSZoneMalloc(z, _count*sizeof(unichar));
memcpy(tmp, _contents.u, _count*sizeof(unichar));
obj->_contents.u = tmp;
}
return obj; return obj;
} }
else else
@ -1751,11 +1774,26 @@ transmute(ivars self, NSString *aString)
@implementation GSUInlineString
- (id) initWithCharacters: (const unichar*)chars length: (unsigned)length
{
_count = length;
_contents.u = (unichar*)&self[1];
memcpy(_contents.u, chars, length*sizeof(unichar));
_flags.wide = 1;
return self;
}
- (void) dealloc
{
NSDeallocateObject(self);
}
@end
@implementation GSUSubString @implementation GSUSubString
- (void) dealloc - (void) dealloc
{ {
RELEASE(_parent); RELEASE(_parent);
[super dealloc]; NSDeallocateObject(self);
} }
@end @end
@ -1835,12 +1873,13 @@ transmute(ivars self, NSString *aString)
if (_flags.wide == 1) if (_flags.wide == 1)
{ {
copy = [GSUStringClass allocWithZone: z]; copy = (NSString*)NSAllocateObject(GSUInlineStringClass,
_count*sizeof(unichar), z);
copy = [copy initWithCharacters: _contents.u length: _count]; copy = [copy initWithCharacters: _contents.u length: _count];
} }
else else
{ {
copy = [GSCStringClass allocWithZone: z]; copy = (NSString*)NSAllocateObject(GSCInlineStringClass, _count, z);
copy = [copy initWithCString: _contents.c length: _count]; copy = [copy initWithCString: _contents.c length: _count];
} }
return copy; return copy;
@ -1879,7 +1918,7 @@ transmute(ivars self, NSString *aString)
self->_contents.c = 0; self->_contents.c = 0;
self->_zone = 0; self->_zone = 0;
} }
[super dealloc]; NSDeallocateObject(self);
} }
- (void) deleteCharactersInRange: (NSRange)range - (void) deleteCharactersInRange: (NSRange)range

View file

@ -8,7 +8,7 @@
Date: February 1997 Date: February 1997
Optimisations by Richard Frith-Macdonald <richard@brainstorm.co.uk> Optimisations by Richard Frith-Macdonald <richard@brainstorm.co.uk>
Date: October 1998 Date: October 1998 - 2000
This file is part of the GNUstep Base Library. This file is part of the GNUstep Base Library.
@ -75,6 +75,8 @@
@class GSString; @class GSString;
@class GSMString; @class GSMString;
@class GSUString; @class GSUString;
@class GSCInlineString;
@class GSUInlineString;
@class NSGMutableArray; @class NSGMutableArray;
@class NSGMutableDictionary; @class NSGMutableDictionary;
@ -89,6 +91,8 @@ static Class NSMutableStringClass;
static Class GSStringClass; static Class GSStringClass;
static Class GSMStringClass; static Class GSMStringClass;
static Class GSUStringClass; static Class GSUStringClass;
static Class GSCInlineStringClass;
static Class GSUInlineStringClass;
static Class plArray; static Class plArray;
static id (*plAdd)(id, SEL, id) = 0; static id (*plAdd)(id, SEL, id) = 0;
@ -281,6 +285,8 @@ handle_printf_atsign (FILE *stream,
GSStringClass = [GSString class]; GSStringClass = [GSString class];
GSMStringClass = [GSMString class]; GSMStringClass = [GSMString class];
GSUStringClass = [GSUString class]; GSUStringClass = [GSUString class];
GSCInlineStringClass = [GSCInlineString class];
GSUInlineStringClass = [GSUInlineString class];
#if HAVE_REGISTER_PRINTF_FUNCTION #if HAVE_REGISTER_PRINTF_FUNCTION
if (register_printf_function ('@', if (register_printf_function ('@',
@ -324,21 +330,34 @@ handle_printf_atsign (FILE *stream,
+ (id) stringWithCharacters: (const unichar*)chars + (id) stringWithCharacters: (const unichar*)chars
length: (unsigned)length length: (unsigned)length
{ {
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] NSString *obj;
initWithCharacters: chars length: length]);
obj = (NSString*)NSAllocateObject(GSUInlineStringClass, length*2,
NSDefaultMallocZone());
obj = [obj initWithCharacters: chars length: length];
return AUTORELEASE(obj);
} }
+ (id) stringWithCString: (const char*) byteString + (id) stringWithCString: (const char*) byteString
{ {
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] NSString *obj;
initWithCString: byteString]); unsigned length = strlen(byteString);
obj = (NSString*)NSAllocateObject(GSCInlineStringClass, length,
NSDefaultMallocZone());
obj = [obj initWithCString: byteString length: length];
return AUTORELEASE(obj);
} }
+ (id) stringWithCString: (const char*)byteString + (id) stringWithCString: (const char*)byteString
length: (unsigned)length length: (unsigned)length
{ {
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] NSString *obj;
initWithCString: byteString length: length]);
obj = (NSString*)NSAllocateObject(GSCInlineStringClass, length,
NSDefaultMallocZone());
obj = [obj initWithCString: byteString length: length];
return AUTORELEASE(obj);
} }
+ (id) stringWithUTF8String: (const char *)bytes + (id) stringWithUTF8String: (const char *)bytes
@ -1410,7 +1429,7 @@ handle_printf_atsign (FILE *stream,
prefix_len++; prefix_len++;
} }
} }
return [NSString stringWithCharacters: u length: prefix_len]; return [NSStringClass stringWithCharacters: u length: prefix_len];
} }
else else
{ {
@ -2450,7 +2469,7 @@ handle_printf_atsign (FILE *stream,
if (lstat(&new_buf[8], &st) == 0) if (lstat(&new_buf[8], &st) == 0)
strcpy(new_buf, &new_buf[8]); strcpy(new_buf, &new_buf[8]);
} }
return [NSString stringWithCString: new_buf]; return [NSStringClass stringWithCString: new_buf];
#endif /* (__MINGW__) */ #endif /* (__MINGW__) */
} }