Does Apple LLVM Compiler Optimize Resource Access?

Consider this Objective-C code (ARC enabled):

[self.aProperty sendMessage]; if (self.aProperty) { [self doSomethingWithProperty:self.aProperty]; } 

I am wondering if rewriting the code for the cut snapshot will be faster (in versions):

 MyPropertyClass* myProperty = self.aProperty; [myProperty sendMessage]; if (myProperty) { [self doSomethingWithProperty:myProperty]; } 

The question is, can Apple LLVM Compiler 3.0 optimize re-access to property recipients? Does any value matter if the property is non-atomic?

If I were to guess, I would say that writing the code below is faster, because the compiler has no guarantee that self.aProperty will not change during these lines. I'm right?

+4
source share
1 answer

More important than the question of atomic and non-atomic, getters are not guaranteed to be clean or idempotent. Calling a getter several times may not be equivalent to calling it once. For example, someNSEnumerator.nextObject will return a different result each time you call it. Therefore, such optimization is not possible in the general case.

However, as far as โ€œfasterโ€, I doubt that the code fragment will be noticeably faster. The correct answer is a profile and see if it matters if this code works often enough to deal with the problem.

+7
source

Source: https://habr.com/ru/post/1436880/


All Articles