discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSAttributeString source code....


From: Richard Frith-Macdonald
Subject: Re: NSAttributeString source code....
Date: Tue, 6 Jun 2006 16:26:28 +0100


On 2 Jun 2006, at 10:24, Lloyd Dupont wrote:

In NSAttributedString I found the following code (snippets)
======= NSAttributedString.m (snippets) ==============

static SEL getSel;

@implementation NSAttributedString

+ (void) initialize
{
 if (self == [NSAttributedString class])
   {
     getSel = @selector(attributesAtIndex:effectiveRange:);
   }
}

- (NSDictionary*) attributesAtIndex: (unsigned int)index
      longestEffectiveRange: (NSRange*)aRange
      inRange: (NSRange)rangeLimit
{
 NSDictionary *attrDictionary;
 IMP  getImp;

 getImp = [self methodForSelector: getSel];
 attrDictionary = (*getImp)(self, getSel, index, aRange);
 return attrDictionary;
}
@end

===========================================

Now I wonder.....
Why in Hell write that?

I mean what's wrong with:
attrDictionary = [self attributesAtIndex: index effectiveRange: aRange]
Why replace it by:
 static SEL getSel;
 getSel = @selector(attributesAtIndex:effectiveRange:);
 getImp = [self methodForSelector: getSel];
 attrDictionary = (*getImp)(self, getSel, index, aRange);


Yes, why????
why GNUstep code work so hard at being so obscure???

Well if the code was as listed above it would be really stupid ... but of course it's not.

In fact the call to (*getImp)(self, getSel, index, aRange) is done inside a loop and might get called a lot of times. Where the attributed string is actually a large document that can make a significant difference to performance by removing method lookup overheads.

Most (maybe all) cases where the code caches method implementations are for situations where it's been tuned for performance because the method call overheads were becoming an issue. By the nature of things this needs to be done mostly in heavily used parts of the base library ... and the fact that it's done there does *NOT* in any way imply that it's sensible to do that sort of thing routinely in applications (or other libraries). That is to say, the base library source is a good example of how to write a base/foundation library, not a good example of how to write general code. I almost never cache method implementations in my appllication code.






reply via email to

[Prev in Thread] Current Thread [Next in Thread]