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

Kyle Sluder kyle at ksluder.com
Tue Feb 11 19:24:11 PST 2014


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



More information about the cfe-users mailing list