Re: Accessors Question
Re: Accessors Question
- Subject: Re: Accessors Question
- From: Tito Ciuro <email@hidden>
- Date: Thu, 12 Jun 2008 12:02:08 -0700
Hello,
Although I prefer the safer accessors, there has been one case where I
had no choice but to return the main object pointer:
- (NSString *)foo
{
return foo;
}
In my app I had a method that was populating a custom cell with a few
elements. Depending on the data source, I had to construct the strings
in different ways. Since this string had to be constructed with up to
5 elements, when the table view had many rows, performance suffered
greatly during sort and memory consumption spiked tremendously due to
a barrage of newly allocated objects.
I ended up writing something like:
// Private version used only for performance
- (NSString *)_foo
{
return foo;
}
// Safer version used in all other cases
- (NSString *)foo
{
return [[foo retain] autorelease];
}
By using '_foo' during sorting I increased speed noticeably, while
reducing the memory footprint.
-- Tito
On 12 Jun 2008, at 11:27 AM, Jens Alfke wrote:
On 12 Jun '08, at 10:41 AM, Andy Lee wrote:
I hadn't thought of this case.
Thanks for the pointer to the docs -- I now see there is a
vulnerability in the accessors I've been writing.
This is kind of a religious issue. Some people like the safer
accessors. Some people see them as a very expensive* workaround for
a problem that rarely occurs.
I'm firmly in the latter camp. I've never used this 'safe' form of
accessor, and have only rarely run into the kind of crash it
prevents; and it was always pretty easy to track down and fix. (The
fix is just for the caller to retain the value it got from the
accessor, then release it when it's done using it.)
Another alternative is to leave the getters simple, but change the
_setter_ methods to autorelease the old value instead of releasing
it; that prevents this same crash, but is less expensive because
setters are much more rarely called than getters (and -autorelease
isn't much more expensive than -release.)
—Jens
* A basic accessor requires one or two machine instructions to do
the actual work; whereas -retain and -autorelease involve extra
method dispatches that each acquire a global lock and do a hashtable
lookup. Obviously any one call isn't going to take a noticeable
amount of time, but accessor calls are so damn ubiquitous that this
can have an overall impact on app performance in some cases. Not to
mention memory usage, since autoreleased objects have a longer
lifespan and can build up during
loops._______________________________________________
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
_______________________________________________
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