Friday, January 31, 2014

ARC’s Fast Autorelease

Wolf Rentzsch:

ARC has a trick that keep returned objects out of autorelease pools if both the caller and callee are ARC.

This has been well advertised, but he links to the relevant runtime source code:

To summarize, callerAcceptsFastAutorelease() inspects the caller’s instructions and uses it to determine at runtime whether it needs to actually put the returned object in the autorelease pool or if it’s on the same ARC-team and it can be skipped (speeding things up).

The related Twitter conversation also revealed some interesting details. For example, the autorelease pool optimization initially did not work for +[NSDate date]. Its implementation calls gettimeofday(), and something about this confused Clang’s optimizer so that it did not generate a tail call, without which the ARC optimization can’t work.

Comments RSS · Twitter

Leave a Comment