Use NS_RETURNS_RETAINED macro to save time #843
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
ARC has a tricky time returning stuff, since it doesn't know whether the caller was written with ARC or not. Under ARC, you don't really need the autorelease pool, but under MRR it's really useful. So when ARC goes to return something from a non-
init/copy/create/new
method, it uses two functionsobjc_retainAutoreleaseReturnValue
andobjc_retainAutoreleasedReturnValue
. The first one, on the callee side, unwinds the stack and checks if the second one is present on the caller side. If so, it skips the autorelease pool to save resources. Great trick!But we don't need to spend that effort. We can annotate methods to say "yeah despite the name, I'm going to return you a +1 straight up." Then ARC will skip those two functions and just do the right thing. So, if we have users who are writing in MRR then this is breaking API since we'll be giving them +1s, not autoreleased objects. But obviously nobody is writing client code in MRR now.
It would be very nice to make, say
-layoutSpecThatFits
use this attribute, but that would require all implementors to add theNS_RETURNS_RETAINED
macro to their implementations, since the macro HAS to be present on both the declaration and implementation sides. Maybe worth doing if we have a big release, but otherwise not worth irritating our users for.