From 379795ebe413f742c1a98282c607297e7b404ee6 Mon Sep 17 00:00:00 2001 From: richard Date: Sun, 29 Oct 2000 14:52:33 +0000 Subject: [PATCH] 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 --- ChangeLog | 8 ++++ Source/GSString.m | 93 +++++++++++++++++++++++++++++++++-------------- Source/NSString.m | 37 ++++++++++++++----- 3 files changed, 102 insertions(+), 36 deletions(-) diff --git a/ChangeLog b/ChangeLog index 769ba752c..dc611c07a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2000-10-29 Richard Frith-Macdonald + + * 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 * Headers/gnustep/base/NSBundle.h: added ivar. diff --git a/Source/GSString.m b/Source/GSString.m index 89a5fe992..88807aded 100644 --- a/Source/GSString.m +++ b/Source/GSString.m @@ -58,7 +58,16 @@ @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. */ @interface GSCSubString : GSCString @@ -76,6 +85,15 @@ } @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 * data stored in a GSUString object. @@ -160,9 +178,11 @@ static Class NSDataClass = 0; static Class NSStringClass = 0; static Class GSStringClass = 0; static Class GSCStringClass = 0; +static Class GSCInlineStringClass = 0; static Class GSCSubStringClass = 0; static Class GSUStringClass = 0; static Class GSUSubStringClass = 0; +static Class GSUInlineStringClass = 0; static Class GSMStringClass = 0; static Class NXConstantStringClass = 0; @@ -193,6 +213,8 @@ setup() GSStringClass = [GSString class]; GSCStringClass = [GSCString class]; GSUStringClass = [GSUString class]; + GSCInlineStringClass = [GSCInlineString class]; + GSUInlineStringClass = [GSUInlineString class]; GSCSubStringClass = [GSCSubString class]; GSUSubStringClass = [GSUSubString class]; GSMStringClass = [GSMString class]; @@ -1204,7 +1226,7 @@ transmute(ivars self, NSString *aString) NSZoneFree(NSZoneFromPointer(_contents.c), _contents.c); _contents.c = 0; } - [super dealloc]; + NSDeallocateObject(self); } - (id) initWithCharactersNoCopy: (unichar*)chars @@ -1300,17 +1322,10 @@ transmute(ivars self, NSString *aString) { if (NSShouldRetainWithZone(self, z) == NO) { - GSCString *obj; + NSString *obj; - obj = (GSCString*)NSCopyObject(self, 0, z); - if (_contents.c != 0) - { - unsigned char *tmp; - - tmp = NSZoneMalloc(z, _count); - memcpy(tmp, _contents.c, _count); - obj->_contents.c = tmp; - } + obj = (NSString*)NSAllocateObject(GSCInlineStringClass, _count, z); + obj = [obj initWithCString: _contents.c length: _count]; return obj; } 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 - (void) dealloc { RELEASE(_parent); - [super dealloc]; + NSDeallocateObject(self); } @end @@ -1559,17 +1589,10 @@ transmute(ivars self, NSString *aString) { if (NSShouldRetainWithZone(self, z) == NO) { - GSUString *obj; + NSString *obj; - obj = (GSUString*)NSCopyObject(self, 0, z); - if (_contents.u != 0) - { - unichar *tmp; - - tmp = NSZoneMalloc(z, _count*sizeof(unichar)); - memcpy(tmp, _contents.u, _count*sizeof(unichar)); - obj->_contents.u = tmp; - } + obj = (NSString*)NSAllocateObject(GSUInlineStringClass, _count*2, z); + obj = [obj initWithCharacters: _contents.u length: _count]; return obj; } 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 - (void) dealloc { RELEASE(_parent); - [super dealloc]; + NSDeallocateObject(self); } @end @@ -1835,12 +1873,13 @@ transmute(ivars self, NSString *aString) if (_flags.wide == 1) { - copy = [GSUStringClass allocWithZone: z]; + copy = (NSString*)NSAllocateObject(GSUInlineStringClass, + _count*sizeof(unichar), z); copy = [copy initWithCharacters: _contents.u length: _count]; } else { - copy = [GSCStringClass allocWithZone: z]; + copy = (NSString*)NSAllocateObject(GSCInlineStringClass, _count, z); copy = [copy initWithCString: _contents.c length: _count]; } return copy; @@ -1879,7 +1918,7 @@ transmute(ivars self, NSString *aString) self->_contents.c = 0; self->_zone = 0; } - [super dealloc]; + NSDeallocateObject(self); } - (void) deleteCharactersInRange: (NSRange)range diff --git a/Source/NSString.m b/Source/NSString.m index f1748d23e..eacff2b43 100644 --- a/Source/NSString.m +++ b/Source/NSString.m @@ -8,7 +8,7 @@ Date: February 1997 Optimisations by Richard Frith-Macdonald - Date: October 1998 + Date: October 1998 - 2000 This file is part of the GNUstep Base Library. @@ -75,6 +75,8 @@ @class GSString; @class GSMString; @class GSUString; +@class GSCInlineString; +@class GSUInlineString; @class NSGMutableArray; @class NSGMutableDictionary; @@ -89,6 +91,8 @@ static Class NSMutableStringClass; static Class GSStringClass; static Class GSMStringClass; static Class GSUStringClass; +static Class GSCInlineStringClass; +static Class GSUInlineStringClass; static Class plArray; static id (*plAdd)(id, SEL, id) = 0; @@ -281,6 +285,8 @@ handle_printf_atsign (FILE *stream, GSStringClass = [GSString class]; GSMStringClass = [GSMString class]; GSUStringClass = [GSUString class]; + GSCInlineStringClass = [GSCInlineString class]; + GSUInlineStringClass = [GSUInlineString class]; #if HAVE_REGISTER_PRINTF_FUNCTION if (register_printf_function ('@', @@ -324,21 +330,34 @@ handle_printf_atsign (FILE *stream, + (id) stringWithCharacters: (const unichar*)chars length: (unsigned)length { - return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] - initWithCharacters: chars length: length]); + NSString *obj; + + obj = (NSString*)NSAllocateObject(GSUInlineStringClass, length*2, + NSDefaultMallocZone()); + obj = [obj initWithCharacters: chars length: length]; + return AUTORELEASE(obj); } + (id) stringWithCString: (const char*) byteString { - return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] - initWithCString: byteString]); + NSString *obj; + unsigned length = strlen(byteString); + + obj = (NSString*)NSAllocateObject(GSCInlineStringClass, length, + NSDefaultMallocZone()); + obj = [obj initWithCString: byteString length: length]; + return AUTORELEASE(obj); } + (id) stringWithCString: (const char*)byteString length: (unsigned)length { - return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] - initWithCString: byteString length: length]); + NSString *obj; + + obj = (NSString*)NSAllocateObject(GSCInlineStringClass, length, + NSDefaultMallocZone()); + obj = [obj initWithCString: byteString length: length]; + return AUTORELEASE(obj); } + (id) stringWithUTF8String: (const char *)bytes @@ -1410,7 +1429,7 @@ handle_printf_atsign (FILE *stream, prefix_len++; } } - return [NSString stringWithCharacters: u length: prefix_len]; + return [NSStringClass stringWithCharacters: u length: prefix_len]; } else { @@ -2450,7 +2469,7 @@ handle_printf_atsign (FILE *stream, if (lstat(&new_buf[8], &st) == 0) strcpy(new_buf, &new_buf[8]); } - return [NSString stringWithCString: new_buf]; + return [NSStringClass stringWithCString: new_buf]; #endif /* (__MINGW__) */ }