Download presentation
Presentation is loading. Please wait.
Published byBlaise Gibson Modified over 9 years ago
1
Computer Science 313 – Advanced Programming Topics
2
Handling Common Subroutines Must perform set of actions in many places
3
Handling Common Subroutines Must perform set of actions in many places Paste code into each different method Write 1 method called everywhere
4
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
5
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
6
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
7
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; }
8
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; }
9
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; }
10
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 Create absolute nightmare for maintenance
11
Inlining Support in Java Automatically inlines certain small methods But only if and when exact definition known Subclasses cannot change definition later staticfinalprivate 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
12
Inlining Support in Java Automatically inlines certain small methods But only if and when exact definition known Subclasses cannot change definition later staticfinalprivate 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:
13
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!
14
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
15
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; }
16
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 = ADD T O X( Z ); return x; }
17
Simple Java Inlining Results
18
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
19
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!
20
Inlining Even When We Cannot
21
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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.