[cfe-users] Objective-C message-to-nil optimization?

Jordan Rose jordan_rose at apple.com
Wed Feb 12 09:23:09 PST 2014


Hi, Kyle. It looks like Clang does not perform this optimization, even under -O3 or -Oz. I can guess why: even though sending a message to nil will result in 0, it's still more work to call objc_msgSend, have it do the comparison to nil, and then have it clean things up properly to return 0. (In fact, you can poke at the source for objc_msgSend and see exactly how much work it is: not much, but still more than doing the check yourself.)

I could see this being a valid optimization for -Os or -Oz mode, though, where the stated goal of optimization is to make the program fast and minimize code size. If you agree, you can file a bug at http://llvm.org/bugs/, or against Apple at http://bugreport.apple.com.

Jordan


On Feb 11, 2014, at 19:24 , Kyle Sluder <kyle at ksluder.com> wrote:

> Just curious if clang optimizes ObjC messages that it knows have a nil
> receiver?
> 
> I'm converting a subclass of NSTextStorage to lazily create its backing
> store. To make this very clear to readers, I want to wrap all accesses
> to the backing store in conditional checks.
> 
> For example:
> 
> - (NSUInteger)length;
> {
>  return _contents ? _contents.length : 0;
> }
> 
> Strictly speaking, this is unnecessary, because sending -length to nil
> will result in 0 anyway.
> 
> Part of the reason I'm making this change is for optimization, so I'd
> feel a lot better if I knew that clang would optimize away conditional
> expressions when it determines that they are 1) solely determined by the
> NULL-ness of an object and 2) the result of evaluating either branch is
> known to be exactly equal to sending the message to the receiver.
> 
> Is this something that clang does already?
> 
> --Kyle Sluder
> _______________________________________________
> cfe-users mailing list
> cfe-users at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20140212/7f7d8d34/attachment.html>


More information about the cfe-users mailing list