• 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: Why does ARC retain method arguments even with full optimization?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: Why does ARC retain method arguments even with full optimization?
      • From: Wade Tregaskis <email@hidden>
References: 
 >Re: Why does ARC retain method arguments even with full optimization? (From: Preston Sumner <email@hidden>)

  • Prev by Date: Re: Why does ARC retain method arguments even with full optimization?
  • Next by Date: Printing image represented by multiple tiles using Cocoa
  • Previous by thread: Re: Why does ARC retain method arguments even with full optimization?
  • Next by thread: Re: Why does ARC retain method arguments even with full optimization?
  • Index(es):
    • Date
    • Thread