From 8138b3d5b7cb7bcbf16646b0cae7defb2c69352b Mon Sep 17 00:00:00 2001 From: Richard Frith-MacDonald Date: Sat, 12 Jun 1999 14:37:58 +0000 Subject: [PATCH] Optimisation updates git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@4401 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 6 +- Headers/gnustep/base/NSConcreteValue.h | 16 +-- Source/NSCTemplateValue.m | 115 ++++++++--------- Source/NSConcreteValue.m | 163 +++++++++++++------------ 4 files changed, 153 insertions(+), 147 deletions(-) diff --git a/ChangeLog b/ChangeLog index fbf818ead..777cd4996 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,9 @@ -Sat Jun 12 10:25:00 1999 Richard Frith-Macdonald +Sat Jun 12 15:50:00 1999 Richard Frith-Macdonald * 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 diff --git a/Headers/gnustep/base/NSConcreteValue.h b/Headers/gnustep/base/NSConcreteValue.h index db0280c07..e39d65a6e 100644 --- a/Headers/gnustep/base/NSConcreteValue.h +++ b/Headers/gnustep/base/NSConcreteValue.h @@ -1,6 +1,6 @@ /* 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 Date: Mar 1995 @@ -29,38 +29,38 @@ @interface NSConcreteValue : NSValue { - void *data; - NSString *objctype; + void *data; + char *objctype; } @end @interface NSNonretainedObjectValue : NSValue { - id data; + id data; } @end @interface NSPointValue : NSValue { - NSPoint data; + NSPoint data; } @end @interface NSPointerValue : NSValue { - void *data; + void *data; } @end @interface NSRectValue : NSValue { - NSRect data; + NSRect data; } @end @interface NSSizeValue : NSValue { - NSSize data; + NSSize data; } @end diff --git a/Source/NSCTemplateValue.m b/Source/NSCTemplateValue.m index 96511fc24..d2272a5d9 100644 --- a/Source/NSCTemplateValue.m +++ b/Source/NSCTemplateValue.m @@ -28,6 +28,7 @@ #include #include #include +#include /* 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 */ @@ -57,67 +58,70 @@ // Allocating and Initializing -- initValue:(const void *)value - withObjCType:(const char *)type +- (id) initValue: (const void *)value + withObjCType: (const char *)type { - typedef _dt = data; - self = [super init]; - data = *(_dt *)value; - return self; + typedef _dt = data; + self = [super init]; + data = *(_dt *)value; + return self; } // Accessing Data -- (void)getValue:(void *)value +- (void) getValue: (void *)value { - if (!value) { - [NSException raise:NSInvalidArgumentException - format:@"Cannot copy value into NULL buffer"]; + if (!value) + { + [NSException raise: NSInvalidArgumentException + format: @"Cannot copy value into NULL buffer"]; /* NOT REACHED */ } - memcpy( value, &data, objc_sizeof_type([self objCType]) ); + memcpy( value, &data, objc_sizeof_type([self objCType]) ); } - (BOOL) isEqual: (id)other { - if ([other isKindOfClass: [self class]]) { - return [self isEqualToValue: other]; + if (other != nil && fastInstanceIsKindOfClass(other, fastClass(self))) + { + return [self isEqualToValue: other]; } - return NO; + return NO; } - (BOOL) isEqualToValue: (NSValue*)aValue { - typedef _dt = data; - if ([aValue isKindOfClass: [self class]]) { - _dt val = [aValue TYPE_METHOD]; + typedef _dt = data; + + if (aValue != nil && fastInstanceIsKindOfClass(aValue, fastClass(self))) + { + _dt val = [aValue TYPE_METHOD]; #if TYPE_ORDER == 0 - return [data isEqual: val]; + return [data isEqual: val]; #elif TYPE_ORDER == 1 - if (data.x == val.x && data.y == val.y) - return YES; - else - return NO; + if (data.x == val.x && data.y == val.y) + return YES; + else + return NO; #elif TYPE_ORDER == 2 - if (data == val) - return YES; - else - return NO; + if (data == val) + return YES; + else + return NO; #elif TYPE_ORDER == 3 - if (data.origin.x == val.origin.x && - data.origin.y == val.origin.y && - data.size.width == val.size.width && - data.size.height == val.size.height) - return YES; - else - return NO; + if (data.origin.x == val.origin.x && data.origin.y == val.origin.y + && data.size.width == val.size.width + && data.size.height == val.size.height) + return YES; + else + return NO; #elif TYPE_ORDER == 4 - if (data.width == val.width && data.height == val.height) - return YES; - else - return NO; + if (data.width == val.width && data.height == val.height) + return YES; + else + return NO; #endif } - return NO; + return NO; } - (unsigned) hash @@ -133,9 +137,8 @@ int i; val.d = data.x + data.y; - for (i = 0; i < sizeof(double); i++) { + for (i = 0; i < sizeof(double); i++) hash += val.c[i]; - } return hash; #elif TYPE_ORDER == 2 return (unsigned)(gsaddr)data; @@ -148,9 +151,8 @@ int i; 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]; - } return hash; #elif TYPE_ORDER == 4 union { @@ -161,22 +163,21 @@ int i; val.d = data.width + data.height; - for (i = 0; i < sizeof(double); i++) { + for (i = 0; i < sizeof(double); i++) hash += val.c[i]; - } return hash; #endif } - (const char *)objCType { - typedef _dt = data; - return @encode(_dt); + typedef _dt = data; + return @encode(_dt); } - (TYPE_NAME)TYPE_METHOD { - return data; + return data; } - (NSString *) description @@ -195,20 +196,20 @@ } // NSCoding -- (void)encodeWithCoder:(NSCoder *)coder +- (void) encodeWithCoder: (NSCoder *)coder { - const char *type; - [super encodeWithCoder:coder]; - type = [self objCType]; - [coder encodeValueOfObjCType:@encode(char *) at:&type]; - [coder encodeValueOfObjCType:type at:&data]; + const char *type; + [super encodeWithCoder: coder]; + type = [self objCType]; + [coder encodeValueOfObjCType: @encode(char *) at: &type]; + [coder encodeValueOfObjCType: type at: &data]; } -- (id)initWithCoder:(NSCoder *)coder +- (id) initWithCoder: (NSCoder *)coder { - [NSException raise:NSInconsistentArchiveException - format:@"Cannot unarchive class - Need NSValueDecoder."]; - return self; + [NSException raise: NSInconsistentArchiveException + format: @"Cannot unarchive class - Need NSValueDecoder."]; + return self; } @end diff --git a/Source/NSConcreteValue.m b/Source/NSConcreteValue.m index 8a427f9cb..a24877603 100644 --- a/Source/NSConcreteValue.m +++ b/Source/NSConcreteValue.m @@ -1,5 +1,5 @@ /* 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 Date: Mar 1995 @@ -29,14 +29,15 @@ #include #include #include +#include /* This is the real, general purpose value object. I've implemented all the methods here (like pointValue) even though most likely, other concrete subclasses were created to handle these types */ #define NS_RAISE_MALLOC \ - [NSException raise:NSMallocException \ - format:@"No memory left to allocate"] + [NSException raise: NSMallocException \ + format: @"No memory left to allocate"] #define NS_CHECK_MALLOC(ptr) \ if (!ptr) {NS_RAISE_MALLOC;} @@ -44,79 +45,86 @@ @implementation NSConcreteValue // NSCopying -- deepen +- (id) deepen { - void *old_ptr; - int size; + void *old_ptr; + char *old_typ; + int size; - size = objc_sizeof_type([objctype cString]); - old_ptr = data; - data = (void *)NSZoneMalloc([self zone], size); - NS_CHECK_MALLOC(data) - memcpy(data, old_ptr, size); + size = objc_sizeof_type(objctype); + old_ptr = data; + data = (void *)NSZoneMalloc(fastZone(self), size); + NS_CHECK_MALLOC(data) + memcpy(data, old_ptr, size); - objctype = [objctype copyWithZone:[self zone]]; - return self; + old_typ = objctype; + objctype = (char *)NSZoneMalloc(fastZone(self), strlen(old_typ)+1); + NS_CHECK_MALLOC(objctype) + strcpy(objctype, old_typ); + + return self; } // Allocating and Initializing -- initValue:(const void *)value - withObjCType:(const char *)type +- (id) initValue: (const void *)value + withObjCType: (const char *)type { - int size; - - if (!value || !type) - { - NSLog(@"Tried to create NSValue with NULL value or NULL type"); - [self release]; - return nil; - } + int size; + + if (!value || !type) + { + NSLog(@"Tried to create NSValue with NULL value or NULL type"); + [self release]; + return nil; + } - self = [super init]; + self = [super init]; - // FIXME: objc_sizeof_type will abort when it finds an invalid type, when - // we really want to just raise an exception - size = objc_sizeof_type(type); - if (size <= 0) - { - NSLog(@"Tried to create NSValue with invalid Objective-C type"); - [self release]; - return nil; - } + // FIXME: objc_sizeof_type will abort when it finds an invalid type, when + // we really want to just raise an exception + size = objc_sizeof_type(type); + if (size <= 0) + { + NSLog(@"Tried to create NSValue with invalid Objective-C type"); + [self release]; + return nil; + } - data = (void *)NSZoneMalloc([self zone], size); - NS_CHECK_MALLOC(data) - memcpy(data, value, size); + data = (void *)NSZoneMalloc(fastZone(self), size); + NS_CHECK_MALLOC(data) + memcpy(data, value, size); - objctype = [[NSString stringWithCString:type] retain]; - return self; + objctype = (char *)NSZoneMalloc(fastZone(self), strlen(type)+1); + NS_CHECK_MALLOC(objctype) + strcpy(objctype, type); + return self; } -- (void)dealloc +- (void) dealloc { if (objctype) - [objctype release]; + NSZoneFree(fastZone(self), objctype); if (data) - NSZoneFree([self zone], data); + NSZoneFree(fastZone(self), data); [super dealloc]; } // Accessing Data -- (void)getValue:(void *)value +- (void) getValue: (void *)value { - if (!value) { - [NSException raise:NSInvalidArgumentException - format:@"Cannot copy value into NULL buffer"]; - /* NOT REACHED */ + if (!value) + { + [NSException raise: NSInvalidArgumentException + 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 { - const char* type = [objctype cString]; - unsigned size = objc_sizeof_type(type); + unsigned size = objc_sizeof_type(objctype); unsigned hash = 0; while (size-- > 0) @@ -128,18 +136,15 @@ { const char* type; - if ([aValue class] != [self class]) + if (fastClass(aValue) != fastClass(self)) return NO; - type = [objctype cString]; - if (strcmp(type, [aValue objCType]) != 0) + if (strcmp(objctype, ((NSConcreteValue*)aValue)->objctype) != 0) return NO; else { - unsigned size = objc_sizeof_type(type); - char buf[size]; + unsigned size = objc_sizeof_type(objctype); - [aValue getValue: buf]; - if (memcmp(buf, data, size) != 0) + if (memcmp(((NSConcreteValue*)aValue)->data, data, size) != 0) return NO; return YES; } @@ -147,33 +152,33 @@ - (const char *)objCType { - return [objctype cString]; + return objctype; } // 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 @@ -181,28 +186,26 @@ int size; NSData *rep; - size = objc_sizeof_type([objctype cString]); + size = objc_sizeof_type(objctype); rep = [NSData dataWithBytes: data length: size]; return [NSString stringWithFormat: @"(%@) %@", objctype, [rep description]]; } // NSCoding -- (void)encodeWithCoder:(NSCoder *)coder +- (void) encodeWithCoder: (NSCoder *)coder { - const char *type; - [super encodeWithCoder:coder]; - // FIXME: Do we need to check for encoding void, void * or will - // NSCoder do this for us? - type = [objctype cString]; - [coder encodeValueOfObjCType:@encode(char *) at:&type]; - [coder encodeValueOfObjCType:type at:&data]; + [super encodeWithCoder: coder]; + // FIXME: Do we need to check for encoding void, void * or will + // NSCoder do this for us? + [coder encodeValueOfObjCType: @encode(char *) at: &objctype]; + [coder encodeValueOfObjCType: objctype at: &data]; } -- (id)initWithCoder:(NSCoder *)coder +- (id) initWithCoder: (NSCoder *)coder { - [NSException raise:NSInconsistentArchiveException - format:@"Cannot unarchive class - Need NSValueDecoder."]; - return self; + [NSException raise: NSInconsistentArchiveException + format: @"Cannot unarchive class - Need NSValueDecoder."]; + return self; } @end