Re: Why does ARC retain method arguments even with full optimization?
Re: Why does ARC retain method arguments even with full optimization?
- Subject: Re: Why does ARC retain method arguments even with full optimization?
- From: Charles Srstka <email@hidden>
- Date: Fri, 04 Nov 2011 23:16:13 -0500
On Nov 4, 2011, at 10:32 PM, Preston Sumner wrote:
> Not if the functions end up sending the message through the runtime anyway, which is apparently the case._______________________________________________
Since part of the ARC spec requires objects to respond to -retain, -release, and -autorelease (http://clang.llvm.org/docs/AutomaticReferenceCounting.html#objects.retains), this is expected. However, there are still speed gains. For example, in the example posted earlier:
Foo *foo = [self foo];
[self doSomethingElse];
[foo bar];
the -foo method will in many cases return an autoreleased object. Under normal memory management, non-ARC, this would end up looking like this:
- (Foo *)foo {
return [[fooIvar retain] autorelease];
}
- (void)bar {
Foo *foo = [self foo];
[self doSomethingElse];
[foo bar];
}
Upon calling the accessor, retain and autorelease are sent to foo. Sometime later on, foo is sent a release when the autorelease pool is drained, for a total of three message sends.
The way I understand it with ARC, though, is that it can peek at the stack to see what will happen to a pointer after it’s returned, and cut out some of the unnecessary message sends, so while it’s actually generating something like this:
- (Foo *)foo {
return objc_retainAutoreleaseReturnValue(fooIvar);
}
- (void)bar {
Foo *foo = objc_retainAutoreleasedReturnValue([self foo]);
[self doSomethingElse];
[foo bar];
objc_release(foo);
}
in practice, it’s effectively doing something equivalent to this:
- (Foo *)foo {
return [fooIvar retain];
}
- (void)bar {
Foo *foo = [self foo]; // -autorelease and -retain cancel each other out here, so they’re both eliminated at runtime
[self doSomethingElse];
[foo bar];
[foo release];
}
The messages actually sent to foo are retain and release, a total of two message sends; plus, the autorelease pool doesn’t get involved. That’s a win if you ask me.
Charles_______________________________________________
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