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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to