• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: + initialize docs conflict?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: + initialize docs conflict?
      • From: j o a r <email@hidden>
    • Re: + initialize docs conflict?
      • From: Ricky Sharp <email@hidden>
References: 
 >Re: + initialize docs conflict? (From: "Buck, Erik (Space Technology)" <email@hidden>)
 >Re: + initialize docs conflict? (From: mmalcolm crawford <email@hidden>)
 >Re: + initialize docs conflict? (From: Ricky Sharp <email@hidden>)

  • Prev by Date: problem in displaying the image with embedded cmyk profile
  • Next by Date: Re: + initialize docs conflict?
  • Previous by thread: Re: + initialize docs conflict?
  • Next by thread: Re: + initialize docs conflict?
  • Index(es):
    • Date
    • Thread