Just-In-Time Compilation Thiemo Bucciarelli Institute for Software - - PowerPoint PPT Presentation

just in time compilation
SMART_READER_LITE
LIVE PREVIEW

Just-In-Time Compilation Thiemo Bucciarelli Institute for Software - - PowerPoint PPT Presentation

Just-In-Time Compilation Thiemo Bucciarelli Institute for Software Engineering and Programming Languages 18. Januar 2016 T. Bucciarelli 18. Januar 2016 1/25 Agenda Definitions Just-In-Time Compilation Comparing JIT, AOT and Interpreters


slide-1
SLIDE 1

Just-In-Time Compilation

Thiemo Bucciarelli

Institute for Software Engineering and Programming Languages

  • 18. Januar 2016
  • T. Bucciarelli
  • 18. Januar 2016

1/25

slide-2
SLIDE 2

Agenda

Definitions Just-In-Time Compilation Comparing JIT, AOT and Interpreters Profiling Optimization The Java Virtual Machine Conclusion

  • T. Bucciarelli
  • 18. Januar 2016

2/25

slide-3
SLIDE 3

Definitions: Compiler Definition: Compiler

Translation of source code from one programming language to another.

◮ Lexical Analysis

Split the input into atomic units.

◮ Syntax Analysis

Check if the syntax is correct (parser).

◮ Semantic Analysis

E.g. type checking. Typically results in an intermediate representation.

◮ Optimization

Increase the effectivity of the code without changing the semantics.

  • T. Bucciarelli
  • 18. Januar 2016

3/25

slide-4
SLIDE 4

Definitions: AOT Compiler Definition: Ahead-of-Time (AOT) Compiler

◮ Compilation before runtime ◮ Typically called by the programmer ◮ Often produces native code for direct execution ◮ Mostly platform dependent!

  • T. Bucciarelli
  • 18. Januar 2016

4/25

slide-5
SLIDE 5

Definitions: Interpreters Definition: Interpreter

◮ Does not create any output ◮ Processes at runtime ◮ Directly executes the code on the processor (=interpreting) ◮ Mostly platform independent!

  • T. Bucciarelli
  • 18. Januar 2016

5/25

slide-6
SLIDE 6

Just-In-Time Compilation

Goals:

◮ Tries to fill the gap between Interpreter and AOT compilers ◮ Efficiency and platform independency

Structure:

◮ Performs compilation during runtime ◮ Trace-based or method-based ◮ Has to be as fast as possible ◮ Can use additional information for better optimizations ◮ Knows the underlying hardware

  • T. Bucciarelli
  • 18. Januar 2016

6/25

slide-7
SLIDE 7

Just-In-Time Compilation

Method-based:

◮ Only analyses the calls, no further analysis ◮ Similar to static AOT compilers ◮ Less efficient, but less overhead

Trace-based:

◮ Analyses what paths (traces) are often used, and to which methods they

belong

◮ Can effectively be combined with an interpreter ◮ Allows targeted optimizations ◮ More efficient, more overhead

  • T. Bucciarelli
  • 18. Januar 2016

7/25

slide-8
SLIDE 8

Comparing JIT, AOT and Interpreters

AOT-Compilers: + Can be arbitrary slow and thus perform time-consuming optimizations + No overhead at runtime − Little platform independence Interpreters: + Platform independence ± Little overhead at runtime − Inefficient for repetitive executions − Little optimization possibilities JIT-Compilers: + Platform independence + Highly optimized and efficient code + Runtime optimizations − High overhead at runtime

  • T. Bucciarelli
  • 18. Januar 2016

8/25

slide-9
SLIDE 9

Profiling

◮ Necessary for the optimization ◮ Analysis of the execution and collection of information ◮ A good profiling is crucial for efficient optimizations ◮ static or dynamic ◮ Has to produce as less overhead as possible (especially for JIT)

Next slides:

◮ How could a profiler be implemented? ◮ What information could be collected?

  • T. Bucciarelli
  • 18. Januar 2016

9/25

slide-10
SLIDE 10

Profiling: How?

Sampling-based:

◮ Statistical approach: Take samples of the execution state ◮ Less precise, but very efficient ◮ Does not affect memory management much ◮ Drawback: A method could be completely undetected

Event-based:

◮ Profiler is triggered by certain events ◮ Very specified

  • T. Bucciarelli
  • 18. Januar 2016

10/25

slide-11
SLIDE 11

Profiling: How?

Instrumentation:

◮ Injection of profiling code ◮ Very flexible, better performance than profiling by another thread

  • T. Bucciarelli
  • 18. Januar 2016

11/25

slide-12
SLIDE 12

Profiling: What?

Call graph:

◮ Graph showing the call-dependencies of the methods ◮ Edges contain the amount of times this call was performed

Execution Trace:

◮ A trace representing the execution, including timestamps. ◮ Most precise profile

  • T. Bucciarelli
  • 18. Januar 2016

12/25

slide-13
SLIDE 13

Optimization

Optimization:

◮ Static or dynamic/adaptive ◮ JIT compilers can use both types (but not every technique, because of

their overhead) Examples for static optimization:

◮ Dead code elimination ◮ Constant folding and propagation ◮ Loop-invariant code motion

Dynamic optimization on the basis of the Java Virtual Machine

  • T. Bucciarelli
  • 18. Januar 2016

13/25

slide-14
SLIDE 14

JVM: Introduction

◮ Here: HotSpot and JRockit ◮ AOT compilation of Java source-code to Java bytecode ◮ JRockit uses JIT and HotSpot uses JIT& Interpreter ◮ Bytecode is assembly-like, and easy to process during runtime

  • T. Bucciarelli
  • 18. Januar 2016

14/25

slide-15
SLIDE 15

JVM: Java Bytecode

What Java bytecode looks like

1 Compiled from

" t e s t . java "

2

public class t e s t {

3

public t e s t ( ) ;

4

Code :

5

0: aload_0

6

1: invokespecial #1 / / Method java / lang / Object ." < i n i t > " : ( ) V

7

4: return

8 9

public s t a t i c void main ( java . lang . String [ ] ) ;

10

Code :

11

0: iconst_5

12

1: istore_1

13

2: return

14

}

  • T. Bucciarelli
  • 18. Januar 2016

15/25

slide-16
SLIDE 16

JRockit structure

  • T. Bucciarelli
  • 18. Januar 2016

16/25

slide-17
SLIDE 17

JVM: HotSpot Optimizations

Hotspot Detection:

◮ Selective optimization: Only optimize parts of the code ◮ Assumption: the execution mostly resides in a small part of the code

(80/20 rule)

◮ For 80% of the code , interpreting is probably more efficient than

compiling

◮ Detect hot spots and focus on these for compilation and optimization

  • T. Bucciarelli
  • 18. Januar 2016

17/25

slide-18
SLIDE 18

JVM: HotSpot Optimizations

Method Inlining:

◮ Method invokations are time consuming ◮ Copy the code of frequently invokated methods inside their

caller-methods

◮ Reduces overhead, allows more optimization

Problems:

◮ Recursive calls (infinite inlining, the optimizer has to set a maximum for

the inlining of recursive calls)

◮ Overriding (see next slide)

  • T. Bucciarelli
  • 18. Januar 2016

18/25

slide-19
SLIDE 19

JVM: HotSpot Optimizations

Dynamic deoptimization:

◮ Not every method can be inlined ◮ If a method is not final, it could possibly be overridden at runtime ◮ HotSpot speculatively inline methods which are not final (but not

  • verridden at this moment)

◮ In certain cases, the inlining has to be undone, this is called dynamic

deoptimization

  • T. Bucciarelli
  • 18. Januar 2016

19/25

slide-20
SLIDE 20

JVM: Hotspot Optimizations

Range check elimination:

◮ Java requires strict array bounding checks ◮ Reading, changing and overwriting a value would need two checks ◮ the JVM can check if it is possible for the index value to change between

  • perations

◮ Reduces the range check amount

  • T. Bucciarelli
  • 18. Januar 2016

20/25

slide-21
SLIDE 21

JVM: When to use JIT?

◮ As stated in HotSpot Detection, interpreting is in some cases better than

JIT

◮ But: How to decide whether a method should be JIT-compiled or

interpreted?

◮ HotSpot uses heuristic approaches

  • T. Bucciarelli
  • 18. Januar 2016

21/25

slide-22
SLIDE 22

JVM: When to use JIT?

The simple heuristics

◮ Often executed methods should be compiled ◮ Large methods should also be compiled, even when they are rarely

executed

◮ Decision is based on the amount of executions and size of the methods

  • T. Bucciarelli
  • 18. Januar 2016

22/25

slide-23
SLIDE 23

Example

Example for a simple decision graph: don’t compile compile

JIT_MIN_SIZE JIT_MIN_TIMES

amount of executions method size

  • T. Bucciarelli
  • 18. Januar 2016

23/25

slide-24
SLIDE 24

Conclusion

◮ JIT compilation gives compilers the ability to be highly platform

independent

◮ Optimizations mostly based on assumptions, which could also have

negative effects

◮ Important decisions: Optimize? JIT or interpret?

  • T. Bucciarelli
  • 18. Januar 2016

24/25

slide-25
SLIDE 25

Questions?

Thank you for your attention. Questions?

  • T. Bucciarelli
  • 18. Januar 2016

25/25