Lecture 18: Method Inlining
210 likes | 324 Vues
Computer Science 313 – Advanced Programming Topics. Lecture 18: Method Inlining. Handling Common Subroutines. Must perform set of actions in many places. Handling Common Subroutines. Must perform set of actions in many places. Paste code into each different method.
Lecture 18: Method Inlining
E N D
Presentation Transcript
Computer Science 313 – Advanced Programming Topics Lecture 18:Method Inlining
Handling Common Subroutines • Must perform set of actions in many places
Handling Common Subroutines • Must perform set of actions in many places • Paste code into each different method • Write 1 method called everywhere
Why Write a Separate Method • Fewer errors from not updating 1 copy • Code is much simpler to debug • Optimizing & refactoring code far simpler • Makes code more reusable
Why Copy-and-Paste Code • Calls expensive relative to most code • Calls will take 10-12 instructions each • Violates assumption of next instruction executed • Compiler may be prevented from optimizing • Unadulterated stupidity making these comments • In many situations,costs do not matter • Only on critical path should we even think about this • Remember Amdahl’s law:10% slowdown to 20% of code = 0.97 slowdown
Method Inlining • From Michael Hind, a leading IBM researcher • Best solution looks like copy-and-paste code • Easiest way to tell if someone a poseur
What is Inlining? • Replaces method call with actual code • Acts as if written using copy-and-paste • But does not suck when maintaining code public int add(int a, int b) {if (b == 3) { return add3(a); }return a + b; } public int add3(int x) {return x + 3; }
What is Inlining? • Replaces method call with actual code • Acts as if written using copy-and-paste • But does not suck when maintaining code public int add(int a, int b) {if (b == 3) { return add3(a); }return a + b; } public int add3(int x) {return x + 3; }
What is Inlining? • Replaces method call with actual code • Acts as if written using copy-and-paste • But does not suck when maintaining code public int add(int a, int b) {if (b == 3) { return a + 3; }return a + b; } public int add3(int x) {return x + 3; }
C/C++ Inlining Options • Suggest to compiler with inline keyword • Allows programmer to hint at function to inline • Compiler may override if not a good idea inline int add3(int x) { return x + 3; } • Rewrite function as macro using #define • Global search-and-replace performed on code • Compiler has no choice; function forced inline • Create absolute nightmare for maintenance
Inlining Support in Java • Automatically inlines certain smallmethods • But only if and when exact definition known • Subclasses cannot change definition later • Method must be static, final, or private • Code must be written to use this skill • Rare for people to know details of this • Simple poseur detector from not knowing this
Inlining Support in Java • Automatically inlines certain smallmethods • But only if and when exact definition known • Subclasses cannot change definition later • Method must be static, final, or private • Code must be written to use this skill • Rare for people to know details of this • Simple poseur detector from not knowing this How not to look like an idiot:
Do the Right (Coding) Thing • Breakup code & write only small methods • Small ones privatefinal to get inlined • Methods far easier to write, debug, & reuse • All benefits and no slowdown!
CSC212/213 Professor Fun! • Make methods static when possible • Code becomes easier to read and follow • Ensures everything can be inlined • Make fields private & use get methods • Make accessorfinalsince it cannot be changed • Guess what, the getter code gets inlined
Bad Methods; No Inline public int three() { return 3; }public int two() { return 2; }public int five() { return three() + two(); }public intaddToX(int y) { return x + y; }public intgetAndUpdateX(int z) { x = x + z; return x;}
Now We Can Inline public staticint three() { return 3; }public staticinttwo() { return 2; }public staticintfive() {return three()+two();}privateintaddToX(int y) { return x + y; }public final intgetAndUpdateX(int z) { x = addToX(z); return x;}
JIT Advantage • So far,inlining based on static analysis • Information available when program compiled • Only inlines small methods, since they are safe • But calling larger methods also takes time • Java has JITcompiler it can use • Information gathered as program runs • Frequent calls (hot paths) will be found
Next Step To Worry About • Can inlinemethods on critical path • Even when larger than otherwise allowed • Even if not final, if calls are not polymorphic • JVM does this automatically • So may not hurt to use patterns on critical path!
For Next Class • Lab #5 on Angel & due week from Fri. • Read pages 191 - 202 in book • Command pattern will be focus of reading • Great for when we need to use remote control • How do we use Command pattern? • How does it fit in with other patterns? • Where should this be used? • Is the pattern behavioral, creational, or structural?