Re: + initialize docs conflict?
Re: + initialize docs conflict?
- Subject: Re: + initialize docs conflict?
- From: Shaun Wexler <email@hidden>
- Date: Wed, 2 Feb 2005 07:13:16 -0800
On Feb 1, 2005, at 11:43 AM, Ricky Sharp wrote:
I've seen (and have been using) this pattern in NSControl subclasses
that need to set their associate cell. For example,
+ (void)initialize
{
if (self == [IIButton class])
{
[self setCellClass:[IIButtonCell class]];
}
}
This may belong on the perfoptimization-dev list, but I just wanted to
point out some optimizations that should become normal practice. If
you need to use a variable retrieved by a message more than once in a
method, you should cache it locally:
+ (void)initialize
{
Class class = [IIButton class];
if (self == class) {
[self setCellClass:class];
... // use class elsewhere
}
}
That illustrates another problem. The above code could generate a LSU
reject unless class is declared register (although -O2/O3 will do this
automatically):
+ (void)initialize
{
Class class;
if (self == (class = [IIButton class])) {
[self setCellClass:class];
... // use class elsewhere
}
}
If this is a one-shot, you can eliminate specifying the local variable
(although it is the same as the above method within the conditional):
+ (void)initialize
{
if (self == [IIButton class]) {
[self setCellClass:self];
}
}
And in a thread-safe manner:
+ (void)initialize
{
Class class;
@synchronized((class = [IIButton class])) {
if (self == class) {
[self setCellClass:class];
} else {
// etc...
}
}
}
--
Shaun Wexler
MacFOH
http://www.macfoh.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden