1. Compiler Tricks
Coding with compiler code generation in mind
divendres 22 de març de 13
2. Coding with compiler code generation in mind
Background
• Compiler technology is very mature nowadays but there is a growing
disconnection between what programmers think compilers optimize and
what they actually do.
• Not all computer languages can be optimized the same way. C and C++
have a lot of built in features specially designed to help compilers
optimize code.
• Objective-C is a highly dynamic language that forbids some compiler
optimizations.
• Sending a message to an object is at most as fast as an optimized
switch statement. This is comparatively very expensive compared to a C
function call.
divendres 22 de març de 13
3. Coding with compiler code generation in mind
What are our tools?
• ARC changed the way we program in Objective-C.
• ARC can be used to our advantage to make the compiler generate
shorter and faster code than it was possible (or practical) before.
• With ARC, Objective-C property accessors have lost one of their original
goal: Helping on memory management.
• Reading and setting iVars directly is very fast, and ARC takes care of all
memory management responsibility.
• Thanks to ARC, storing temporary object references on the stack is now
safe and can provide significant performance boosts in almost all
circumstances.
divendres 22 de març de 13
4. Coding with compiler code generation in mind
Testing Scenario
• We will provide several different method implementations and will look
at what Assembly code the compiler generated for them.
• Build Settings are set to:
- Compiler: LLVM compiler 4.2
- Target: iOS Device
- Configuration: Release
- Optimization Level: Fastest, Smallest
divendres 22 de març de 13
5. Example: updateBackground
@interface SWView : UIView
@property (nonatomic) UIColor* tintColor;
@end
- (void)updateBackground
{
[self setBackgroundColor:_tintColor];
}
- (void)updateBackground2
{
[self setBackgroundColor:self.tintColor];
}
divendres 22 de març de 13
6. Example: updateBackground
[self setBackgroundColor:_tintColor];
• 10 lines of assembly code
• 1 single call to objc_msg_send
[self setBackgroundColor:self.tintColor];
• 22 lines of assembly code
• 2 calls to objc_msg_send
divendres 22 de març de 13
12. Example: bottomRight
- (CGPoint)bottomRight
• 30 lines of assembly code
• 1 single call to objc_msgSend - (CGRect)frame
- (CGPoint)bottomRight2
• 59 lines of assembly code
• 4 calls to objc_msgSend - (CGRect)frame
divendres 22 de març de 13
16. Coding with compiler code generation in mind
More optimization opportunities
• Loops: Don’t do this:
for ( NSInteger i=0; i<array.count ; i++ )
• @protected iVars: Instance variables are @protected by default, this
means you can use them in subclasses, as long as they are declared
on the superclass @interface section.
• @public iVars: No need to use @property at all !!. As odd as it may seem
you can access @public iVars with the -> operator
object->myIvar
This is still safe from the point of view of memory management and it removes a lot of
overhead. Only recommended in very particular cases (struct like objects)
• __unsafe_unretained object references and collections of unretained
objects using Core Foundation initialization functions.
divendres 22 de març de 13
17. Coding with compiler code generation in mind
Conclusions
• Formerly recommended property accessor conventions are no longer your
friend for memory safe coding. Your best friend is now ARC.
• No need to use Obj-C properties unless you plan to observe them. Use iVars
instead.
• Do not call a method more than once in your implementation of another
method, the compiler will *not* optimize repeated calls.
• Always use local variables to store objects temporarily within the scope of a
method. Avoid calling a method over and over.
• Take out of loops all method invocations returning the same objects. Store
temporary values in local variables outside the loop boundaries.
• Do not assume the Compiler will do a particular optimization. Look at
assembly code for commonly used coding patterns. Learn which are the best
and get the habit to apply them in your code.
divendres 22 de març de 13