Property-list optimisations.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@4300 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 1999-05-26 17:09:21 +00:00
parent 37116b1f02
commit f4af85f50e
4 changed files with 133 additions and 75 deletions

View file

@ -39,6 +39,8 @@
#include <base/fast.x> #include <base/fast.x>
@class NSGMutableCString;
@class NSArrayEnumerator; @class NSArrayEnumerator;
@class NSArrayEnumeratorReverse; @class NSArrayEnumeratorReverse;
@ -601,7 +603,8 @@ static Class NSMutableArray_concrete_class;
{ {
NSMutableString *result; NSMutableString *result;
result = [NSMutableString stringWithCapacity: 20*[self count]]; result = [[[NSGMutableCString alloc] initWithCapacity: 20*[self count]]
autorelease];
[self descriptionWithLocale: locale [self descriptionWithLocale: locale
indent: level indent: level
to: (id<GNUDescriptionDestination>)result]; to: (id<GNUDescriptionDestination>)result];

View file

@ -39,6 +39,8 @@
@implementation NSDictionary @implementation NSDictionary
@class NSGMutableCString;
@class NSGDictionary; @class NSGDictionary;
@class NSGMutableDictionary; @class NSGMutableDictionary;
@ -503,10 +505,11 @@ compareIt(id o1, id o2, void* context)
- (NSString*) descriptionInStringsFileFormat - (NSString*) descriptionInStringsFileFormat
{ {
NSMutableString *result = [NSMutableString stringWithCapacity: 1024]; NSMutableString *result;
NSEnumerator *enumerator; NSEnumerator *enumerator;
id key; id key;
result = [[[NSGMutableCString alloc] initWithCapacity: 1024] autorelease];
enumerator = [self keyEnumerator]; enumerator = [self keyEnumerator];
while ((key = [enumerator nextObject]) != nil) while ((key = [enumerator nextObject]) != nil)
{ {
@ -536,7 +539,8 @@ compareIt(id o1, id o2, void* context)
{ {
NSMutableString *result; NSMutableString *result;
result = [NSMutableString stringWithCapacity: 20*[self count]]; result = [[[NSGMutableCString alloc] initWithCapacity: 20*[self count]]
autorelease];
[self descriptionWithLocale: locale [self descriptionWithLocale: locale
indent: level indent: level
to: (id<GNUDescriptionDestination>)result]; to: (id<GNUDescriptionDestination>)result];

View file

@ -74,6 +74,10 @@ static SEL msInitSel = @selector(initWithCapacity:);
static IMP csInitImp; /* designated initialiser for cString */ static IMP csInitImp; /* designated initialiser for cString */
static IMP msInitImp; /* designated initialiser for mutable */ static IMP msInitImp; /* designated initialiser for mutable */
@interface NSGMutableCString (GNUDescription)
- (char*) _extendBy: (unsigned)len;
@end
@implementation NSGCString @implementation NSGCString
+ (void) initialize + (void) initialize
@ -539,6 +543,9 @@ static IMP msInitImp; /* designated initialiser for mutable */
- (void) descriptionTo: (id<GNUDescriptionDestination>)output - (void) descriptionTo: (id<GNUDescriptionDestination>)output
{ {
if (output == nil)
return;
if (_count == 0) if (_count == 0)
{ {
[output appendString: @"\"\""]; [output appendString: @"\"\""];
@ -586,11 +593,21 @@ static IMP msInitImp; /* designated initialiser for mutable */
if (needQuote || length != _count) if (needQuote || length != _count)
{ {
NSZone *z = fastZone(self); Class c = fastClass(output);
char *buf = NSZoneMalloc(z, length+3); NSZone *z = NSDefaultMallocZone();
char *ptr = buf; char *buf;
NSString *result; char *ptr;
length += 2;
if (c == _fastCls._NSGMutableCString)
{
buf = [(NSGMutableCString*)output _extendBy: length];
}
else
{
buf = NSZoneMalloc(z, length+1);
}
ptr = buf;
*ptr++ = '"'; *ptr++ = '"';
for (i = 0; i < _count; i++) for (i = 0; i < _count; i++)
{ {
@ -626,11 +643,16 @@ static IMP msInitImp; /* designated initialiser for mutable */
} }
*ptr++ = '"'; *ptr++ = '"';
*ptr = '\0'; *ptr = '\0';
result = [[_fastCls._NSGCString allocWithZone: NSDefaultMallocZone()] if (c != _fastCls._NSGMutableCString)
initWithCStringNoCopy: buf length: length+2 fromZone: z]; {
NSString *result;
result = [[_fastCls._NSGCString allocWithZone: z]
initWithCStringNoCopy: buf length: length fromZone: z];
[output appendString: result]; [output appendString: result];
[result release]; [result release];
} }
}
else else
{ {
[output appendString: self]; [output appendString: self];
@ -757,6 +779,14 @@ typedef struct {
@defs(NSGMutableCString) @defs(NSGMutableCString)
} NSGMutableCStringStruct; } NSGMutableCStringStruct;
static inline void
stringGrowBy(NSGMutableCStringStruct *self, unsigned size)
{
self->_capacity = MAX(self->_capacity*2, self->_count+size+1);
self->_contents_chars
= NSZoneRealloc(self->_zone, self->_contents_chars, self->_capacity);
}
static inline void static inline void
stringIncrementCountAndMakeHoleAt(NSGMutableCStringStruct *self, stringIncrementCountAndMakeHoleAt(NSGMutableCStringStruct *self,
int index, int size) int index, int size)
@ -939,10 +969,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
unsigned c = [aString cStringLength]; unsigned c = [aString cStringLength];
char save; char save;
if (_count + c >= _capacity) if (_count + c >= _capacity)
{ stringGrowBy((NSGMutableCStringStruct *)self, c);
_capacity = MAX(_capacity*2, _count+c+1);
_contents_chars = NSZoneRealloc(_zone, _contents_chars, _capacity);
}
stringIncrementCountAndMakeHoleAt((NSGMutableCStringStruct*)self, index, c); stringIncrementCountAndMakeHoleAt((NSGMutableCStringStruct*)self, index, c);
save = _contents_chars[index+c]; // getCString will put a nul here. save = _contents_chars[index+c]; // getCString will put a nul here.
[aString getCString: _contents_chars + index]; [aString getCString: _contents_chars + index];
@ -961,11 +988,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
unsigned l = other->_count; unsigned l = other->_count;
if (_count + l > _capacity) if (_count + l > _capacity)
{ stringGrowBy((NSGMutableCStringStruct *)self, l);
_capacity = MAX(_capacity*2, _count+l);
_contents_chars =
NSZoneRealloc(fastZone(self), _contents_chars, _capacity);
}
memcpy(_contents_chars + _count, other->_contents_chars, l); memcpy(_contents_chars + _count, other->_contents_chars, l);
_count += l; _count += l;
_hash = 0; _hash = 0;
@ -974,11 +997,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
{ {
unsigned l = [aString cStringLength]; unsigned l = [aString cStringLength];
if (_count + l >= _capacity) if (_count + l >= _capacity)
{ stringGrowBy((NSGMutableCStringStruct *)self, l);
_capacity = MAX(_capacity*2, _count+l+1);
_contents_chars =
NSZoneRealloc(fastZone(self), _contents_chars, _capacity);
}
[aString getCString: _contents_chars + _count]; [aString getCString: _contents_chars + _count];
_count += l; _count += l;
_hash = 0; _hash = 0;
@ -1061,6 +1080,16 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
stringDecrementCountAndFillHoleAt((NSGMutableCStringStruct*)self, index, 1); stringDecrementCountAndFillHoleAt((NSGMutableCStringStruct*)self, index, 1);
} }
- (char*) _extendBy: (unsigned)len
{
char *ptr;
stringGrowBy((NSGMutableCStringStruct *)self, len);
ptr = _contents_chars + _count;
_count += len;
_hash = 0;
return ptr;
}
@end @end
@implementation NXConstantString @implementation NXConstantString

View file

@ -24,7 +24,6 @@
#include <config.h> #include <config.h>
#include <base/preface.h> #include <base/preface.h>
#include <base/fast.x> #include <base/fast.x>
#include <mframe.h>
#include <Foundation/NSData.h> #include <Foundation/NSData.h>
#include <Foundation/NSDictionary.h> #include <Foundation/NSDictionary.h>
#include <Foundation/NSArray.h> #include <Foundation/NSArray.h>
@ -32,10 +31,10 @@
#include <Foundation/NSException.h> #include <Foundation/NSException.h>
#include <Foundation/NSProxy.h> #include <Foundation/NSProxy.h>
@class NSGCString; #include <base/NSGArray.h>
@class NSGString; #include <base/NSGCString.h>
@class NSGArray; #include <base/NSGString.h>
@class NSGMutableArray;
@class NSGDictionary; @class NSGDictionary;
@class NSGMutableDictionary; @class NSGMutableDictionary;
@class NSDataMalloc; @class NSDataMalloc;
@ -82,6 +81,14 @@ static char st_dict = (char)ST_DICT;
static char st_mdict = (char)ST_MDICT; static char st_mdict = (char)ST_MDICT;
static char st_data = (char)ST_DATA; static char st_data = (char)ST_DATA;
typedef struct {
@defs(NSGArray)
} NSGArrayStruct;
typedef struct {
@defs(NSGMutableArray)
} NSGMutableArrayStruct;
/* /*
@ -339,11 +346,13 @@ static BOOL shouldBeCompact = NO;
/* /*
* Variables to cache class information. * Variables to cache class information.
*/ */
static Class GArrayClass = 0; static Class IACls = 0; /* Immutable Array */
static Class GMutableArrayClass = 0; static Class MACls = 0; /* Mutable Array */
static Class GDataClass = 0; static Class DCls = 0; /* Data */
static Class GDictionaryClass = 0; static Class IDCls = 0; /* Immutable Dictionary */
static Class GMutableDictionaryClass = 0; static Class MDCls = 0; /* Mutable Dictionary */
static Class USCls = 0; /* Unicode String */
static Class CSCls = 0; /* C String */
typedef struct { typedef struct {
NSData *data; NSData *data;
@ -357,6 +366,20 @@ typedef struct {
static SEL debSel = @selector(deserializeBytes:length:atCursor:); static SEL debSel = @selector(deserializeBytes:length:atCursor:);
static SEL deiSel = @selector(deserializeIntAtCursor:); static SEL deiSel = @selector(deserializeIntAtCursor:);
static SEL csInitSel = @selector(initWithCStringNoCopy:length:fromZone:);
static SEL usInitSel = @selector(initWithCharactersNoCopy:length:fromZone:);
static SEL dInitSel = @selector(initWithBytesNoCopy:length:fromZone:);
static SEL iaInitSel = @selector(initWithObjects:count:);
static SEL maInitSel = @selector(initWithObjects:count:);
static SEL idInitSel = @selector(initWithObjects:forKeys:count:);
static SEL mdInitSel = @selector(initWithObjects:forKeys:count:);
static IMP csInitImp;
static IMP usInitImp;
static IMP dInitImp;
static IMP iaInitImp;
static IMP maInitImp;
static IMP idInitImp;
static IMP mdInitImp;
static void static void
initDeserializerInfo(_NSDeserializerInfo* info, NSData *d, unsigned *c, BOOL m) initDeserializerInfo(_NSDeserializerInfo* info, NSData *d, unsigned *c, BOOL m)
@ -400,10 +423,8 @@ deserializeFromInfo(_NSDeserializerInfo* info)
char *b = objc_malloc(size); char *b = objc_malloc(size);
(*info->debImp)(info->data, debSel, b, size, info->cursor); (*info->debImp)(info->data, debSel, b, size, info->cursor);
s = [_fastCls._NSGCString allocWithZone: NSDefaultMallocZone()]; s = (NSGCString*)NSAllocateObject(CSCls, 0, NSDefaultMallocZone());
s = [s initWithCStringNoCopy: b s = (*csInitImp)(s, csInitSel, b, size-1, NSDefaultMallocZone());
length: size-1
fromZone: NSDefaultMallocZone()];
if (info->didUnique) if (info->didUnique)
FastArrayAddItem(&info->array, (FastArrayItem)s); FastArrayAddItem(&info->array, (FastArrayItem)s);
return s; return s;
@ -415,10 +436,8 @@ deserializeFromInfo(_NSDeserializerInfo* info)
unichar *b = objc_malloc(size*2); unichar *b = objc_malloc(size*2);
(*info->debImp)(info->data, debSel, b, size*2, info->cursor); (*info->debImp)(info->data, debSel, b, size*2, info->cursor);
s = [_fastCls._NSGString allocWithZone: NSDefaultMallocZone()]; s = (NSGString*)NSAllocateObject(USCls, 0, NSDefaultMallocZone());
s = [s initWithCharactersNoCopy: b s = (*usInitImp)(s, usInitSel, b, size, NSDefaultMallocZone());
length: size
fromZone: NSDefaultMallocZone()];
if (info->didUnique) if (info->didUnique)
FastArrayAddItem(&info->array, (FastArrayItem)s); FastArrayAddItem(&info->array, (FastArrayItem)s);
return s; return s;
@ -429,7 +448,7 @@ deserializeFromInfo(_NSDeserializerInfo* info)
{ {
id objects[size]; id objects[size];
id a; id a;
int i; unsigned i;
for (i = 0; i < size; i++) for (i = 0; i < size; i++)
{ {
@ -440,24 +459,24 @@ deserializeFromInfo(_NSDeserializerInfo* info)
{ {
[objects[--i] release]; [objects[--i] release];
} }
objc_free(objects);
return nil; return nil;
} }
} }
if (code == ST_MARRAY || info->mutable) if (code == ST_MARRAY || info->mutable)
{ {
a = [GMutableArrayClass allocWithZone: NSDefaultMallocZone()]; a = NSAllocateObject(MACls, 0, NSDefaultMallocZone());
a = [a initWithObjects: objects a = (*maInitImp)(a, maInitSel, objects, size);
count: size];
} }
else else
{ {
a = [GArrayClass allocWithZone: NSDefaultMallocZone()]; a = NSAllocateObject(IACls, 0, NSDefaultMallocZone());
a = [a initWithObjects: objects a = (*iaInitImp)(a, iaInitSel, objects, size);
count: size];
} }
while (i > 0)
for (i = 0; i < size; i++)
{ {
[objects[--i] release]; [objects[i] release];
} }
return a; return a;
} }
@ -496,21 +515,17 @@ deserializeFromInfo(_NSDeserializerInfo* info)
} }
if (code == ST_MDICT || info->mutable) if (code == ST_MDICT || info->mutable)
{ {
d=[GMutableDictionaryClass allocWithZone: NSDefaultMallocZone()]; d = NSAllocateObject(MDCls, 0, NSDefaultMallocZone());
d = [d initWithObjects: objects d = (*mdInitImp)(d, mdInitSel, objects, keys, size);
forKeys: keys
count: size];
} }
else else
{ {
d = [GDictionaryClass allocWithZone: NSDefaultMallocZone()]; d = NSAllocateObject(IDCls, 0, NSDefaultMallocZone());
d = [d initWithObjects: objects d = (*idInitImp)(d, idInitSel, objects, keys, size);
forKeys: keys
count: size];
} }
while (i > 0) for (i = 0; i < size; i++)
{ {
[keys[--i] release]; [keys[i] release];
[objects[i] release]; [objects[i] release];
} }
return d; return d;
@ -522,10 +537,8 @@ deserializeFromInfo(_NSDeserializerInfo* info)
void *b = objc_malloc(size); void *b = objc_malloc(size);
(*info->debImp)(info->data, debSel, b, size, info->cursor); (*info->debImp)(info->data, debSel, b, size, info->cursor);
d = [GDataClass allocWithZone: NSDefaultMallocZone()]; d = (NSData*)NSAllocateObject(DCls, 0, NSDefaultMallocZone());
d = [d initWithBytesNoCopy: b d = (*dInitImp)(d, dInitSel, b, size, NSDefaultMallocZone());
length: size
fromZone: NSDefaultMallocZone()];
return d; return d;
} }
@ -605,11 +618,20 @@ deserializeFromInfo(_NSDeserializerInfo* info)
{ {
if (self == [NSDeserializer class]) if (self == [NSDeserializer class])
{ {
GArrayClass = [NSGArray class]; IACls = [NSGArray class];
GMutableArrayClass = [NSGMutableArray class]; MACls = [NSGMutableArray class];
GDataClass = [NSDataMalloc class]; DCls = [NSDataMalloc class];
GDictionaryClass = [NSGDictionary class]; IDCls = [NSGDictionary class];
GMutableDictionaryClass = [NSGMutableDictionary class]; MDCls = [NSGMutableDictionary class];
USCls = [NSGString class];
CSCls = [NSGCString class];
csInitImp = [CSCls instanceMethodForSelector: csInitSel];
usInitImp = [USCls instanceMethodForSelector: usInitSel];
dInitImp = [DCls instanceMethodForSelector: dInitSel];
iaInitImp = [IACls instanceMethodForSelector: iaInitSel];
maInitImp = [MACls instanceMethodForSelector: maInitSel];
idInitImp = [IDCls instanceMethodForSelector: idInitSel];
mdInitImp = [MDCls instanceMethodForSelector: mdInitSel];
} }
} }