mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-31 16:50:58 +00:00
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:
parent
7e2d21836f
commit
acaae6854e
5 changed files with 217 additions and 117 deletions
|
@ -110,6 +110,12 @@ extern fastImp _fastImp; /* Populated by _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!
|
||||
* These should really do different things conditional upon the objc
|
||||
|
|
109
Source/NSData.m
109
Source/NSData.m
|
@ -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)
|
||||
return [self retain];
|
||||
else
|
||||
return [[dataMalloc allocWithZone: zone]
|
||||
return [[dataMalloc allocWithZone: z]
|
||||
initWithBytes: [self bytes] length: [self length]];
|
||||
}
|
||||
|
||||
|
@ -1402,6 +1402,28 @@ failure:
|
|||
|
||||
/* 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
|
||||
{
|
||||
bytes = 0;
|
||||
|
@ -1461,12 +1483,14 @@ failure:
|
|||
- (void) getBytes: (void*)buffer
|
||||
range: (NSRange)aRange
|
||||
{
|
||||
if (aRange.location > length || NSMaxRange(aRange) > length) {
|
||||
if (aRange.location > length || NSMaxRange(aRange) > length)
|
||||
{
|
||||
[NSException raise: NSRangeException
|
||||
format: @"Range: (%u, %u) Size: %d",
|
||||
aRange.location, aRange.length, length];
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
memcpy(buffer, bytes + aRange.location, aRange.length);
|
||||
}
|
||||
return;
|
||||
|
@ -1480,7 +1504,8 @@ failure:
|
|||
static inline void
|
||||
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
|
||||
format: @"Range: (%u, %u) Size: %d",
|
||||
*pos, len, limit];
|
||||
|
@ -1728,9 +1753,28 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
|
|||
|
||||
@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
|
||||
{
|
||||
if (bytes) {
|
||||
if (bytes)
|
||||
{
|
||||
NSZoneFree(zone, bytes);
|
||||
bytes = 0;
|
||||
}
|
||||
|
@ -1741,15 +1785,18 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
|
|||
{
|
||||
void* tmp = 0;
|
||||
|
||||
if (aBuffer != 0 && bufferSize > 0) {
|
||||
if (aBuffer != 0 && bufferSize > 0)
|
||||
{
|
||||
zone = [self zone];
|
||||
tmp = NSZoneMalloc(zone, bufferSize);
|
||||
if (tmp == 0) {
|
||||
if (tmp == 0)
|
||||
{
|
||||
NSLog(@"[NSDataMalloc -initWithBytes:length:] unable to allocate %lu bytes", bufferSize);
|
||||
[self release];
|
||||
return nil;
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
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
|
||||
* to use so we must create an NSDataStatic object to contain it.
|
||||
*/
|
||||
if (aZone == 0) {
|
||||
if (aZone == 0)
|
||||
{
|
||||
NSData *data;
|
||||
|
||||
data = [[NSDataStatic alloc] initWithBytesNoCopy: aBuffer
|
||||
|
@ -1784,7 +1832,8 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
|
|||
|
||||
zone = aZone;
|
||||
bytes = aBuffer;
|
||||
if (bytes) {
|
||||
if (bytes)
|
||||
{
|
||||
length = bufferSize;
|
||||
}
|
||||
return self;
|
||||
|
@ -1798,16 +1847,19 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
|
|||
zone = [self zone];
|
||||
|
||||
[aCoder decodeValueOfObjCType: @encode(unsigned long) at: &l];
|
||||
if (l) {
|
||||
if (l)
|
||||
{
|
||||
b = NSZoneMalloc(zone, l);
|
||||
if (b == 0) {
|
||||
if (b == 0)
|
||||
{
|
||||
NSLog(@"[NSDataMalloc -initWithCoder:] unable to get %lu bytes", l);
|
||||
[self release];
|
||||
return nil;
|
||||
}
|
||||
[aCoder decodeArrayOfObjCType: @encode(unsigned char) count: l at: b];
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
b = 0;
|
||||
}
|
||||
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
|
||||
{
|
||||
zone = [self zone];
|
||||
if (readContentsOfFile(path, &bytes, &length, zone) == NO) {
|
||||
if (readContentsOfFile(path, &bytes, &length, zone) == NO)
|
||||
{
|
||||
[self release];
|
||||
self = nil;
|
||||
}
|
||||
|
@ -1838,10 +1891,12 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
|
|||
|
||||
- (id) initWithData: (NSData*)anObject
|
||||
{
|
||||
if (anObject == nil) {
|
||||
if (anObject == nil)
|
||||
{
|
||||
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");
|
||||
[self release];
|
||||
return nil;
|
||||
|
@ -1851,7 +1906,8 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
|
|||
|
||||
- (void*) relinquishAllocatedBytesFromZone: (NSZone*)aZone
|
||||
{
|
||||
if (aZone == zone || aZone == 0) {
|
||||
if (aZone == zone || aZone == 0)
|
||||
{
|
||||
void *buf = bytes;
|
||||
|
||||
bytes = 0;
|
||||
|
@ -1872,7 +1928,8 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
|
|||
|
||||
- (void) dealloc
|
||||
{
|
||||
if (bytes) {
|
||||
if (bytes)
|
||||
{
|
||||
munmap(bytes, length);
|
||||
bytes = 0;
|
||||
}
|
||||
|
@ -2068,6 +2125,18 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
|
|||
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
|
||||
{
|
||||
self = [self initWithCapacity: bufferSize];
|
||||
|
|
|
@ -26,7 +26,6 @@
|
|||
#include <config.h>
|
||||
#include <gnustep/base/preface.h>
|
||||
#include <Foundation/NSString.h>
|
||||
#include <Foundation/NSData.h>
|
||||
#include <Foundation/NSCoder.h>
|
||||
#include <gnustep/base/NSGString.h>
|
||||
#include <gnustep/base/NSGCString.h>
|
||||
|
@ -299,11 +298,10 @@ static IMP msInitImp; /* designated initialiser for mutable */
|
|||
|
||||
- (const char *) cString
|
||||
{
|
||||
char *r = NSZoneMalloc(NSDefaultMallocZone(), _count+1);
|
||||
char *r = (char*)_fastMallocBuffer(_count+1);
|
||||
|
||||
memcpy(r, _contents_chars, _count);
|
||||
r[_count] = '\0';
|
||||
[NSData dataWithBytesNoCopy:r length: _count+1];
|
||||
return r;
|
||||
}
|
||||
|
||||
|
|
|
@ -33,7 +33,6 @@
|
|||
#include <gnustep/base/preface.h>
|
||||
#include <Foundation/NSString.h>
|
||||
#include <Foundation/NSGString.h>
|
||||
#include <Foundation/NSData.h>
|
||||
#include <Foundation/NSCoder.h>
|
||||
#include <gnustep/base/IndexedCollection.h>
|
||||
#include <gnustep/base/IndexedCollectionPrivate.h>
|
||||
|
@ -224,13 +223,11 @@
|
|||
|
||||
- (const char *) cString
|
||||
{
|
||||
char *r;
|
||||
char *r = (char*)_fastMallocBuffer(_count+1);
|
||||
|
||||
OBJC_MALLOC(r, char, _count+1);
|
||||
if (_count > 0)
|
||||
ustrtostr(r,_contents_chars, _count);
|
||||
r[_count] = '\0';
|
||||
[NSData dataWithBytesNoCopy: r length: _count+1];
|
||||
return r;
|
||||
}
|
||||
|
||||
|
|
|
@ -44,6 +44,10 @@
|
|||
fastCls _fastCls; /* Structure to cache classes. */
|
||||
fastImp _fastImp; /* Structure to cache methods. */
|
||||
|
||||
@class _FastMallocBuffer;
|
||||
static Class fastMallocClass;
|
||||
static unsigned fastMallocOffset;
|
||||
|
||||
@class NSDataMalloc;
|
||||
@class NSMutableDataMalloc;
|
||||
|
||||
|
@ -76,7 +80,6 @@ void _fastBuildCache()
|
|||
instanceMethodForSelector: @selector(isEqual:)];
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Reference count and memory management
|
||||
|
@ -374,6 +377,8 @@ static BOOL double_release_check_enabled = NO;
|
|||
#endif
|
||||
autorelease_class = [NSAutoreleasePool class];
|
||||
autorelease_imp = [autorelease_class methodForSelector: autorelease_sel];
|
||||
fastMallocClass = [_FastMallocBuffer class];
|
||||
fastMallocOffset = fastMallocClass->instance_size % ALIGN;
|
||||
_fastBuildCache();
|
||||
}
|
||||
return;
|
||||
|
@ -1081,3 +1086,28 @@ static BOOL double_release_check_enabled = NO;
|
|||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue