lecture 9: just-in-time compilation & peephole optimizations

45
LECTURE 9: JUST-IN-TIME COMPILATION & PEEPHOLE OPTIMIZATIONS CSC 313 – Advanced Programming Topics

Upload: patch

Post on 24-Feb-2016

26 views

Category:

Documents


0 download

DESCRIPTION

CSC 313 – Advanced Programming Topics. Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS . Data Centers & Server Farms. Major expense for companies LucasArts at 4,000+ servers (2008) Dual core/dual processor in each Each one had 16GB RAM 250+ TB of storage total in system - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

LECTURE 9:JUST-IN-TIME COMPILATION &PEEPHOLE OPTIMIZATIONS

CSC 313 – Advanced Programming Topics

Page 2: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

Data Centers & Server Farms Major expense for

companies LucasArts at 4,000+

servers (2008) Dual core/dual processor in

each Each one had 16GB RAM 250+ TB of storage total in

system Powering all of this demanded

53% of Niagara Falls output

Page 3: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

Data Centers & Server Farms In 2006, Google had 25 data centers Data centers held 450,000 servers

Greater diversity in machines Generally < 2 years old 4 – 8 GB RAM each

By 2011, MS wants 800,000 servers

Page 4: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

Optimization Matters

Lots of money at stake LucasArts doubles server count 18

months At least 3 more centers being build by

Google Each center costs $450 - $600 million

1% improvement is huge for these companies For Google that is 4,500+ servers $72 million saved in power worldwide

(2005)

Page 5: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

Traditional Approach

Entire program compiled and optimized once Target single processor (e.g., Pentium 4,

Power) Creates static machine language

executable Repeatedly analyze code during

optimization After each step reanalyze new result to

improve Does anything to get last 1%

improvement

Page 6: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

Compilation Is Slow

Page 7: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

Optimizer Limited

Compiler must examine code as it is written Cannot take advantage of many situationswhile (true) { String line = scanner.readLine(); int i; if (line.compareTo(“EXIT”) != 0) { i = 1; } else { i = 0; } // What is the value of i?

Page 8: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

JIT to the Rescue

Add Just-In-Time compiler (JIT) As it runs, system will spy on methods Look for patterns that can yield

optimization When good pattern found, recompile

Page 9: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

JIT to the Rescue

Add Just-In-Time compiler (JIT) As it runs, system will spy on methods Look for patterns that can yield

optimization When good pattern found, recompileExecution Time

Original 1.00

Page 10: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

JIT to the Rescue

Add Just-In-Time compiler (JIT) As it runs, system will spy on methods Look for patterns that can yield

optimization When good pattern found, recompileExecution Time

Original 1.00 W/Speedup - 0.20

Page 11: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

JIT to the Rescue

Add Just-In-Time compiler (JIT) As it runs, system will spy on methods Look for patterns that can yield

optimization When good pattern found, recompileExecution Time

Original 1.00 W/Speedup - 0.20

Total 1.50

Page 12: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

What Happened? (1)

Program must examine methods as they run

Page 13: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

What Happened? (1)

Program must examine methods as they run

Page 14: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

What Happened? (1)

Program must examine methods as they run Cannot just execute on computer at top

speed

Page 15: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

What Happened? (1)

Program must examine methods as they run Cannot just execute on computer at top

speed

Page 16: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

What Happened? (1)

Program must examine methods as they run Cannot just execute on computer at top

speed Will need to evaluate & interpret

instructions

Page 17: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

What Happened? (1)

Program must examine methods as they run Cannot just execute on computer at top

speed Will need to evaluate & interpret

instructions

Page 18: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

What Happened? (1)

Program must examine methods as they run Cannot just execute on computer at top

speed Will need to evaluate & interpret

instructions 10 times slower to interpret instructions

Page 19: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

What Happened? (1)

Program must examine methods as they run Cannot just execute on computer at top

speed Will need to evaluate & interpret

instructions 10 times slower to interpret instructions

Page 20: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

What Happened? (1)

Program must examine methods as they run Cannot just execute on computer at top

speed Will need to evaluate & interpret

instructions 10 times slower to interpret instructions

Analyze results to see if optimization found

Page 21: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

What Happened? (2)

Page 22: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

What Happened? (2)

Compiling done once, if we were not using JIT Will take forever (~10 minutes in coder

speak) Reducing even 1% still more than

employees salary As program runs, JIT compiles &

recompiles Each recompilation smaller & faster, but

still slow But compilation now added to total

execution time

Page 23: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

What Went WrongYou're bound to be unhappy if you optimize everything.

Page 24: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

What Went WrongYou're bound to be unhappy if you optimize everything.

Execution TimeOriginal 1.00 W/Overhead 0.70 W/Speedup - 0.20

Total 1.50

Page 25: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

Can We Reduce Costs?

Not worth doing this for certain methods main() String.offsetByCodePoints() Most constructors Anything that Matt wrote

Ignore cold code (code not run often/long) Code not run enough to justify JIT costs Code not important enough to justify JIT

costs

Page 26: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

Some Like It Hot

JIT examines only hottest methods Executed most often or for the longest

time Hot methods are where program's

time spent Need data to analyze; only these provide

it Also these methods only where impact

felt When profitable, recompile hot

methods So the JIT will do this only if costs < time

saved

Page 27: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

Should We Recompile?

When we need to make this decision… Time to compile code must be known Need to find out how often method run Improvement percentage also needed Naturally, there is no way to know any of

this What can we do?

Do best, hope & pray that future resembles past

Make wild- scientific guess about how to do

Page 28: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

Does It Work?

Dynamo runs programs 10% faster 45,000+ servers would not be needed by

Google Would have saved US $270 million in 2005

power bills

Page 29: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

Does It Work?

Dynamo runs programs 10% faster 45,000+ servers would not be needed by

Google Would have saved US $270 million in 2005

power bills

Execution TimeOriginal 1.00 W/Overhead 0.10 W/Speedup - 0.20

Total 0.90

Page 30: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

What Does It All Mean?

Optimizations can make a huge difference Without our knowledge this will already

be done Always check "optimizations" do no

harm Compilers optimize a lot, but only if can

prove safe Know compiler details if you want

fast code After all, this is only way to write

optimizable code

Page 31: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

Instances of Class Class

Objects representing classes used by program What fields is has & how fields laid out in

memory Starting address for methods to call them

in code This is structure (literally) extended by

subclasses Superclass code expects members to

not move Overriden method has starting address

changed Add fields to end to save older field

definition But, we must ask, what about

optimizations?

Page 32: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

Normal Method Call

public class Super {private void foo() {

System.err.print(“Foo”);}public void bar() {

foo();System.err.println(“bar”);

}}Super s = new Super();s.bar();

Class Superfoo() StartAddress1bar() StartAddress2

Page 33: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

Polymorphic Method Call

public class Super {private void foo() { … }public void bar() {

foo();System.err.println(“bar”);

}}public class Sub extends Super {

private void foo() {System.err.print(“Not foo”);

}}Super s = new Sub();s.bar()

Class Superfoo() StartAddress1bar() StartAddress2

Class Subfoo() StartAddress3bar() StartAddress2

Page 34: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

What Is Done For Us

Good news! JIT compilers are very smart If class has subclasses, will be recorded

& used Examines what the types of instances

used are If one method definition can ever be

used… JIT compiler can skip step to look up

address If more possible, but only one

actually used… Slightly slower, but guess address & add

failpath SOL if many used, since lookup is

necessary

Page 35: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

Help Compiler Help Us

Use final keyword to let compiler know: Specify that a variable keeps initial value Method not overriden & starting address

constant Compiler can do as much as possible w/o

checks!

Page 36: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

Compiler Helping Us

With final compiler can do many things Getter & setter method calls become

direct access Addresses hardcoded & lookups

eliminated Use keyhole optimizations relying on

actual values

Page 37: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

final Method Calls

public class Super {private final void foo() {

System.err.print(“Foo”);}public void bar() {

foo();System.err.println(“bar”);

}}Super s = new Super();s.bar();

Page 38: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

final Field Uses

public class GoodField {private int LENGTH = 8;public long recombine(byte[] arr){

long sum = 0;for (int i = 0; i < LENGTH; i++){ sum = (sum * LENGTH) + arr[i];}return sum;

}}

Page 39: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

final Field Uses

public class GoodField {private final int LENGTH = 8;public long recombine(byte[] arr){

long sum = 0;for (int i = 0; i < LENGTH; i++){ sum = (sum * LENGTH) + arr[i];}return sum;

}}

Page 40: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

final Field Uses

public class GoodField {private final int LENGTH = 8;public long recombine(byte[] arr){

long sum = 0;for (int i = 0; i < 8; i++){ sum = (sum * 8) + arr[i];}return sum;

}}

Page 41: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

final Field Uses

public class GoodField {private final int LENGTH = 8;public long recombine(byte[] arr){

long sum = 0;for (int i = 0; i < 8; i++){ sum = (sum << 3) + arr[i];}return sum;

}}

Page 42: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

Rules of Optimization

NO!Not Yet

For experts only

Page 43: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

Problems With final

Remember that final is a negative option It removes options and limits future

possibilities Code becomes static and its reuse may

be hurt Each savings is small (remember

Amdahl's Law) Use when ABSOLUTELY

NECESSARY ONLY

Page 44: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

Problems With final

Remember that final is a negative option It removes options and limits future

possibilities Code becomes static and its reuse may

be hurt Each savings is small (remember

Amdahl's Law) Use when ABSOLUTELY

NECESSARY ONLY

Isolate code that changes from code that stays the same

Page 45: Lecture 9: Just-in-Time COMPILATION & PEEPHOLE OPTIMIZATIONS

For Next Lecture

Read pages 79 – 94 in the book What is the DECORATOR PATTERN? Can we learn design from coffee shops?

They are LOUSY coders who mess up great systems

What do the following have in common: Dijkstra’s algorithm Household finance program E-mail program