Optimisation updates

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@4401 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 1999-06-12 14:37:58 +00:00
parent f001c36343
commit 2e5303ee74
4 changed files with 153 additions and 147 deletions

View file

@ -1,7 +1,9 @@
Sat Jun 12 10:25:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk> Sat Jun 12 15:50:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk>
* Source/NSConcreteValue.m: Implemented [hash and isEqualToValue * Source/NSConcreteValue.m: Implemented [hash and isEqualToValue
* Source/NSCTemplateValue.m: Implemented hash * Source/include/NSConcreteValue.h: Change NSString ivar to char* for
performance reasons.
* Source/NSCTemplateValue.m: Implemented hash and optimised.
Thu Jun 4 13:55:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk> Thu Jun 4 13:55:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk>

View file

@ -1,6 +1,6 @@
/* NSConcreteValue - Interface for Concrete NSValue classes /* NSConcreteValue - Interface for Concrete NSValue classes
Copyright (C) 1993,1994 Free Software Foundation, Inc. Copyright (C) 1993,1994,1995,1999 Free Software Foundation, Inc.
Written by: Adam Fedor <fedor@boulder.colorado.edu> Written by: Adam Fedor <fedor@boulder.colorado.edu>
Date: Mar 1995 Date: Mar 1995
@ -29,38 +29,38 @@
@interface NSConcreteValue : NSValue @interface NSConcreteValue : NSValue
{ {
void *data; void *data;
NSString *objctype; char *objctype;
} }
@end @end
@interface NSNonretainedObjectValue : NSValue @interface NSNonretainedObjectValue : NSValue
{ {
id data; id data;
} }
@end @end
@interface NSPointValue : NSValue @interface NSPointValue : NSValue
{ {
NSPoint data; NSPoint data;
} }
@end @end
@interface NSPointerValue : NSValue @interface NSPointerValue : NSValue
{ {
void *data; void *data;
} }
@end @end
@interface NSRectValue : NSValue @interface NSRectValue : NSValue
{ {
NSRect data; NSRect data;
} }
@end @end
@interface NSSizeValue : NSValue @interface NSSizeValue : NSValue
{ {
NSSize data; NSSize data;
} }
@end @end

View file

@ -28,6 +28,7 @@
#include <Foundation/NSException.h> #include <Foundation/NSException.h>
#include <Foundation/NSCoder.h> #include <Foundation/NSCoder.h>
#include <base/preface.h> #include <base/preface.h>
#include <base/fast.x>
/* This file should be run through a preprocessor with the macro TYPE_ORDER /* This file should be run through a preprocessor with the macro TYPE_ORDER
defined to a number from 0 to 4 cooresponding to each value type */ defined to a number from 0 to 4 cooresponding to each value type */
@ -57,67 +58,70 @@
// Allocating and Initializing // Allocating and Initializing
- initValue:(const void *)value - (id) initValue: (const void *)value
withObjCType:(const char *)type withObjCType: (const char *)type
{ {
typedef _dt = data; typedef _dt = data;
self = [super init]; self = [super init];
data = *(_dt *)value; data = *(_dt *)value;
return self; return self;
} }
// Accessing Data // Accessing Data
- (void)getValue:(void *)value - (void) getValue: (void *)value
{ {
if (!value) { if (!value)
[NSException raise:NSInvalidArgumentException {
format:@"Cannot copy value into NULL buffer"]; [NSException raise: NSInvalidArgumentException
format: @"Cannot copy value into NULL buffer"];
/* NOT REACHED */ /* NOT REACHED */
} }
memcpy( value, &data, objc_sizeof_type([self objCType]) ); memcpy( value, &data, objc_sizeof_type([self objCType]) );
} }
- (BOOL) isEqual: (id)other - (BOOL) isEqual: (id)other
{ {
if ([other isKindOfClass: [self class]]) { if (other != nil && fastInstanceIsKindOfClass(other, fastClass(self)))
return [self isEqualToValue: other]; {
return [self isEqualToValue: other];
} }
return NO; return NO;
} }
- (BOOL) isEqualToValue: (NSValue*)aValue - (BOOL) isEqualToValue: (NSValue*)aValue
{ {
typedef _dt = data; typedef _dt = data;
if ([aValue isKindOfClass: [self class]]) {
_dt val = [aValue TYPE_METHOD]; if (aValue != nil && fastInstanceIsKindOfClass(aValue, fastClass(self)))
{
_dt val = [aValue TYPE_METHOD];
#if TYPE_ORDER == 0 #if TYPE_ORDER == 0
return [data isEqual: val]; return [data isEqual: val];
#elif TYPE_ORDER == 1 #elif TYPE_ORDER == 1
if (data.x == val.x && data.y == val.y) if (data.x == val.x && data.y == val.y)
return YES; return YES;
else else
return NO; return NO;
#elif TYPE_ORDER == 2 #elif TYPE_ORDER == 2
if (data == val) if (data == val)
return YES; return YES;
else else
return NO; return NO;
#elif TYPE_ORDER == 3 #elif TYPE_ORDER == 3
if (data.origin.x == val.origin.x && if (data.origin.x == val.origin.x && data.origin.y == val.origin.y
data.origin.y == val.origin.y && && data.size.width == val.size.width
data.size.width == val.size.width && && data.size.height == val.size.height)
data.size.height == val.size.height) return YES;
return YES; else
else return NO;
return NO;
#elif TYPE_ORDER == 4 #elif TYPE_ORDER == 4
if (data.width == val.width && data.height == val.height) if (data.width == val.width && data.height == val.height)
return YES; return YES;
else else
return NO; return NO;
#endif #endif
} }
return NO; return NO;
} }
- (unsigned) hash - (unsigned) hash
@ -133,9 +137,8 @@
int i; int i;
val.d = data.x + data.y; val.d = data.x + data.y;
for (i = 0; i < sizeof(double); i++) { for (i = 0; i < sizeof(double); i++)
hash += val.c[i]; hash += val.c[i];
}
return hash; return hash;
#elif TYPE_ORDER == 2 #elif TYPE_ORDER == 2
return (unsigned)(gsaddr)data; return (unsigned)(gsaddr)data;
@ -148,9 +151,8 @@
int i; int i;
val.d = data.origin.x + data.origin.y + data.size.width + data.size.height; val.d = data.origin.x + data.origin.y + data.size.width + data.size.height;
for (i = 0; i < sizeof(double); i++) { for (i = 0; i < sizeof(double); i++)
hash += val.c[i]; hash += val.c[i];
}
return hash; return hash;
#elif TYPE_ORDER == 4 #elif TYPE_ORDER == 4
union { union {
@ -161,22 +163,21 @@
int i; int i;
val.d = data.width + data.height; val.d = data.width + data.height;
for (i = 0; i < sizeof(double); i++) { for (i = 0; i < sizeof(double); i++)
hash += val.c[i]; hash += val.c[i];
}
return hash; return hash;
#endif #endif
} }
- (const char *)objCType - (const char *)objCType
{ {
typedef _dt = data; typedef _dt = data;
return @encode(_dt); return @encode(_dt);
} }
- (TYPE_NAME)TYPE_METHOD - (TYPE_NAME)TYPE_METHOD
{ {
return data; return data;
} }
- (NSString *) description - (NSString *) description
@ -195,20 +196,20 @@
} }
// NSCoding // NSCoding
- (void)encodeWithCoder:(NSCoder *)coder - (void) encodeWithCoder: (NSCoder *)coder
{ {
const char *type; const char *type;
[super encodeWithCoder:coder]; [super encodeWithCoder: coder];
type = [self objCType]; type = [self objCType];
[coder encodeValueOfObjCType:@encode(char *) at:&type]; [coder encodeValueOfObjCType: @encode(char *) at: &type];
[coder encodeValueOfObjCType:type at:&data]; [coder encodeValueOfObjCType: type at: &data];
} }
- (id)initWithCoder:(NSCoder *)coder - (id) initWithCoder: (NSCoder *)coder
{ {
[NSException raise:NSInconsistentArchiveException [NSException raise: NSInconsistentArchiveException
format:@"Cannot unarchive class - Need NSValueDecoder."]; format: @"Cannot unarchive class - Need NSValueDecoder."];
return self; return self;
} }
@end @end

View file

@ -1,5 +1,5 @@
/* NSConcreteValue - Object encapsulation for C types. /* NSConcreteValue - Object encapsulation for C types.
Copyright (C) 1993,1994 Free Software Foundation, Inc. Copyright (C) 1993,1994,1995,1999 Free Software Foundation, Inc.
Written by: Adam Fedor <fedor@boulder.colorado.edu> Written by: Adam Fedor <fedor@boulder.colorado.edu>
Date: Mar 1995 Date: Mar 1995
@ -29,14 +29,15 @@
#include <Foundation/NSCoder.h> #include <Foundation/NSCoder.h>
#include <Foundation/NSZone.h> #include <Foundation/NSZone.h>
#include <base/preface.h> #include <base/preface.h>
#include <base/fast.x>
/* This is the real, general purpose value object. I've implemented all the /* This is the real, general purpose value object. I've implemented all the
methods here (like pointValue) even though most likely, other concrete methods here (like pointValue) even though most likely, other concrete
subclasses were created to handle these types */ subclasses were created to handle these types */
#define NS_RAISE_MALLOC \ #define NS_RAISE_MALLOC \
[NSException raise:NSMallocException \ [NSException raise: NSMallocException \
format:@"No memory left to allocate"] format: @"No memory left to allocate"]
#define NS_CHECK_MALLOC(ptr) \ #define NS_CHECK_MALLOC(ptr) \
if (!ptr) {NS_RAISE_MALLOC;} if (!ptr) {NS_RAISE_MALLOC;}
@ -44,79 +45,86 @@
@implementation NSConcreteValue @implementation NSConcreteValue
// NSCopying // NSCopying
- deepen - (id) deepen
{ {
void *old_ptr; void *old_ptr;
int size; char *old_typ;
int size;
size = objc_sizeof_type([objctype cString]); size = objc_sizeof_type(objctype);
old_ptr = data; old_ptr = data;
data = (void *)NSZoneMalloc([self zone], size); data = (void *)NSZoneMalloc(fastZone(self), size);
NS_CHECK_MALLOC(data) NS_CHECK_MALLOC(data)
memcpy(data, old_ptr, size); memcpy(data, old_ptr, size);
objctype = [objctype copyWithZone:[self zone]]; old_typ = objctype;
return self; objctype = (char *)NSZoneMalloc(fastZone(self), strlen(old_typ)+1);
NS_CHECK_MALLOC(objctype)
strcpy(objctype, old_typ);
return self;
} }
// Allocating and Initializing // Allocating and Initializing
- initValue:(const void *)value - (id) initValue: (const void *)value
withObjCType:(const char *)type withObjCType: (const char *)type
{ {
int size; int size;
if (!value || !type) if (!value || !type)
{ {
NSLog(@"Tried to create NSValue with NULL value or NULL type"); NSLog(@"Tried to create NSValue with NULL value or NULL type");
[self release]; [self release];
return nil; return nil;
} }
self = [super init]; self = [super init];
// FIXME: objc_sizeof_type will abort when it finds an invalid type, when // FIXME: objc_sizeof_type will abort when it finds an invalid type, when
// we really want to just raise an exception // we really want to just raise an exception
size = objc_sizeof_type(type); size = objc_sizeof_type(type);
if (size <= 0) if (size <= 0)
{ {
NSLog(@"Tried to create NSValue with invalid Objective-C type"); NSLog(@"Tried to create NSValue with invalid Objective-C type");
[self release]; [self release];
return nil; return nil;
} }
data = (void *)NSZoneMalloc([self zone], size); data = (void *)NSZoneMalloc(fastZone(self), size);
NS_CHECK_MALLOC(data) NS_CHECK_MALLOC(data)
memcpy(data, value, size); memcpy(data, value, size);
objctype = [[NSString stringWithCString:type] retain]; objctype = (char *)NSZoneMalloc(fastZone(self), strlen(type)+1);
return self; NS_CHECK_MALLOC(objctype)
strcpy(objctype, type);
return self;
} }
- (void)dealloc - (void) dealloc
{ {
if (objctype) if (objctype)
[objctype release]; NSZoneFree(fastZone(self), objctype);
if (data) if (data)
NSZoneFree([self zone], data); NSZoneFree(fastZone(self), data);
[super dealloc]; [super dealloc];
} }
// Accessing Data // Accessing Data
- (void)getValue:(void *)value - (void) getValue: (void *)value
{ {
if (!value) { if (!value)
[NSException raise:NSInvalidArgumentException {
format:@"Cannot copy value into NULL buffer"]; [NSException raise: NSInvalidArgumentException
/* NOT REACHED */ format: @"Cannot copy value into NULL buffer"];
/* NOT REACHED */
} }
memcpy( value, data, objc_sizeof_type([objctype cString]) ); memcpy(value, data, objc_sizeof_type(objctype));
} }
- (unsigned) hash - (unsigned) hash
{ {
const char* type = [objctype cString]; unsigned size = objc_sizeof_type(objctype);
unsigned size = objc_sizeof_type(type);
unsigned hash = 0; unsigned hash = 0;
while (size-- > 0) while (size-- > 0)
@ -128,18 +136,15 @@
{ {
const char* type; const char* type;
if ([aValue class] != [self class]) if (fastClass(aValue) != fastClass(self))
return NO; return NO;
type = [objctype cString]; if (strcmp(objctype, ((NSConcreteValue*)aValue)->objctype) != 0)
if (strcmp(type, [aValue objCType]) != 0)
return NO; return NO;
else else
{ {
unsigned size = objc_sizeof_type(type); unsigned size = objc_sizeof_type(objctype);
char buf[size];
[aValue getValue: buf]; if (memcmp(((NSConcreteValue*)aValue)->data, data, size) != 0)
if (memcmp(buf, data, size) != 0)
return NO; return NO;
return YES; return YES;
} }
@ -147,33 +152,33 @@
- (const char *)objCType - (const char *)objCType
{ {
return [objctype cString]; return objctype;
} }
// FIXME: need to check to make sure these hold the right values... // FIXME: need to check to make sure these hold the right values...
- (id)nonretainedObjectValue - (id) nonretainedObjectValue
{ {
return *((id *)data); return *((id *)data);
} }
- (void *)pointerValue - (void *) pointerValue
{ {
return *((void **)data); return *((void **)data);
} }
- (NSRect)rectValue - (NSRect) rectValue
{ {
return *((NSRect *)data); return *((NSRect *)data);
} }
- (NSSize)sizeValue - (NSSize) sizeValue
{ {
return *((NSSize *)data); return *((NSSize *)data);
} }
- (NSPoint)pointValue - (NSPoint) pointValue
{ {
return *((NSPoint *)data); return *((NSPoint *)data);
} }
- (NSString *) description - (NSString *) description
@ -181,28 +186,26 @@
int size; int size;
NSData *rep; NSData *rep;
size = objc_sizeof_type([objctype cString]); size = objc_sizeof_type(objctype);
rep = [NSData dataWithBytes: data length: size]; rep = [NSData dataWithBytes: data length: size];
return [NSString stringWithFormat: @"(%@) %@", objctype, [rep description]]; return [NSString stringWithFormat: @"(%@) %@", objctype, [rep description]];
} }
// NSCoding // NSCoding
- (void)encodeWithCoder:(NSCoder *)coder - (void) encodeWithCoder: (NSCoder *)coder
{ {
const char *type; [super encodeWithCoder: coder];
[super encodeWithCoder:coder]; // FIXME: Do we need to check for encoding void, void * or will
// FIXME: Do we need to check for encoding void, void * or will // NSCoder do this for us?
// NSCoder do this for us? [coder encodeValueOfObjCType: @encode(char *) at: &objctype];
type = [objctype cString]; [coder encodeValueOfObjCType: objctype at: &data];
[coder encodeValueOfObjCType:@encode(char *) at:&type];
[coder encodeValueOfObjCType:type at:&data];
} }
- (id)initWithCoder:(NSCoder *)coder - (id) initWithCoder: (NSCoder *)coder
{ {
[NSException raise:NSInconsistentArchiveException [NSException raise: NSInconsistentArchiveException
format:@"Cannot unarchive class - Need NSValueDecoder."]; format: @"Cannot unarchive class - Need NSValueDecoder."];
return self; return self;
} }
@end @end