| Sign In/My Account | View Cart |
| Article: |
Knowing When to Let Go: Better Living Through Memory Management | |
| Subject: | is simpler slower? | |
| Date: | 2003-06-12 21:54:15 | |
| From: | anonymous2 | |
|
The following algorithm is used in the The Objective-C Programming Language document from the Apple developer's site. Although the code is a little simpler then creating a temporary variable for nothing more than a placeholder for an object you're going to release, I'm not sure that it gives you a net gain in efficiency. Does the extra overhead from the "autorease" outway the penalty involved in creating an extra variable?
|
||
Showing messages 1 through 6 of 6.
- one 4-byte word for the pointer (id old)
- two messages, both of which will have been optimized by Apple considering how commonly they're called
- one assignment.
In the example you show here, you have the same except for the pointer to the object, but the difference in message (autorelease as opposed to release) will make a much larger difference.
For one thing, in order to autorelease the object, the autorelease pool has to know it exists--that is, it has to use a pointer to it of its own. And as I discussed in the article, autoreleasing is a slow operation.
Therefore, I would recommend against autoreleasing in this context unless you have some very specific reason to.
As another example of an accessor, how about:
-(void)setFriend:(id)aFriend
{
[friend release];
friend = [aFriend retain];
}
Note that none of these examples make any attempt at addressing concurrency.
I recommend reading http://www.cocoadev.com/index.pl?AccessorMethods for more insight into Cocoa accessor methods, including thread-safe accessors.