21
LECTURE 22: METHOD INLINING Computer Science 313 – Advanced Programming Topics

Lecture 22: Method Inlining

  • 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

Page 1: Lecture 22: Method  Inlining

LECTURE 22:METHOD INLINING

Computer Science 313 – Advanced Programming Topics

Page 2: Lecture 22: Method  Inlining

Handling Common Subroutines Must perform set of actions in many

places

Page 3: Lecture 22: Method  Inlining

Handling Common Subroutines Must perform set of actions in many

placesPaste code into each different method

Write 1 method called everywhere

Page 4: Lecture 22: Method  Inlining

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

Page 5: Lecture 22: Method  Inlining

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

Page 6: Lecture 22: Method  Inlining

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

Page 7: Lecture 22: Method  Inlining

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

Page 8: Lecture 22: Method  Inlining

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

Page 9: Lecture 22: Method  Inlining

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

Page 10: Lecture 22: Method  Inlining

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

Page 11: Lecture 22: Method  Inlining

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

Page 12: Lecture 22: Method  Inlining

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:

Page 13: Lecture 22: Method  Inlining

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!

Page 14: Lecture 22: Method  Inlining

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

Page 15: Lecture 22: Method  Inlining

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;

}

Page 16: Lecture 22: Method  Inlining

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;

}

Page 17: Lecture 22: Method  Inlining

Simple Java Inlining Results

Page 18: Lecture 22: Method  Inlining

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

Page 19: Lecture 22: Method  Inlining

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!

Page 20: Lecture 22: Method  Inlining

Inlining Even When We Cannot

Page 21: Lecture 22: Method  Inlining

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?