Relax the rules around objc_alloc and objc_alloc_init optimizations. #580
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.
Today the optimization is limited to:
[ClassName alloc]
[self alloc]
when within a class methodHowever it means that when code is written this way:
... then the optimization doesn't kick in and
+[NSObject alloc]
endsup in IMP caches where it could have been avoided. It turns out that
+alloc
->+[NSObject alloc]
is the most cached SEL/IMP pair in theentire platform which is rather silly).
There's two theoretical risks allowing this optimization:
if the receiver is nil (which it can't be today), but it turns out
that
objc_alloc()
/objc_alloc_init()
cope with a nil receiver,if the
Clas
type for the receiver is a lie. However, for such acode to work today (and not fail witn an unrecognized selector
anyway) you'd have to have implemented the
-alloc
instancemethod.
Fortunately,
objc_alloc()
doesn't assume that the receiver is aClass, it basically starts with a test that is similar to
This bit is only set on metaclasses by the runtime, so if an instance
is passed to this function by accident, its isa will fail this test,
and
objc_alloc()
will gracefully fallback toobjc_msgSend()
.The one thing
objc_alloc()
doesn't support is tagged pointerinstances. None of the tagged pointer classes implement an instance
method called
'alloc'
(actually there's a single class in theentire Apple codebase that has such a method).
Differential Revision: https://reviews.llvm.org/D71682
Radar-Id: rdar://problem/58058316
Reviewed-By: Akira Hatanaka
Signed-off-by: Pierre Habouzit [email protected]