Minor performance improvements.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@3209 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 1998-11-12 10:58:17 +00:00
parent 7e2d21836f
commit acaae6854e
5 changed files with 217 additions and 117 deletions

View file

@ -110,6 +110,12 @@ extern fastImp _fastImp; /* Populated by _fastBuildCache() */
extern void _fastBuildCache(); extern void _fastBuildCache();
/*
* The '_fastMallocBuffer()' function is called to get a chunk of
* memory that will automatically be released when the current
* autorelease pool goes away.
extern void *_fastMallocBuffer(unsigned size);
/* /*
* Fast access to class info - DON'T pass nil to these! * Fast access to class info - DON'T pass nil to these!
* These should really do different things conditional upon the objc * These should really do different things conditional upon the objc

View file

@ -869,13 +869,13 @@ failure:
} }
} }
- (id) copyWithZone: (NSZone*)zone - (id) copyWithZone: (NSZone*)z
{ {
if (NSShouldRetainWithZone(self, zone) && if (NSShouldRetainWithZone(self, z) &&
[self isKindOfClass: [NSMutableData class]] == NO) [self isKindOfClass: [NSMutableData class]] == NO)
return [self retain]; return [self retain];
else else
return [[dataMalloc allocWithZone: zone] return [[dataMalloc allocWithZone: z]
initWithBytes: [self bytes] length: [self length]]; initWithBytes: [self bytes] length: [self length]];
} }
@ -1402,6 +1402,28 @@ failure:
/* Creation and Destruction of objects. */ /* Creation and Destruction of objects. */
- (id) copy
{
return [self retain];
}
- (id) copyWithZone: (NSZone*)z
{
return [self retain];
}
- (id) mutableCopy
{
return [[mutableDataMalloc allocWithZone: NSDefaultMallocZone()]
initWithBytes: bytes length: length];
}
- (id) mutableCopyWithZone: (NSZone*)z
{
return [[mutableDataMalloc allocWithZone: z]
initWithBytes: bytes length: length];
}
- (void) dealloc - (void) dealloc
{ {
bytes = 0; bytes = 0;
@ -1461,12 +1483,14 @@ failure:
- (void) getBytes: (void*)buffer - (void) getBytes: (void*)buffer
range: (NSRange)aRange range: (NSRange)aRange
{ {
if (aRange.location > length || NSMaxRange(aRange) > length) { if (aRange.location > length || NSMaxRange(aRange) > length)
{
[NSException raise: NSRangeException [NSException raise: NSRangeException
format: @"Range: (%u, %u) Size: %d", format: @"Range: (%u, %u) Size: %d",
aRange.location, aRange.length, length]; aRange.location, aRange.length, length];
} }
else { else
{
memcpy(buffer, bytes + aRange.location, aRange.length); memcpy(buffer, bytes + aRange.location, aRange.length);
} }
return; return;
@ -1480,7 +1504,8 @@ failure:
static inline void static inline void
getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos) getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
{ {
if (*pos > limit || len > limit || len+*pos > limit) { if (*pos > limit || len > limit || len+*pos > limit)
{
[NSException raise: NSRangeException [NSException raise: NSRangeException
format: @"Range: (%u, %u) Size: %d", format: @"Range: (%u, %u) Size: %d",
*pos, len, limit]; *pos, len, limit];
@ -1728,9 +1753,28 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
@implementation NSDataMalloc @implementation NSDataMalloc
- (id) copy
{
if (NSShouldRetainWithZone(self, NSDefaultMallocZone()))
return [self retain];
else
return [[dataMalloc allocWithZone: NSDefaultMallocZone()]
initWithBytes: bytes length: length];
}
- (id) copyWithZone: (NSZone*)z
{
if (NSShouldRetainWithZone(self, z))
return [self retain];
else
return [[dataMalloc allocWithZone: z]
initWithBytes: bytes length: length];
}
- (void) dealloc - (void) dealloc
{ {
if (bytes) { if (bytes)
{
NSZoneFree(zone, bytes); NSZoneFree(zone, bytes);
bytes = 0; bytes = 0;
} }
@ -1741,15 +1785,18 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
{ {
void* tmp = 0; void* tmp = 0;
if (aBuffer != 0 && bufferSize > 0) { if (aBuffer != 0 && bufferSize > 0)
{
zone = [self zone]; zone = [self zone];
tmp = NSZoneMalloc(zone, bufferSize); tmp = NSZoneMalloc(zone, bufferSize);
if (tmp == 0) { if (tmp == 0)
{
NSLog(@"[NSDataMalloc -initWithBytes:length:] unable to allocate %lu bytes", bufferSize); NSLog(@"[NSDataMalloc -initWithBytes:length:] unable to allocate %lu bytes", bufferSize);
[self release]; [self release];
return nil; return nil;
} }
else { else
{
memcpy(tmp, aBuffer, bufferSize); memcpy(tmp, aBuffer, bufferSize);
} }
} }
@ -1773,7 +1820,8 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
* If the zone is zero, the data we have been given does not belong * If the zone is zero, the data we have been given does not belong
* to use so we must create an NSDataStatic object to contain it. * to use so we must create an NSDataStatic object to contain it.
*/ */
if (aZone == 0) { if (aZone == 0)
{
NSData *data; NSData *data;
data = [[NSDataStatic alloc] initWithBytesNoCopy: aBuffer data = [[NSDataStatic alloc] initWithBytesNoCopy: aBuffer
@ -1784,7 +1832,8 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
zone = aZone; zone = aZone;
bytes = aBuffer; bytes = aBuffer;
if (bytes) { if (bytes)
{
length = bufferSize; length = bufferSize;
} }
return self; return self;
@ -1798,16 +1847,19 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
zone = [self zone]; zone = [self zone];
[aCoder decodeValueOfObjCType: @encode(unsigned long) at: &l]; [aCoder decodeValueOfObjCType: @encode(unsigned long) at: &l];
if (l) { if (l)
{
b = NSZoneMalloc(zone, l); b = NSZoneMalloc(zone, l);
if (b == 0) { if (b == 0)
{
NSLog(@"[NSDataMalloc -initWithCoder:] unable to get %lu bytes", l); NSLog(@"[NSDataMalloc -initWithCoder:] unable to get %lu bytes", l);
[self release]; [self release];
return nil; return nil;
} }
[aCoder decodeArrayOfObjCType: @encode(unsigned char) count: l at: b]; [aCoder decodeArrayOfObjCType: @encode(unsigned char) count: l at: b];
} }
else { else
{
b = 0; b = 0;
} }
return [self initWithBytesNoCopy: b length: l fromZone: zone]; return [self initWithBytesNoCopy: b length: l fromZone: zone];
@ -1816,7 +1868,8 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
- (id) initWithContentsOfFile: (NSString *)path - (id) initWithContentsOfFile: (NSString *)path
{ {
zone = [self zone]; zone = [self zone];
if (readContentsOfFile(path, &bytes, &length, zone) == NO) { if (readContentsOfFile(path, &bytes, &length, zone) == NO)
{
[self release]; [self release];
self = nil; self = nil;
} }
@ -1838,10 +1891,12 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
- (id) initWithData: (NSData*)anObject - (id) initWithData: (NSData*)anObject
{ {
if (anObject == nil) { if (anObject == nil)
{
return [self initWithBytesNoCopy: 0 length: 0 fromZone: [self zone]]; return [self initWithBytesNoCopy: 0 length: 0 fromZone: [self zone]];
} }
if ([anObject isKindOfClass: [NSData class]] == NO) { if ([anObject isKindOfClass: [NSData class]] == NO)
{
NSLog(@"-initWithData: passed a non-data object"); NSLog(@"-initWithData: passed a non-data object");
[self release]; [self release];
return nil; return nil;
@ -1851,7 +1906,8 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
- (void*) relinquishAllocatedBytesFromZone: (NSZone*)aZone - (void*) relinquishAllocatedBytesFromZone: (NSZone*)aZone
{ {
if (aZone == zone || aZone == 0) { if (aZone == zone || aZone == 0)
{
void *buf = bytes; void *buf = bytes;
bytes = 0; bytes = 0;
@ -1872,7 +1928,8 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
- (void) dealloc - (void) dealloc
{ {
if (bytes) { if (bytes)
{
munmap(bytes, length); munmap(bytes, length);
bytes = 0; bytes = 0;
} }
@ -2068,6 +2125,18 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
return mutableDataMalloc; return mutableDataMalloc;
} }
- (id) copy
{
return [[dataMalloc allocWithZone: NSDefaultMallocZone()]
initWithBytes: bytes length: length];
}
- (id) copyWithZone: (NSZone*)z
{
return [[dataMalloc allocWithZone: z]
initWithBytes: bytes length: length];
}
- (id) initWithBytes: (const void*)aBuffer length: (unsigned)bufferSize - (id) initWithBytes: (const void*)aBuffer length: (unsigned)bufferSize
{ {
self = [self initWithCapacity: bufferSize]; self = [self initWithCapacity: bufferSize];

View file

@ -26,7 +26,6 @@
#include <config.h> #include <config.h>
#include <gnustep/base/preface.h> #include <gnustep/base/preface.h>
#include <Foundation/NSString.h> #include <Foundation/NSString.h>
#include <Foundation/NSData.h>
#include <Foundation/NSCoder.h> #include <Foundation/NSCoder.h>
#include <gnustep/base/NSGString.h> #include <gnustep/base/NSGString.h>
#include <gnustep/base/NSGCString.h> #include <gnustep/base/NSGCString.h>
@ -299,11 +298,10 @@ static IMP msInitImp; /* designated initialiser for mutable */
- (const char *) cString - (const char *) cString
{ {
char *r = NSZoneMalloc(NSDefaultMallocZone(), _count+1); char *r = (char*)_fastMallocBuffer(_count+1);
memcpy(r, _contents_chars, _count); memcpy(r, _contents_chars, _count);
r[_count] = '\0'; r[_count] = '\0';
[NSData dataWithBytesNoCopy:r length: _count+1];
return r; return r;
} }

View file

@ -33,7 +33,6 @@
#include <gnustep/base/preface.h> #include <gnustep/base/preface.h>
#include <Foundation/NSString.h> #include <Foundation/NSString.h>
#include <Foundation/NSGString.h> #include <Foundation/NSGString.h>
#include <Foundation/NSData.h>
#include <Foundation/NSCoder.h> #include <Foundation/NSCoder.h>
#include <gnustep/base/IndexedCollection.h> #include <gnustep/base/IndexedCollection.h>
#include <gnustep/base/IndexedCollectionPrivate.h> #include <gnustep/base/IndexedCollectionPrivate.h>
@ -224,13 +223,11 @@
- (const char *) cString - (const char *) cString
{ {
char *r; char *r = (char*)_fastMallocBuffer(_count+1);
OBJC_MALLOC(r, char, _count+1);
if (_count > 0) if (_count > 0)
ustrtostr(r,_contents_chars, _count); ustrtostr(r,_contents_chars, _count);
r[_count] = '\0'; r[_count] = '\0';
[NSData dataWithBytesNoCopy: r length: _count+1];
return r; return r;
} }

View file

@ -44,6 +44,10 @@
fastCls _fastCls; /* Structure to cache classes. */ fastCls _fastCls; /* Structure to cache classes. */
fastImp _fastImp; /* Structure to cache methods. */ fastImp _fastImp; /* Structure to cache methods. */
@class _FastMallocBuffer;
static Class fastMallocClass;
static unsigned fastMallocOffset;
@class NSDataMalloc; @class NSDataMalloc;
@class NSMutableDataMalloc; @class NSMutableDataMalloc;
@ -76,7 +80,6 @@ void _fastBuildCache()
instanceMethodForSelector: @selector(isEqual:)]; instanceMethodForSelector: @selector(isEqual:)];
} }
/* /*
* Reference count and memory management * Reference count and memory management
@ -374,6 +377,8 @@ static BOOL double_release_check_enabled = NO;
#endif #endif
autorelease_class = [NSAutoreleasePool class]; autorelease_class = [NSAutoreleasePool class];
autorelease_imp = [autorelease_class methodForSelector: autorelease_sel]; autorelease_imp = [autorelease_class methodForSelector: autorelease_sel];
fastMallocClass = [_FastMallocBuffer class];
fastMallocOffset = fastMallocClass->instance_size % ALIGN;
_fastBuildCache(); _fastBuildCache();
} }
return; return;
@ -1081,3 +1086,28 @@ static BOOL double_release_check_enabled = NO;
} }
@end @end
/*
* Stuff for temporary memory management.
*/
@interface _FastMallocBuffer : NSObject
@end
@implementation _FastMallocBuffer
@end
/*
* Function for giving us the fastest possible allocation of memory to
* be used for temporary storage.
*/
void *
_fastMallocBuffer(unsigned size)
{
_FastMallocBuffer *o;
o = (_FastMallocBuffer*)NSAllocateObject(fastMallocClass,
size + fastMallocOffset, NSDefaultMallocZone());
(*autorelease_imp)(autorelease_class, autorelease_sel, o);
return ((void*)&o[1])+fastMallocOffset;
}