1 / 21

Lecture 18: Method Inlining

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.

sora
Download Presentation

Lecture 18: Method Inlining

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 313 – Advanced Programming Topics Lecture 18:Method Inlining

  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 for maintenance

  11. 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

  12. 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:

  13. 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!

  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 accessorfinalsince 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 intaddToX(int y) { return x + y; }public intgetAndUpdateX(int z) { x = x + z; return x;}

  16. 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;}

  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 JITcompiler it can use • Information gathered as program runs • Frequent calls (hot paths) will be found

  19. 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!

  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?

More Related