Upload
shawn
View
33
Download
0
Embed Size (px)
DESCRIPTION
Computer Science 313 – Advanced Programming Topics. Lecture 22: 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. - PowerPoint PPT Presentation
Citation preview
LECTURE 22:METHOD INLINING
Computer Science 313 – Advanced Programming Topics
Handling Common Subroutines Must perform set of actions in many
places
Handling Common Subroutines Must perform set of actions in many
placesPaste 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 small methods 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 small methods 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 thisHow not to look like an idiot:
Do the Right (Coding) Thing Breakup code & write only small
methods Small ones private final 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 accessor final since 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 int addToX(int y) { return x + y; }
public int getAndUpdateX(int z) {x = x + z;return x;
}
Now We Can Inline
public STATIC int three() { return 3; }
public STATIC int two() { return 2; }
public STATIC int five() {return three()+two();}
PRIVATE int addToX(int y) { return x + y; }
public FINAL int getAndUpdateX(int z) {x = ADDTOX(Z);return x;
}
Simple Java Inlining Results
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 JIT compiler it can use Information gathered as program runs Frequent calls (hot paths) will be found
Next Step To Worry About
Can inline methods 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!
Inlining Even When We Cannot
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?