SlideShare a Scribd company logo
1 of 18
Download to read offline
Compiler Tricks
                             Coding with compiler code generation in mind




divendres 22 de març de 13
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
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
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
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
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
Example: updateBackground
              [self setBackgroundColor:_tintColor];


             Lfunc_begin0:
             	 .loc	 1 18 0                  @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/
             SWView.m:18:0
             @ BB#0:
             	 @DEBUG_VALUE: -[SWView updateBackground]:self <- R0+0
             	 @DEBUG_VALUE: -[SWView updateBackground]:_cmd <- R1+0
             	 .loc	 1 20 5 prologue_end     @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/
             SWView.m:20:5
             	 movw	 r1, :lower16:(_OBJC_IVAR_$_SWView._tintColor-(LPC0_0+4))
             Ltmp0:
             	 movt	 r1, :upper16:(_OBJC_IVAR_$_SWView._tintColor-(LPC0_0+4))
             	 movw	 r3, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_1+4))
             LPC0_0:
             	 add	 r1, pc
             	 movt	 r3, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_1+4))
             LPC0_1:
             	 add	 r3, pc
             	 ldr	 r1, [r1]
             	 ldr	 r2, [r0, r1]
             	 ldr	 r1, [r3]
             	 b.w	 _objc_msgSend
             Ltmp1:
             Lfunc_end0:




divendres 22 de març de 13
Example: updateBackground2
              [self setBackgroundColor:self.tintColor];

             Lfunc_begin1:
             	 .loc	 1 23 0                  @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/
             SWView.m:23:0
             @ BB#0:
             	 .loc	 1 23 1 prologue_end     @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/
             SWView.m:23:1
             	 push	 {r4, r5, r7, lr}
             	 mov	 r4, r0
             	 @DEBUG_VALUE: -[SWView updateBackground2]:self <- R4+0
             	 .loc	 1 25 5                  @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/
             SWView.m:25:5
             Ltmp2:
             	 movw	 r0, :lower16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC1_0+4))
             	 movt	 r0, :upper16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC1_0+4))
             Ltmp3:
             	 .loc	 1 23 1                  @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/
             SWView.m:23:1
             	 add	 r7, sp, #8
             Ltmp4:
             	 @DEBUG_VALUE: -[SWView updateBackground2]:self <- R0+0
             	 @DEBUG_VALUE: -[SWView updateBackground2]:_cmd <- R1+0
             	 .loc	 1 25 5                  @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/
             SWView.m:25:5

             .
             .
             .




divendres 22 de març de 13
.
             .
             .

             LPC1_0:
             	 add	 r0, pc
             Ltmp5:
             	 ldr	 r1, [r0]
             Ltmp6:
             	 mov	 r0, r4
             	 blx	 _objc_msgSend
             	 @ InlineAsm Start
             	 mov	 r7, r7	 	      @ marker for objc_retainAutoreleaseReturnValue
             	 @ InlineAsm End
             	 blx	 _objc_retainAutoreleasedReturnValue
             	 mov	 r5, r0
             	 movw	 r0, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_1+4))
             	 movt	 r0, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_1+4))
             	 mov	 r2, r5
             LPC1_1:
             	 add	 r0, pc
             	 ldr	 r1, [r0]
             	 mov	 r0, r4
             	 blx	 _objc_msgSend
             	 mov	 r0, r5
             	 pop.w	 {r4, r5, r7, lr}
             	 b.w	 _objc_release
             Ltmp7:
             Lfunc_end1:




divendres 22 de març de 13
tintColor
              self.tintColor;


             Lfunc_begin2:
             	 .loc	 1 12 0                  @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/
             SWView.m:12:0
             @ BB#0:
             	 @DEBUG_VALUE: -[SWView tintColor]:self <- R0+0
             	 @DEBUG_VALUE: -[SWView tintColor]:_cmd <- R1+0
             	 .loc	 1 12 32 prologue_end    @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/
             SWView.m:12:32
             	 movw	 r1, :lower16:(_OBJC_IVAR_$_SWView._tintColor-(LPC2_0+4))
             Ltmp8:
             	 movt	 r1, :upper16:(_OBJC_IVAR_$_SWView._tintColor-(LPC2_0+4))
             LPC2_0:
             	 add	 r1, pc
             	 ldr	 r1, [r1]
             	 ldr	 r0, [r0, r1]
             Ltmp9:
             	 bx	 lr
             Ltmp10:
             Lfunc_end2:




divendres 22 de març de 13
Example: bottomRight
              @interface UIView(bottomRightAddition)
              - (CGPoint)bottomRight;
              - (CGPoint)bottomRight2;
              @end


              - (CGPoint)bottomRight
              {
                  CGRect frame = self.frame;
                  return CGPointMake(frame.origin.x + frame.size.width, frame.origin.y +
              frame.size.height);
              }



              - (CGPoint)bottomRight2
              {
                  return CGPointMake(self.frame.origin.x + self.frame.size.width,
              self.frame.origin.y + self.frame.size.height);
              }




divendres 22 de març de 13
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
Lfunc_begin2:
                                             Example: bottomRight
             	    .loc	    1 28 0                  @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/SWView.m:28:0
             @ BB#0:
             	    .loc	    1 28 1 prologue_end     @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/SWView.m:28:1
             	    push	    {r4, r7, lr}
             	    add	     r7, sp, #4
             	    sub	     sp, #16
             	    @DEBUG_VALUE: -[SWView bottomRight]:self <- R1+0
             	    @DEBUG_VALUE: -[SWView bottomRight]:_cmd <- R2+0
             	    mov	     r4, r0
             	    .loc	    1 30 30                 @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/SWView.m:30:30
             Ltmp8:
             	    cbz	     r1, LBB2_2
             @ BB#1:
             Ltmp9:
             	    @DEBUG_VALUE: -[SWView bottomRight]:self <- R1+0
             	    @DEBUG_VALUE: -[SWView bottomRight]:_cmd <- R2+0
             	    movw	    r0, :lower16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC2_0+4))
             	    movt	    r0, :upper16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC2_0+4))
             LPC2_0:
             	    add	     r0, pc
             	    ldr	     r2, [r0]
             Ltmp10:
             	    mov	     r0, sp
             	    blx	     _objc_msgSend_stret
             	    .loc	    1 31 12                 @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/SWView.m:31:12
             	    vldr	    s4, [sp]
             	    vldr	    s2, [sp, #4]
             	    vldr	    s6, [sp, #8]
             	    vldr	    s0, [sp, #12]
             	    b	       LBB2_3
             Ltmp11:
             LBB2_2:
             	    @DEBUG_VALUE: -[SWView bottomRight]:_cmd <- R2+0
             	    vmov.i32	d0, #0x0
             	    mov	     r0, sp
             	    vmov.i32	q8, #0x0
             	    vorr	    d1, d0, d0
             	    vorr	    d3, d0, d0
             	    vorr	    d2, d0, d0
             	    .loc	    1 30 30                 @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/SWView.m:30:30
             	    vst1.32	 {d16, d17}, [r0]
             LBB2_3:
             Ltmp12:
             	    @DEBUG_VALUE: -[SWView bottomRight]:_cmd <- R2+0
             	    .loc	    1 31 12                 @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/SWView.m:31:12
             	    vadd.f32	d2, d2, d3
             	    vadd.f32	d0, d1, d0
             Ltmp13:
             	    @DEBUG_VALUE: CGPointMake:y <- S0+0
             	    @DEBUG_VALUE: CGPointMake:x <- S4+0
             	    vstr	    s4, [r4]
             	    vstr	    s0, [r4, #4]
             	    add	     sp, #16
             	    vorr	    d0, d0, d0              @ vnop
             	    pop	     {r4, r7, pc}
             Ltmp14:
             Lfunc_end2:

divendres 22 de març de 13
Example: bottomRight2
             Lfunc_begin3:
             	    .loc	    1 34 0                  @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/SWView.m:34:0
             @ BB#0:
             	    .loc	    1 34 1 prologue_end     @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/SWView.m:34:1
             	    push	    {r4, r5, r6, r7, lr}
             	    sub.w	 r4, sp, #24
             	    add	     r7, sp, #12
             	    bic	     r4, r4, #15
             	    mov	     sp, r4
             	    vst1.64	 {d8, d9}, [r4, :128]
             	    vstr	    d10, [r4, #16]
             	    sub	     sp, #64
             	    @DEBUG_VALUE: -[SWView bottomRight2]:self <- R1+0
             	    @DEBUG_VALUE: -[SWView bottomRight2]:_cmd <- R2+0
             	    mov	     r5, r1
             Ltmp15:
             	    @DEBUG_VALUE: -[SWView bottomRight2]:self <- R5+0
             	    mov	     r4, r0
             	    .loc	    1 36 12                 @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/SWView.m:36:12
             Ltmp16:
             	    cbz	     r5, LBB3_2
             @ BB#1:
             Ltmp17:
             	    @DEBUG_VALUE: -[SWView bottomRight2]:self <- R5+0
             	    @DEBUG_VALUE: -[SWView bottomRight2]:_cmd <- R2+0
             	    movw	    r0, :lower16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC3_0+4))
             	    mov	     r1, r5
             	    movt	    r0, :upper16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC3_0+4))
             LPC3_0:
             	    add	     r0, pc
             	    ldr	     r6, [r0]
             	    add	     r0, sp, #48
             	    mov	     r2, r6
             Ltmp18:
             	    blx	     _objc_msgSend_stret
             	    add	     r0, sp, #32
             	    mov	     r1, r5
             	    mov	     r2, r6
             	    vldr	    s16, [sp, #48]
             	    blx	     _objc_msgSend_stret
             	    add	     r0, sp, #16
             	    mov	     r1, r5
             	    mov	     r2, r6
             	    vldr	    s20, [sp, #40]
             	    blx	     _objc_msgSend_stret
             	    mov	     r0, sp
             	    mov	     r1, r5
             	    mov	     r2, r6
             	    vldr	    s18, [sp, #20]
             	    blx	     _objc_msgSend_stret
             	    vadd.f32	d1, d8, d10
             	    vldr	    s0, [sp, #12]
             	    b	       LBB3_3
             .
             .
             .




divendres 22 de març de 13
.
               .
               .

               Ltmp19:
               LBB3_2:
               	    @DEBUG_VALUE: -[SWView bottomRight2]:_cmd <- R2+0
               	    vmov.i32	d0, #0x0
               	    add	     r1, sp, #16
               	    vmov.i32	q8, #0x0
               	    add	     r2, sp, #32
               Ltmp20:
               	    vorr	    d9, d0, d0
               	    add	     r3, sp, #48
               	    vorr	    d1, d0, d0
               	    mov	     r0, sp
               	    vst1.32	 {d16, d17}, [r3]
               	    vst1.32	 {d16, d17}, [r2]
               	    vst1.32	 {d16, d17}, [r1]
               	   vst1.32	    {d16, d17}, [r0]
               LBB3_3:
               Ltmp21:
               	   @DEBUG_VALUE: -[SWView bottomRight2]:_cmd <- R2+0
               	   vadd.f32	   d0, d9, d0
               Ltmp22:
               	   @DEBUG_VALUE: CGPointMake:x <- S2+0
               	   @DEBUG_VALUE: CGPointMake:y <- S0+0
               	   vstr	 s2, [r4]
               	   vstr	 s0, [r4, #4]
               	   add	   r4, sp, #64
               	   vld1.64	    {d8, d9}, [r4, :128]
               	   vldr	 d10, [r4, #16]
               	   sub.w	 r4, r7, #12
               	   mov	   sp, r4
               	   vorr	 d0, d0, d0               @ vnop
               	   pop	   {r4, r5, r6, r7, pc}
               Ltmp23:
               Lfunc_end3:




divendres 22 de març de 13
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
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
Thanks for your attention.




divendres 22 de març de 13

More Related Content

What's hot

Turbo C Compiler Reports
Turbo C Compiler Reports Turbo C Compiler Reports
Turbo C Compiler Reports Sunil Kumar R
 
cmp104 lec 8
cmp104 lec 8cmp104 lec 8
cmp104 lec 8kapil078
 
1.1 programming fundamentals
1.1 programming fundamentals1.1 programming fundamentals
1.1 programming fundamentalsJawad Khan
 
OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)Kai-Feng Chou
 
OOP in C - Inherit (Chinese Version)
OOP in C - Inherit (Chinese Version)OOP in C - Inherit (Chinese Version)
OOP in C - Inherit (Chinese Version)Kai-Feng Chou
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programminggajendra singh
 
Talk Python To Me: Stream Processing in your favourite Language with Beam on ...
Talk Python To Me: Stream Processing in your favourite Language with Beam on ...Talk Python To Me: Stream Processing in your favourite Language with Beam on ...
Talk Python To Me: Stream Processing in your favourite Language with Beam on ...Aljoscha Krettek
 
Introduction to llvm
Introduction to llvmIntroduction to llvm
Introduction to llvmTao He
 
Embedded c programming22 for fdp
Embedded c programming22 for fdpEmbedded c programming22 for fdp
Embedded c programming22 for fdpPradeep Kumar TS
 
Challenges in GPU compilers
Challenges in GPU compilersChallenges in GPU compilers
Challenges in GPU compilersAnastasiaStulova
 
Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 studrohassanie
 
C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1Rumman Ansari
 
LD_PRELOAD Exploitation - DC9723
LD_PRELOAD Exploitation - DC9723LD_PRELOAD Exploitation - DC9723
LD_PRELOAD Exploitation - DC9723Iftach Ian Amit
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointJavaTpoint.Com
 

What's hot (20)

Turbo C Compiler Reports
Turbo C Compiler Reports Turbo C Compiler Reports
Turbo C Compiler Reports
 
cmp104 lec 8
cmp104 lec 8cmp104 lec 8
cmp104 lec 8
 
1.1 programming fundamentals
1.1 programming fundamentals1.1 programming fundamentals
1.1 programming fundamentals
 
C tutorial
C tutorialC tutorial
C tutorial
 
OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)
 
OOP in C - Inherit (Chinese Version)
OOP in C - Inherit (Chinese Version)OOP in C - Inherit (Chinese Version)
OOP in C - Inherit (Chinese Version)
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Talk Python To Me: Stream Processing in your favourite Language with Beam on ...
Talk Python To Me: Stream Processing in your favourite Language with Beam on ...Talk Python To Me: Stream Processing in your favourite Language with Beam on ...
Talk Python To Me: Stream Processing in your favourite Language with Beam on ...
 
C tutorial
C tutorialC tutorial
C tutorial
 
Introduction to llvm
Introduction to llvmIntroduction to llvm
Introduction to llvm
 
Gnu debugger
Gnu debuggerGnu debugger
Gnu debugger
 
Embedded c programming22 for fdp
Embedded c programming22 for fdpEmbedded c programming22 for fdp
Embedded c programming22 for fdp
 
Alp 05
Alp 05Alp 05
Alp 05
 
Challenges in GPU compilers
Challenges in GPU compilersChallenges in GPU compilers
Challenges in GPU compilers
 
Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 stud
 
Nug2004 yhe
Nug2004 yheNug2004 yhe
Nug2004 yhe
 
C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1
 
LD_PRELOAD Exploitation - DC9723
LD_PRELOAD Exploitation - DC9723LD_PRELOAD Exploitation - DC9723
LD_PRELOAD Exploitation - DC9723
 
Next Stop, Android
Next Stop, AndroidNext Stop, Android
Next Stop, Android
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 

Viewers also liked

No.6 soal un 2012 fisika gaya & energi
No.6 soal un 2012 fisika gaya & energiNo.6 soal un 2012 fisika gaya & energi
No.6 soal un 2012 fisika gaya & energiEma Rachmawati
 
Do you have a competitive advantage and how sustainable is it
Do you have a competitive advantage and how sustainable is itDo you have a competitive advantage and how sustainable is it
Do you have a competitive advantage and how sustainable is itmilemadinah
 
Terrasatii - Produtos e serviços
Terrasatii - Produtos e serviçosTerrasatii - Produtos e serviços
Terrasatii - Produtos e serviçosMatheus Ribeiro
 
Dixie Graphics Signage Slideshow
Dixie Graphics Signage SlideshowDixie Graphics Signage Slideshow
Dixie Graphics Signage SlideshowDixieGraphics
 
120327262 makalah-korosi
120327262 makalah-korosi120327262 makalah-korosi
120327262 makalah-korosiBeny Firiya
 
TRAC Company Values
TRAC Company ValuesTRAC Company Values
TRAC Company ValuesDavid Reed
 
Developmental attitude towards science among girls of secondary
Developmental attitude towards science among girls of secondaryDevelopmental attitude towards science among girls of secondary
Developmental attitude towards science among girls of secondaryIAEME Publication
 
New Flexo Plate Technology - Does Pinning Top Nail It?
New Flexo Plate Technology - Does Pinning Top Nail It?New Flexo Plate Technology - Does Pinning Top Nail It?
New Flexo Plate Technology - Does Pinning Top Nail It?DixieGraphics
 
Scrum fan shirt - T-Shirt für alle Fans des agilen Projektmanegmentframeworks...
Scrum fan shirt - T-Shirt für alle Fans des agilen Projektmanegmentframeworks...Scrum fan shirt - T-Shirt für alle Fans des agilen Projektmanegmentframeworks...
Scrum fan shirt - T-Shirt für alle Fans des agilen Projektmanegmentframeworks...scrumfanshirt_de
 
Making Capital Budgeting Decisions
Making Capital Budgeting DecisionsMaking Capital Budgeting Decisions
Making Capital Budgeting DecisionsAlice Bell, M.B.A.
 
Tóm tắt tình huống
Tóm tắt tình huốngTóm tắt tình huống
Tóm tắt tình huốngYến Lê
 

Viewers also liked (20)

No.6 soal un 2012 fisika gaya & energi
No.6 soal un 2012 fisika gaya & energiNo.6 soal un 2012 fisika gaya & energi
No.6 soal un 2012 fisika gaya & energi
 
Do you have a competitive advantage and how sustainable is it
Do you have a competitive advantage and how sustainable is itDo you have a competitive advantage and how sustainable is it
Do you have a competitive advantage and how sustainable is it
 
Cherk_kray_11-13
Cherk_kray_11-13Cherk_kray_11-13
Cherk_kray_11-13
 
Diaspositivas de programas
Diaspositivas de programasDiaspositivas de programas
Diaspositivas de programas
 
Lei 002.2014
Lei 002.2014Lei 002.2014
Lei 002.2014
 
Terrasatii - Produtos e serviços
Terrasatii - Produtos e serviçosTerrasatii - Produtos e serviços
Terrasatii - Produtos e serviços
 
Tweet tweet
Tweet tweet   Tweet tweet
Tweet tweet
 
Dixie Graphics Signage Slideshow
Dixie Graphics Signage SlideshowDixie Graphics Signage Slideshow
Dixie Graphics Signage Slideshow
 
120327262 makalah-korosi
120327262 makalah-korosi120327262 makalah-korosi
120327262 makalah-korosi
 
Branding - marka wnetrz
Branding - marka wnetrzBranding - marka wnetrz
Branding - marka wnetrz
 
TRAC Company Values
TRAC Company ValuesTRAC Company Values
TRAC Company Values
 
Developmental attitude towards science among girls of secondary
Developmental attitude towards science among girls of secondaryDevelopmental attitude towards science among girls of secondary
Developmental attitude towards science among girls of secondary
 
New Flexo Plate Technology - Does Pinning Top Nail It?
New Flexo Plate Technology - Does Pinning Top Nail It?New Flexo Plate Technology - Does Pinning Top Nail It?
New Flexo Plate Technology - Does Pinning Top Nail It?
 
Scrum fan shirt - T-Shirt für alle Fans des agilen Projektmanegmentframeworks...
Scrum fan shirt - T-Shirt für alle Fans des agilen Projektmanegmentframeworks...Scrum fan shirt - T-Shirt für alle Fans des agilen Projektmanegmentframeworks...
Scrum fan shirt - T-Shirt für alle Fans des agilen Projektmanegmentframeworks...
 
Understanding the HIPPA Act
Understanding the HIPPA ActUnderstanding the HIPPA Act
Understanding the HIPPA Act
 
Making Capital Budgeting Decisions
Making Capital Budgeting DecisionsMaking Capital Budgeting Decisions
Making Capital Budgeting Decisions
 
Tóm tắt tình huống
Tóm tắt tình huốngTóm tắt tình huống
Tóm tắt tình huống
 
Case
CaseCase
Case
 
Manuel diapositiva
Manuel diapositivaManuel diapositiva
Manuel diapositiva
 
Manuel diapositiva
Manuel diapositivaManuel diapositiva
Manuel diapositiva
 

Similar to Compiler tricks

Mac ruby deployment
Mac ruby deploymentMac ruby deployment
Mac ruby deploymentThilo Utke
 
Oh the compilers you'll build
Oh the compilers you'll buildOh the compilers you'll build
Oh the compilers you'll buildMark Stoodley
 
Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...Andrey Karpov
 
cReComp : Automated Design Tool for ROS-Compliant FPGA Component
cReComp : Automated Design Tool  for ROS-Compliant FPGA Component cReComp : Automated Design Tool  for ROS-Compliant FPGA Component
cReComp : Automated Design Tool for ROS-Compliant FPGA Component Kazushi Yamashina
 
PVS-Studio and Continuous Integration: TeamCity. Analysis of the Open RollerC...
PVS-Studio and Continuous Integration: TeamCity. Analysis of the Open RollerC...PVS-Studio and Continuous Integration: TeamCity. Analysis of the Open RollerC...
PVS-Studio and Continuous Integration: TeamCity. Analysis of the Open RollerC...Andrey Karpov
 
Instrumentation of Unsafe C Methods to Safe Methods (Samuel Bret Collie) (1)
Instrumentation of Unsafe C Methods to Safe Methods (Samuel Bret Collie) (1)Instrumentation of Unsafe C Methods to Safe Methods (Samuel Bret Collie) (1)
Instrumentation of Unsafe C Methods to Safe Methods (Samuel Bret Collie) (1)Samuel Collie
 
Knowledge Sharing Session on JavaScript Source Maps & Angular Compilation
Knowledge Sharing Session on JavaScript Source Maps & Angular CompilationKnowledge Sharing Session on JavaScript Source Maps & Angular Compilation
Knowledge Sharing Session on JavaScript Source Maps & Angular CompilationMd.Zahidur Rahman
 
Webinar: Começando seus trabalhos com Machine Learning utilizando ferramentas...
Webinar: Começando seus trabalhos com Machine Learning utilizando ferramentas...Webinar: Começando seus trabalhos com Machine Learning utilizando ferramentas...
Webinar: Começando seus trabalhos com Machine Learning utilizando ferramentas...Embarcados
 
Pragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Pragmatic Optimization in Modern Programming - Ordering Optimization ApproachesPragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Pragmatic Optimization in Modern Programming - Ordering Optimization ApproachesMarina Kolpakova
 
PHP QA Tools
PHP QA ToolsPHP QA Tools
PHP QA Toolsrjsmelo
 
Adding a BOLT pass
Adding a BOLT passAdding a BOLT pass
Adding a BOLT passAmir42407
 
MongoDB.local Austin 2018: MongoDB Ops Manager + Kubernetes
MongoDB.local Austin 2018: MongoDB Ops Manager + KubernetesMongoDB.local Austin 2018: MongoDB Ops Manager + Kubernetes
MongoDB.local Austin 2018: MongoDB Ops Manager + KubernetesMongoDB
 
Infecting the Embedded Supply Chain
 Infecting the Embedded Supply Chain Infecting the Embedded Supply Chain
Infecting the Embedded Supply ChainPriyanka Aash
 
Advanced Node.JS Meetup
Advanced Node.JS MeetupAdvanced Node.JS Meetup
Advanced Node.JS MeetupLINAGORA
 
MongoDB.local DC 2018: MongoDB Ops Manager + Kubernetes
MongoDB.local DC 2018: MongoDB Ops Manager + KubernetesMongoDB.local DC 2018: MongoDB Ops Manager + Kubernetes
MongoDB.local DC 2018: MongoDB Ops Manager + KubernetesMongoDB
 
BKK16-302: Android Optimizing Compiler: New Member Assimilation Guide
BKK16-302: Android Optimizing Compiler: New Member Assimilation GuideBKK16-302: Android Optimizing Compiler: New Member Assimilation Guide
BKK16-302: Android Optimizing Compiler: New Member Assimilation GuideLinaro
 
C# Production Debugging Made Easy
 C# Production Debugging Made Easy C# Production Debugging Made Easy
C# Production Debugging Made EasyAlon Fliess
 
Lightweight APIs in mRuby (Михаил Бортник)
Lightweight APIs in mRuby (Михаил Бортник)Lightweight APIs in mRuby (Михаил Бортник)
Lightweight APIs in mRuby (Михаил Бортник)Fwdays
 
Writing a Gem with native extensions
Writing a Gem with native extensionsWriting a Gem with native extensions
Writing a Gem with native extensionsTristan Penman
 

Similar to Compiler tricks (20)

Mac ruby deployment
Mac ruby deploymentMac ruby deployment
Mac ruby deployment
 
Oh the compilers you'll build
Oh the compilers you'll buildOh the compilers you'll build
Oh the compilers you'll build
 
Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...
 
cReComp : Automated Design Tool for ROS-Compliant FPGA Component
cReComp : Automated Design Tool  for ROS-Compliant FPGA Component cReComp : Automated Design Tool  for ROS-Compliant FPGA Component
cReComp : Automated Design Tool for ROS-Compliant FPGA Component
 
Java Decompiler
Java DecompilerJava Decompiler
Java Decompiler
 
PVS-Studio and Continuous Integration: TeamCity. Analysis of the Open RollerC...
PVS-Studio and Continuous Integration: TeamCity. Analysis of the Open RollerC...PVS-Studio and Continuous Integration: TeamCity. Analysis of the Open RollerC...
PVS-Studio and Continuous Integration: TeamCity. Analysis of the Open RollerC...
 
Instrumentation of Unsafe C Methods to Safe Methods (Samuel Bret Collie) (1)
Instrumentation of Unsafe C Methods to Safe Methods (Samuel Bret Collie) (1)Instrumentation of Unsafe C Methods to Safe Methods (Samuel Bret Collie) (1)
Instrumentation of Unsafe C Methods to Safe Methods (Samuel Bret Collie) (1)
 
Knowledge Sharing Session on JavaScript Source Maps & Angular Compilation
Knowledge Sharing Session on JavaScript Source Maps & Angular CompilationKnowledge Sharing Session on JavaScript Source Maps & Angular Compilation
Knowledge Sharing Session on JavaScript Source Maps & Angular Compilation
 
Webinar: Começando seus trabalhos com Machine Learning utilizando ferramentas...
Webinar: Começando seus trabalhos com Machine Learning utilizando ferramentas...Webinar: Começando seus trabalhos com Machine Learning utilizando ferramentas...
Webinar: Começando seus trabalhos com Machine Learning utilizando ferramentas...
 
Pragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Pragmatic Optimization in Modern Programming - Ordering Optimization ApproachesPragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Pragmatic Optimization in Modern Programming - Ordering Optimization Approaches
 
PHP QA Tools
PHP QA ToolsPHP QA Tools
PHP QA Tools
 
Adding a BOLT pass
Adding a BOLT passAdding a BOLT pass
Adding a BOLT pass
 
MongoDB.local Austin 2018: MongoDB Ops Manager + Kubernetes
MongoDB.local Austin 2018: MongoDB Ops Manager + KubernetesMongoDB.local Austin 2018: MongoDB Ops Manager + Kubernetes
MongoDB.local Austin 2018: MongoDB Ops Manager + Kubernetes
 
Infecting the Embedded Supply Chain
 Infecting the Embedded Supply Chain Infecting the Embedded Supply Chain
Infecting the Embedded Supply Chain
 
Advanced Node.JS Meetup
Advanced Node.JS MeetupAdvanced Node.JS Meetup
Advanced Node.JS Meetup
 
MongoDB.local DC 2018: MongoDB Ops Manager + Kubernetes
MongoDB.local DC 2018: MongoDB Ops Manager + KubernetesMongoDB.local DC 2018: MongoDB Ops Manager + Kubernetes
MongoDB.local DC 2018: MongoDB Ops Manager + Kubernetes
 
BKK16-302: Android Optimizing Compiler: New Member Assimilation Guide
BKK16-302: Android Optimizing Compiler: New Member Assimilation GuideBKK16-302: Android Optimizing Compiler: New Member Assimilation Guide
BKK16-302: Android Optimizing Compiler: New Member Assimilation Guide
 
C# Production Debugging Made Easy
 C# Production Debugging Made Easy C# Production Debugging Made Easy
C# Production Debugging Made Easy
 
Lightweight APIs in mRuby (Михаил Бортник)
Lightweight APIs in mRuby (Михаил Бортник)Lightweight APIs in mRuby (Михаил Бортник)
Lightweight APIs in mRuby (Михаил Бортник)
 
Writing a Gem with native extensions
Writing a Gem with native extensionsWriting a Gem with native extensions
Writing a Gem with native extensions
 

Compiler tricks

  • 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
  • 7. Example: updateBackground [self setBackgroundColor:_tintColor]; Lfunc_begin0: .loc 1 18 0 @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/ SWView.m:18:0 @ BB#0: @DEBUG_VALUE: -[SWView updateBackground]:self <- R0+0 @DEBUG_VALUE: -[SWView updateBackground]:_cmd <- R1+0 .loc 1 20 5 prologue_end @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/ SWView.m:20:5 movw r1, :lower16:(_OBJC_IVAR_$_SWView._tintColor-(LPC0_0+4)) Ltmp0: movt r1, :upper16:(_OBJC_IVAR_$_SWView._tintColor-(LPC0_0+4)) movw r3, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_1+4)) LPC0_0: add r1, pc movt r3, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_1+4)) LPC0_1: add r3, pc ldr r1, [r1] ldr r2, [r0, r1] ldr r1, [r3] b.w _objc_msgSend Ltmp1: Lfunc_end0: divendres 22 de març de 13
  • 8. Example: updateBackground2 [self setBackgroundColor:self.tintColor]; Lfunc_begin1: .loc 1 23 0 @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/ SWView.m:23:0 @ BB#0: .loc 1 23 1 prologue_end @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/ SWView.m:23:1 push {r4, r5, r7, lr} mov r4, r0 @DEBUG_VALUE: -[SWView updateBackground2]:self <- R4+0 .loc 1 25 5 @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/ SWView.m:25:5 Ltmp2: movw r0, :lower16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC1_0+4)) movt r0, :upper16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC1_0+4)) Ltmp3: .loc 1 23 1 @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/ SWView.m:23:1 add r7, sp, #8 Ltmp4: @DEBUG_VALUE: -[SWView updateBackground2]:self <- R0+0 @DEBUG_VALUE: -[SWView updateBackground2]:_cmd <- R1+0 .loc 1 25 5 @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/ SWView.m:25:5 . . . divendres 22 de març de 13
  • 9. . . . LPC1_0: add r0, pc Ltmp5: ldr r1, [r0] Ltmp6: mov r0, r4 blx _objc_msgSend @ InlineAsm Start mov r7, r7 @ marker for objc_retainAutoreleaseReturnValue @ InlineAsm End blx _objc_retainAutoreleasedReturnValue mov r5, r0 movw r0, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_1+4)) movt r0, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_1+4)) mov r2, r5 LPC1_1: add r0, pc ldr r1, [r0] mov r0, r4 blx _objc_msgSend mov r0, r5 pop.w {r4, r5, r7, lr} b.w _objc_release Ltmp7: Lfunc_end1: divendres 22 de març de 13
  • 10. tintColor self.tintColor; Lfunc_begin2: .loc 1 12 0 @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/ SWView.m:12:0 @ BB#0: @DEBUG_VALUE: -[SWView tintColor]:self <- R0+0 @DEBUG_VALUE: -[SWView tintColor]:_cmd <- R1+0 .loc 1 12 32 prologue_end @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/ SWView.m:12:32 movw r1, :lower16:(_OBJC_IVAR_$_SWView._tintColor-(LPC2_0+4)) Ltmp8: movt r1, :upper16:(_OBJC_IVAR_$_SWView._tintColor-(LPC2_0+4)) LPC2_0: add r1, pc ldr r1, [r1] ldr r0, [r0, r1] Ltmp9: bx lr Ltmp10: Lfunc_end2: divendres 22 de març de 13
  • 11. Example: bottomRight @interface UIView(bottomRightAddition) - (CGPoint)bottomRight; - (CGPoint)bottomRight2; @end - (CGPoint)bottomRight { CGRect frame = self.frame; return CGPointMake(frame.origin.x + frame.size.width, frame.origin.y + frame.size.height); } - (CGPoint)bottomRight2 { return CGPointMake(self.frame.origin.x + self.frame.size.width, self.frame.origin.y + self.frame.size.height); } 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
  • 13. Lfunc_begin2: Example: bottomRight .loc 1 28 0 @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/SWView.m:28:0 @ BB#0: .loc 1 28 1 prologue_end @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/SWView.m:28:1 push {r4, r7, lr} add r7, sp, #4 sub sp, #16 @DEBUG_VALUE: -[SWView bottomRight]:self <- R1+0 @DEBUG_VALUE: -[SWView bottomRight]:_cmd <- R2+0 mov r4, r0 .loc 1 30 30 @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/SWView.m:30:30 Ltmp8: cbz r1, LBB2_2 @ BB#1: Ltmp9: @DEBUG_VALUE: -[SWView bottomRight]:self <- R1+0 @DEBUG_VALUE: -[SWView bottomRight]:_cmd <- R2+0 movw r0, :lower16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC2_0+4)) movt r0, :upper16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC2_0+4)) LPC2_0: add r0, pc ldr r2, [r0] Ltmp10: mov r0, sp blx _objc_msgSend_stret .loc 1 31 12 @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/SWView.m:31:12 vldr s4, [sp] vldr s2, [sp, #4] vldr s6, [sp, #8] vldr s0, [sp, #12] b LBB2_3 Ltmp11: LBB2_2: @DEBUG_VALUE: -[SWView bottomRight]:_cmd <- R2+0 vmov.i32 d0, #0x0 mov r0, sp vmov.i32 q8, #0x0 vorr d1, d0, d0 vorr d3, d0, d0 vorr d2, d0, d0 .loc 1 30 30 @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/SWView.m:30:30 vst1.32 {d16, d17}, [r0] LBB2_3: Ltmp12: @DEBUG_VALUE: -[SWView bottomRight]:_cmd <- R2+0 .loc 1 31 12 @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/SWView.m:31:12 vadd.f32 d2, d2, d3 vadd.f32 d0, d1, d0 Ltmp13: @DEBUG_VALUE: CGPointMake:y <- S0+0 @DEBUG_VALUE: CGPointMake:x <- S4+0 vstr s4, [r4] vstr s0, [r4, #4] add sp, #16 vorr d0, d0, d0 @ vnop pop {r4, r7, pc} Ltmp14: Lfunc_end2: divendres 22 de març de 13
  • 14. Example: bottomRight2 Lfunc_begin3: .loc 1 34 0 @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/SWView.m:34:0 @ BB#0: .loc 1 34 1 prologue_end @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/SWView.m:34:1 push {r4, r5, r6, r7, lr} sub.w r4, sp, #24 add r7, sp, #12 bic r4, r4, #15 mov sp, r4 vst1.64 {d8, d9}, [r4, :128] vstr d10, [r4, #16] sub sp, #64 @DEBUG_VALUE: -[SWView bottomRight2]:self <- R1+0 @DEBUG_VALUE: -[SWView bottomRight2]:_cmd <- R2+0 mov r5, r1 Ltmp15: @DEBUG_VALUE: -[SWView bottomRight2]:self <- R5+0 mov r4, r0 .loc 1 36 12 @ /Users/joan/MyiPhoneProjects/CompilerTesting/CompilerTesting/SWView.m:36:12 Ltmp16: cbz r5, LBB3_2 @ BB#1: Ltmp17: @DEBUG_VALUE: -[SWView bottomRight2]:self <- R5+0 @DEBUG_VALUE: -[SWView bottomRight2]:_cmd <- R2+0 movw r0, :lower16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC3_0+4)) mov r1, r5 movt r0, :upper16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC3_0+4)) LPC3_0: add r0, pc ldr r6, [r0] add r0, sp, #48 mov r2, r6 Ltmp18: blx _objc_msgSend_stret add r0, sp, #32 mov r1, r5 mov r2, r6 vldr s16, [sp, #48] blx _objc_msgSend_stret add r0, sp, #16 mov r1, r5 mov r2, r6 vldr s20, [sp, #40] blx _objc_msgSend_stret mov r0, sp mov r1, r5 mov r2, r6 vldr s18, [sp, #20] blx _objc_msgSend_stret vadd.f32 d1, d8, d10 vldr s0, [sp, #12] b LBB3_3 . . . divendres 22 de març de 13
  • 15. . . . Ltmp19: LBB3_2: @DEBUG_VALUE: -[SWView bottomRight2]:_cmd <- R2+0 vmov.i32 d0, #0x0 add r1, sp, #16 vmov.i32 q8, #0x0 add r2, sp, #32 Ltmp20: vorr d9, d0, d0 add r3, sp, #48 vorr d1, d0, d0 mov r0, sp vst1.32 {d16, d17}, [r3] vst1.32 {d16, d17}, [r2] vst1.32 {d16, d17}, [r1] vst1.32 {d16, d17}, [r0] LBB3_3: Ltmp21: @DEBUG_VALUE: -[SWView bottomRight2]:_cmd <- R2+0 vadd.f32 d0, d9, d0 Ltmp22: @DEBUG_VALUE: CGPointMake:x <- S2+0 @DEBUG_VALUE: CGPointMake:y <- S0+0 vstr s2, [r4] vstr s0, [r4, #4] add r4, sp, #64 vld1.64 {d8, d9}, [r4, :128] vldr d10, [r4, #16] sub.w r4, r7, #12 mov sp, r4 vorr d0, d0, d0 @ vnop pop {r4, r5, r6, r7, pc} Ltmp23: Lfunc_end3: 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
  • 18. Thanks for your attention. divendres 22 de març de 13