[cfe-dev] [Proposal]: Devirtualizing local methods (messages) in final class in Objective-C

Aditya K via cfe-dev cfe-dev at lists.llvm.org
Tue Sep 11 21:09:10 PDT 2018


> Objective-C allows method implementations to be replaced  dynamically.  It is not obvious that `objc_subclassing_restricted` in  any > way prevents either this or similar techniques where a dynamic  subclass is formed in order to change the implementation of a 
> method  on a particular instance.

Can we revise the semantics of `objc_subclassing_restricted` to disallow subclassing dynamically?

> Objective-C also allows the class of a particular instance to be  changed, although I think it would be fair to restrict this to ensure  
> that the new class is related in some reasonable way to the old class —  probably that it must have the old class (ignoring  dynamic > subclasses) as a superclass.

Yes, I guess while utilizing some of the super-dynamic behavior of Objective-C, it may be tricky to take advantage of devirutalization. We may need to have proper documentation etc. to make the programmers aware of the expected behavior as a result of this optimization.

> There are some other concerns that apply to devirtualization in general, but not to your restricted case:
>   - The receiver of a message send is generally allowed to be `nil`.
That was the reason why I restricted the optimization to work only for messages passed to self because, IIUC, self cannot be nil.

>   - Objective-C is not strongly typed.  Instance-variable accesses  have C-like restrictions, but otherwise static type information is  
> defined to only be meaningful for determining the type signature for  message sends, not for determining the actual dynamic  type. > For example, several major libraries rely on the ability to  create "proxy" objects that transparently forward most messages to  
> another object.

> Ultimately, while I think devirtualization would be very powerful  in Objective-C, I think it really needs to be opted-in in some more  
> explicit way.

We can have a compiler flag to enable Objective-C devirtualization. Additionally we can introduce an __attribute__((objc_local_method)) to enable finer grained control of devirtualization.

Thanks for the review, I can start putting the patches for review if you think the overall approach seems reasonable.
-Aditya



More information about the cfe-dev mailing list