Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use NS_RETURNS_RETAINED macro to save time #843

Merged
merged 3 commits into from
Mar 21, 2018
Merged

Conversation

Adlai-Holler
Copy link
Member

@Adlai-Holler Adlai-Holler commented Mar 18, 2018

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 functions objc_retainAutoreleaseReturnValue and objc_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 the NS_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.

@TextureGroup TextureGroup deleted a comment Mar 18, 2018
Copy link
Member

@nguyenhuy nguyenhuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat trick!

@Adlai-Holler Adlai-Holler merged commit 2b12654 into master Mar 21, 2018
@Adlai-Holler Adlai-Holler deleted the AHReturnsRetained branch March 21, 2018 15:13
bernieperez pushed a commit to AtomTickets/Texture that referenced this pull request Apr 25, 2018
* Use NS_RETURNS_RETAINED macro to save time

* Update changelog and do the thing with the license headers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants