Re: Is this the correct way to cache an IMP?
Re: Is this the correct way to cache an IMP?
- Subject: Re: Is this the correct way to cache an IMP?
- From: Ken Thomases <email@hidden>
- Date: Thu, 5 Aug 2010 08:14:29 -0500
On Aug 5, 2010, at 7:59 AM, Graham Cox wrote:
> I have a Obj-C method that is highly recursive, and needs to run at maximum performance. With this in mind, I am locally caching the method's own IMP to avoid the message dispatch for the recursive calls. It seems to work and gives a measurable benefit, but I need to be absolutely certain it's right before committing it to a release. Can anyone comment on whether it actually is right? Have I missed anything? Anything even faster?
>
>
> #define qUseImpCaching 1
>
>
> - (void) recursivelySearchWithRect:(NSRect) rect index:(NSUInteger) indx
> {
> #if qUseImpCaching
> static void(*sfunc)( id, SEL, NSRect, NSUInteger ) = nil;
>
> if ( sfunc == nil )
> sfunc = (void(*)( id, SEL, NSRect, NSUInteger ))[[self class] instanceMethodForSelector:_cmd];
> #endif
This fails in the face of subclassing/overriding. There's exactly one instance of this static variable, but there may be multiple results from [[self class] instanceMethodForSelector:_cmd], depending on what the actual class of self is.
If you don't need to support subclassing and overriding of this method, just move its body to a function, and have the method be a thin wrapper around the function.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden