One VM, Many Languages Brian Goetz Java Language Architect, Oracle - - PowerPoint PPT Presentation

one vm many languages
SMART_READER_LITE
LIVE PREVIEW

One VM, Many Languages Brian Goetz Java Language Architect, Oracle - - PowerPoint PPT Presentation

<Insert Picture Here> One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation Monday, October 4, 2010 The following is intended to outline our general product direction. It is intended for information purposes


slide-1
SLIDE 1

<Insert Picture Here>

One VM, Many Languages

Brian Goetz Java Language Architect, Oracle Corporation

Monday, October 4, 2010

slide-2
SLIDE 2 2

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any

  • contract. It is not a commitment to deliver any

material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

Monday, October 4, 2010

slide-3
SLIDE 3 3

Overview The Java Virtual Machine (JVM) has, in large part, been the engine behind the success of the Java programming language

  • The JVM is undergoing a transformation: to become a

Universal VM

  • In years to come, it will power the success of other

languages too

Monday, October 4, 2010

slide-4
SLIDE 4 4

“Java is slow because it runs on a VM”

  • Early

implementations of the JVM executed bytecode with an interpreter [slow]

Monday, October 4, 2010

slide-5
SLIDE 5 5

“Java is fast because it runs on a VM”

  • Major breakthrough was the

advent of “Just In Time” compilers [fast]

– Compile from bytecode to machine code at runtime – Optimize using information available at runtime only

  • Simplifies static compilers

– javac and ecj generate “dumb” bytecode and trust the JVM to

  • ptimize

– Optimization is real, but invisible

Monday, October 4, 2010

slide-6
SLIDE 6 6

Optimizations are universal

  • Optimizations work on bytecode in .class files
  • A compiler for any language – not just Java – can emit

a .class file

  • All languages can benefit from dynamic compilation

and optimizations like inlining

Monday, October 4, 2010

slide-7
SLIDE 7 7

HotSpot optimizations

compiler tactics delayed compilation Tiered compilation

  • n-stack replacement

delayed reoptimization program dependence graph representation static single assignment representation proof-based techniques exact type inference memory value inference memory value tracking constant folding reassociation

  • perator strength reduction

null check elimination type test strength reduction type test elimination algebraic simplification common subexpression elimination integer range typing flow-sensitive rewrites conditional constant propagation dominating test detection flow-carried type narrowing dead code elimination language-specific techniques class hierarchy analysis devirtualization symbolic constant propagation autobox elimination escape analysis lock elision lock fusion de-reflection speculative (profile-based) techniques

  • ptimistic nullness assertions
  • ptimistic type assertions
  • ptimistic type strengthening
  • ptimistic array length strengthening

untaken branch pruning

  • ptimistic N-morphic inlining

branch frequency prediction call frequency prediction memory and placement transformation expression hoisting expression sinking redundant store elimination adjacent store fusion card-mark elimination merge-point splitting loop transformations loop unrolling loop peeling safepoint elimination iteration range splitting range check elimination loop vectorization global code shaping inlining (graph integration) global code motion heat-based code layout switch balancing throw inlining control flow graph transformation local code scheduling local code bundling delay slot filling graph-coloring register allocation linear scan register allocation live range splitting copy coalescing constant splitting copy removal address mode matching instruction peepholing DFA-based code generator Monday, October 4, 2010

slide-8
SLIDE 8 8

Inlining is the uber-optimization

  • Speeding up method calls is the big win
  • For a given method call, try to predict which method

should be called

  • Numerous techniques available

– Devirtualization (Prove there's only one target method) – Monomorphic inline caching – Profile-driven inline caching

  • Goal is inlining: copying called method's body into

caller

– Gives more code for the optimizer to chew on

Monday, October 4, 2010

slide-9
SLIDE 9 9

public interface FooHolder<T> { public T getFoo(); } public class MyHolder<T> implements FooHolder<T> { private final T foo; public MyHolder(T foo) { this.foo = foo; } public T getFoo() { return foo; } }

Inlining: Example

Monday, October 4, 2010

slide-10
SLIDE 10 10

public interface FooHolder<T> { public T getFoo(); } public class MyHolder<T> implements FooHolder<T> { private final T foo; public MyHolder(T foo) { this.foo = foo; } public T getFoo() { return foo; } } ... public String getString(FooHolder<String> holder) { if (holder == null) throw new NullPointerException("You dummy."); else return holder.getFoo(); }

Inlining: Example

Monday, October 4, 2010

slide-11
SLIDE 11 11

public interface FooHolder<T> { public T getFoo(); } public class MyHolder<T> implements FooHolder<T> { private final T foo; public MyHolder(T foo) { this.foo = foo; } public T getFoo() { return foo; } } ... public String getString(FooHolder<String> holder) { if (holder == null) throw new NullPointerException("You dummy."); else return holder.getFoo(); } ... public String foo(String x) { FooHolder<String> myFooHolder = new MyHolder<String>(x); return getString(myFooHolder); }

Inlining: Example

Monday, October 4, 2010

slide-12
SLIDE 12 12

public interface FooHolder<T> { public T getFoo(); } public class MyHolder<T> implements FooHolder<T> { private final T foo; public MyHolder(T foo) { this.foo = foo; } public T getFoo() { return foo; } } ... public String getString(FooHolder<String> holder) { if (holder == null) throw new NullPointerException("You dummy."); else return holder.getFoo(); } ... public String foo(String x) { FooHolder<String> myFooHolder = new MyHolder<String>(x); return getString(myFooHolder); }

Inlining: Example

Step 1 Inline getString()

Monday, October 4, 2010

slide-13
SLIDE 13 13

public interface FooHolder<T> { public T getFoo(); } public class MyHolder<T> implements FooHolder<T> { private final T foo; public MyHolder(T foo) { this.foo = foo; } public T getFoo() { return foo; } } ... public String foo(String x) { FooHolder<String> myFooHolder = new MyHolder<String>(x); if (myFooHolder == null) throw new NullPointerException("You dummy."); else return myFooHolder.getFoo(); }

Inlining: Example

Monday, October 4, 2010

slide-14
SLIDE 14 14

public interface FooHolder<T> { public T getFoo(); } public class MyHolder<T> implements FooHolder<T> { private final T foo; public MyHolder(T foo) { this.foo = foo; } public T getFoo() { return foo; } } ... public String foo(String x) { FooHolder<String> myFooHolder = new MyHolder<String>(x); if (myFooHolder == null) throw new NullPointerException("You dummy."); else return myFooHolder.getFoo(); }

Inlining: Example

Step 2 Dead code

Monday, October 4, 2010

slide-15
SLIDE 15 15

public interface FooHolder<T> { public T getFoo(); } public class MyHolder<T> implements FooHolder<T> { private final T foo; public MyHolder(T foo) { this.foo = foo; } public T getFoo() { return foo; } } ... public String foo(String x) { FooHolder<String> myFooHolder = new MyHolder<String>(x); if (myFooHolder == null) throw new NullPointerException("You dummy."); else return myFooHolder.getFoo(); }

Inlining: Example

Monday, October 4, 2010

slide-16
SLIDE 16 16

public interface FooHolder<T> { public T getFoo(); } public class MyHolder<T> implements FooHolder<T> { private final T foo; public MyHolder(T foo) { this.foo = foo; } public T getFoo() { return foo; } } ... public String foo(String x) { FooHolder<String> myFooHolder = new MyHolder<String>(x); return myFooHolder.getFoo(); }

Inlining: Example

Step 3 Type sharpen and inlining

Monday, October 4, 2010

slide-17
SLIDE 17 17

public interface FooHolder<T> { public T getFoo(); } public class MyHolder<T> implements FooHolder<T> { private final T foo; public MyHolder(T foo) { this.foo = foo; } public T getFoo() { return foo; } } ... public String foo(String x) { FooHolder<String> myFooHolder = new MyHolder<String>(x); return myFooHolder.foo; }

Inlining: Example

Monday, October 4, 2010

slide-18
SLIDE 18 18

public interface FooHolder<T> { public T getFoo(); } public class MyHolder<T> implements FooHolder<T> { private final T foo; public MyHolder(T foo) { this.foo = foo; } public T getFoo() { return foo; } } ... public String foo(String x) { FooHolder<String> myFooHolder = new MyHolder<String>(x); return myFooHolder.foo; }

Inlining: Example

Step 4 Escape analysis

Monday, October 4, 2010

slide-19
SLIDE 19 19

public interface FooHolder<T> { public T getFoo(); } public class MyHolder<T> implements FooHolder<T> { private final T foo; public MyHolder(T foo) { this.foo = foo; } public T getFoo() { return foo; } } ... public String foo(String x) { return x; }

Inlining: Example

Monday, October 4, 2010

slide-20
SLIDE 20 20

Inlining is the uber-optimization

  • Each time we inlined, we exposed information from

the outer scope

  • Which could be used to optimize the inner scope

further, now that there is more information available

  • Code often gets smaller and faster at the same time
  • HotSpot works hard to inline everything it can
  • Will apply “inline caching” when it can't predict inlining

perfectly

  • Will inline speculatively based on current loaded class

hierarchy

Monday, October 4, 2010

slide-21
SLIDE 21 21

Languages ♥ Virtual Machines

  • Programming languages need runtime support

– Memory management / Garbage collection – Concurrency control – Security – Reflection – Debugging / Profiling – Standard libraries (collections, database, XML, etc)

  • Traditionally, language implementers coded these

features themselves

  • Many implementers now choose to target a VM to

reuse infrastructure

Monday, October 4, 2010

slide-22
SLIDE 22 22

The Great Ruby Shootout 2008

2.00 means “twice as fast” 0.50 means “half the speed” http://antoniocangiano.com/2008/12/09/the-great-ruby-shootout- december-2008/

Monday, October 4, 2010

slide-23
SLIDE 23 23

Benefits for the developer

  • Choice

– Use the right tool for the right job, while sharing infrastructure – Unit tests in Scala, Business logic in Java, Web app in JRuby, Config scripts in Jython... – ...with the same IDE, same debugger, same JVM

  • Extensibility

– Extend a Java application with a Groovy plugin

  • Manageability

– Run RubyOnRails with JRuby on a managed JVM

Monday, October 4, 2010

slide-24
SLIDE 24 24

Trends in programming languages

Monday, October 4, 2010

slide-25
SLIDE 25 25

Different kinds of languages

Monday, October 4, 2010

slide-26
SLIDE 26 26

Fibonacci in Java and Ruby

int fib(int n) { if (n<2) return n; else return fib(n-1)+fib (n-2); } def fib(n) { if n<2 n else fib(n-1)+fib(n-2) end }

Monday, October 4, 2010

slide-27
SLIDE 27 27

Not as similar as they look

  • Data types

– Not just char/int/long/double and java.lang.Object

  • Method call

– Not just Java-style overloading and overriding

  • Control structures

– Not just 'for', 'while', 'break', 'continue'

  • Collections

– Not just java.util.*

Monday, October 4, 2010

slide-28
SLIDE 28 28

Reality is a simulation

Primitive types+ops Object model Memory model Dynamic linking Access control GC Unicode Checked exceptions Generics Enums Overloading Constructor chaining Program analysis Primitive types+ops Object model Memory model Dynamic linking Access control GC Unicode

Java language fictions Java VM features

Open classes Dynamic typing 'eval' Closures Mixins Regular expressions Primitive types+ops Object model Memory model Dynamic linking Access control GC Unicode

Ruby language fictions

Monday, October 4, 2010

slide-29
SLIDE 29 29

Towards a Universal VM

  • Simulating language features at runtime is slow
  • When multiple languages target a VM, common

issues quickly become apparent

  • With expertise and taste, the JVM can grow to benefit

all languages

– Adding a little more gains us a lot! – Each additional “stretch” helps many more languages

Monday, October 4, 2010

slide-30
SLIDE 30 30

Java VM Specification, 1997

  • The Java Virtual Machine knows nothing about the Java

programming language, only of a particular binary format, the class file format.

  • A class file contains Java Virtual Machine instructions (or

bytecodes) and a symbol table, as well as other ancillary information.

  • Any language with functionality that can be expressed in terms
  • f a valid class file can be hosted by the Java virtual machine.
  • Attracted by a generally available, machine-independent

platform, implementors of other languages are turning to the Java Virtual Machine as a delivery vehicle for their languages.

  • In the future, we will consider bounded extensions to the Java

Virtual Machine to provide better support for other languages.

Monday, October 4, 2010

slide-31
SLIDE 31 31

JVM extensions for other languages

Monday, October 4, 2010

slide-32
SLIDE 32 31

JVM extensions for other languages

  • There’s no shortage of JVM feature suggestions

Monday, October 4, 2010

slide-33
SLIDE 33 31

JVM extensions for other languages

  • There’s no shortage of JVM feature suggestions

– Dynamic method linkage (non-Java method lookup)

Monday, October 4, 2010

slide-34
SLIDE 34 31

JVM extensions for other languages

  • There’s no shortage of JVM feature suggestions

– Dynamic method linkage (non-Java method lookup) – Tail calls (more dynamic control flow)

Monday, October 4, 2010

slide-35
SLIDE 35 31

JVM extensions for other languages

  • There’s no shortage of JVM feature suggestions

– Dynamic method linkage (non-Java method lookup) – Tail calls (more dynamic control flow) – Continuations (fibers vs. threads, mobile vs. bound, …)

Monday, October 4, 2010

slide-36
SLIDE 36 31

JVM extensions for other languages

  • There’s no shortage of JVM feature suggestions

– Dynamic method linkage (non-Java method lookup) – Tail calls (more dynamic control flow) – Continuations (fibers vs. threads, mobile vs. bound, …) – Tuples (a.k.a. value types, structs)

Monday, October 4, 2010

slide-37
SLIDE 37 31

JVM extensions for other languages

  • There’s no shortage of JVM feature suggestions

– Dynamic method linkage (non-Java method lookup) – Tail calls (more dynamic control flow) – Continuations (fibers vs. threads, mobile vs. bound, …) – Tuples (a.k.a. value types, structs) – Open classes (e.g., for “monkey patching”)

Monday, October 4, 2010

slide-38
SLIDE 38 31

JVM extensions for other languages

  • There’s no shortage of JVM feature suggestions

– Dynamic method linkage (non-Java method lookup) – Tail calls (more dynamic control flow) – Continuations (fibers vs. threads, mobile vs. bound, …) – Tuples (a.k.a. value types, structs) – Open classes (e.g., for “monkey patching”) – Interface injection (making new views of old types)

Monday, October 4, 2010

slide-39
SLIDE 39 31

JVM extensions for other languages

  • There’s no shortage of JVM feature suggestions

– Dynamic method linkage (non-Java method lookup) – Tail calls (more dynamic control flow) – Continuations (fibers vs. threads, mobile vs. bound, …) – Tuples (a.k.a. value types, structs) – Open classes (e.g., for “monkey patching”) – Interface injection (making new views of old types) – Tagged fixnums (autoboxing without tears)

Monday, October 4, 2010

slide-40
SLIDE 40 32

Monday, October 4, 2010

slide-41
SLIDE 41 32

If we could make one change to the JVM to improve life for dynamic languages, what would it be?

Monday, October 4, 2010

slide-42
SLIDE 42 32

If we could make one change to the JVM to improve life for dynamic languages, what would it be? More flexible method calls

Monday, October 4, 2010

slide-43
SLIDE 43 33

More flexible method calls

Monday, October 4, 2010

slide-44
SLIDE 44 33

More flexible method calls

  • The invokevirtual bytecode performs a method call

Monday, October 4, 2010

slide-45
SLIDE 45 33

More flexible method calls

  • The invokevirtual bytecode performs a method call
  • Its behavior is Java-like and fixed

Monday, October 4, 2010

slide-46
SLIDE 46 33

More flexible method calls

  • The invokevirtual bytecode performs a method call
  • Its behavior is Java-like and fixed
  • Other languages need custom behavior

Monday, October 4, 2010

slide-47
SLIDE 47 33

More flexible method calls

  • The invokevirtual bytecode performs a method call
  • Its behavior is Java-like and fixed
  • Other languages need custom behavior
  • Idea: let some “language logic” determine the

behavior of a JVM method call

Monday, October 4, 2010

slide-48
SLIDE 48 33

More flexible method calls

  • The invokevirtual bytecode performs a method call
  • Its behavior is Java-like and fixed
  • Other languages need custom behavior
  • Idea: let some “language logic” determine the

behavior of a JVM method call

  • Invention: the invokedynamic bytecode

– VM asks some “language logic” how to call a method – Language logic gives an answer, and decides if it needs to stay in the loop

Monday, October 4, 2010

slide-49
SLIDE 49 34

Caller Method invokevirtual

Virtual method call in Java

Monday, October 4, 2010

slide-50
SLIDE 50 35

Caller Method

Dynamic method call

invokedynamic

Monday, October 4, 2010

slide-51
SLIDE 51 35

Caller Method Language logic

Dynamic method call

invokedynamic

Monday, October 4, 2010

slide-52
SLIDE 52 35

Caller Method Language logic invokevirtual

Dynamic method call

invokedynamic

Monday, October 4, 2010

slide-53
SLIDE 53 35

Caller Method Language logic invokevirtual

Check which methods are available now in each class [open classes] Check the dynamic types of arguments to the method [multimethods] Rearrange and inject arguments [optional and default parameters] Convert numbers to a different representation [fixnums] Dynamic method call

invokedynamic

Monday, October 4, 2010

slide-54
SLIDE 54 36

JRuby caller Method JRuby logic invokedynamic invokevirtual Jython logic Groovy logic Jython caller Groovy caller

Monday, October 4, 2010

slide-55
SLIDE 55 37

Language logic is only needed...

*†‡

Monday, October 4, 2010

slide-56
SLIDE 56 37

Language logic is only needed...

*†‡

Monday, October 4, 2010

slide-57
SLIDE 57 37

Language logic is only needed...

ONCE

*†‡

Monday, October 4, 2010

slide-58
SLIDE 58 37

Language logic is only needed...

ONCE

*†‡

Monday, October 4, 2010

slide-59
SLIDE 59 37

Language logic is only needed...

ONCE

* Until a different object is assigned to the receiver variable

† Until the receiver's dynamic type is changed ‡ Until the arguments' dynamic types are changed

*†‡

Monday, October 4, 2010

slide-60
SLIDE 60 38

The deal with method calls (in one slide)

4

Monday, October 4, 2010

slide-61
SLIDE 61 38

The deal with method calls (in one slide)

  • Calling a method is cheap (VMs can even inline!)

4

Monday, October 4, 2010

slide-62
SLIDE 62 38

The deal with method calls (in one slide)

  • Calling a method is cheap (VMs can even inline!)
  • Selecting the right target method can be costly

– Static languages do most of their method selection at compile time (e.g., System.out.println(x)) Single-dispatch on receiver type is left for runtime – Dynamic languages do almost none at compile-time Don’t re-do method selection for every single invocation! 4

Monday, October 4, 2010

slide-63
SLIDE 63 38

The deal with method calls (in one slide)

  • Calling a method is cheap (VMs can even inline!)
  • Selecting the right target method can be costly

– Static languages do most of their method selection at compile time (e.g., System.out.println(x)) Single-dispatch on receiver type is left for runtime – Dynamic languages do almost none at compile-time Don’t re-do method selection for every single invocation!

  • Each language has its own ideas about linkage

– The VM enforces static rules of naming and linkage Language runtimes want to decide (& re-decide) linkage 4

Monday, October 4, 2010

slide-64
SLIDE 64 39

What’s in a method call? A sequence of tasks

5

Monday, October 4, 2010

slide-65
SLIDE 65 39

What’s in a method call? A sequence of tasks

  • Naming — using a symbolic name

5

Monday, October 4, 2010

slide-66
SLIDE 66 39

What’s in a method call? A sequence of tasks

  • Naming — using a symbolic name
  • Selecting — deciding which one to call

5

Monday, October 4, 2010

slide-67
SLIDE 67 39

What’s in a method call? A sequence of tasks

  • Naming — using a symbolic name
  • Selecting — deciding which one to call
  • Adapting — agreeing on calling conventions

5

Monday, October 4, 2010

slide-68
SLIDE 68 39

What’s in a method call? A sequence of tasks

  • Naming — using a symbolic name
  • Selecting — deciding which one to call
  • Adapting — agreeing on calling conventions
  • Calling – finally, a parameterized control transfer

5

Monday, October 4, 2010

slide-69
SLIDE 69 40

What’s in a method call? Connection from A to B

6

Monday, October 4, 2010

slide-70
SLIDE 70 40

What’s in a method call? Connection from A to B

  • Including naming, linking, selecting, adapting:

6

Monday, October 4, 2010

slide-71
SLIDE 71 40

What’s in a method call? Connection from A to B

  • Including naming, linking, selecting, adapting:
  • …callee B might be known to caller A only by a name

6

Monday, October 4, 2010

slide-72
SLIDE 72 40

What’s in a method call? Connection from A to B

  • Including naming, linking, selecting, adapting:
  • …callee B might be known to caller A only by a name
  • …and A and B might be far apart

6

Monday, October 4, 2010

slide-73
SLIDE 73 40

What’s in a method call? Connection from A to B

  • Including naming, linking, selecting, adapting:
  • …callee B might be known to caller A only by a name
  • …and A and B might be far apart
  • …and B might depend on arguments passed by A

6

Monday, October 4, 2010

slide-74
SLIDE 74 40

What’s in a method call? Connection from A to B

  • Including naming, linking, selecting, adapting:
  • …callee B might be known to caller A only by a name
  • …and A and B might be far apart
  • …and B might depend on arguments passed by A
  • …and a correct call to B might require adaptations

6

Monday, October 4, 2010

slide-75
SLIDE 75 40

What’s in a method call? Connection from A to B

  • Including naming, linking, selecting, adapting:
  • …callee B might be known to caller A only by a name
  • …and A and B might be far apart
  • …and B might depend on arguments passed by A
  • …and a correct call to B might require adaptations
  • After everything is decided, A jumps to B’s code.

6

Monday, October 4, 2010

slide-76
SLIDE 76 41

What’s in a method call? Several phases

  • Source code: What the language says
  • Bytecode: What’s (statically) in the classfile

Monday, October 4, 2010

slide-77
SLIDE 77 41

What’s in a method call? Several phases

  • Source code: What the language says
  • Bytecode: What’s (statically) in the classfile
  • Linking: One-time setup done by the JVM

Monday, October 4, 2010

slide-78
SLIDE 78 41

What’s in a method call? Several phases

  • Source code: What the language says
  • Bytecode: What’s (statically) in the classfile
  • Linking: One-time setup done by the JVM
  • Executing: What happens on every call

Monday, October 4, 2010

slide-79
SLIDE 79 42

Phases versus tasks (before invokedynamic)

Source code Bytecode Linking Executing Naming Selecting Adapting Calling Identifiers Utf8 constants JVM “dictionary” Scopes Class names Loaded classes V-table lookup Argument conversion C2I / I2C adapters Receiver narrowing Jump with arguments

Monday, October 4, 2010

slide-80
SLIDE 80 42

Phases versus tasks (before invokedynamic)

Source code Bytecode Linking Executing Naming Selecting Adapting Calling Identifiers Utf8 constants JVM “dictionary” Scopes Class names Loaded classes V-table lookup Argument conversion C2I / I2C adapters Receiver narrowing Jump with arguments

Monday, October 4, 2010

slide-81
SLIDE 81 43

Invokedynamic removes some limits

Monday, October 4, 2010

slide-82
SLIDE 82 43

Invokedynamic removes some limits

  • Method naming is not limited to Java APIs

Monday, October 4, 2010

slide-83
SLIDE 83 43

Invokedynamic removes some limits

  • Method naming is not limited to Java APIs
  • Method lookup is not limited to class scopes

– Completely generalized via Bootstrap Methods

Monday, October 4, 2010

slide-84
SLIDE 84 43

Invokedynamic removes some limits

  • Method naming is not limited to Java APIs
  • Method lookup is not limited to class scopes

– Completely generalized via Bootstrap Methods

  • Invocation targets can be mixed and matched

– Adapter method handles can transform arguments – Bound method handles can close over “live” data

Monday, October 4, 2010

slide-85
SLIDE 85 44

Phases versus tasks (with invokedynamic)

Source code Bytecode Linking Executing Naming Selecting Adapting Calling

∞ ∞ ∞ ∞ ∞

Bootstrap methods Bootstrap method call

∞ ∞

Method handles

Jump with arguments

Monday, October 4, 2010

slide-86
SLIDE 86 44

Phases versus tasks (with invokedynamic)

Source code Bytecode Linking Executing Naming Selecting Adapting Calling

∞ ∞ ∞ ∞ ∞

Bootstrap methods Bootstrap method call

∞ ∞

Method handles

Jump with arguments

Monday, October 4, 2010

slide-87
SLIDE 87 45

Phases versus tasks (before invokedynamic)

Monday, October 4, 2010

slide-88
SLIDE 88 46

Phases versus tasks (after invokedynamic)

Monday, October 4, 2010

slide-89
SLIDE 89 47

Method handles and closures

Monday, October 4, 2010

slide-90
SLIDE 90 47

Method handles and closures

  • We are working on closures in Java

– More flexible, less bulky than anonymous inner classes

Monday, October 4, 2010

slide-91
SLIDE 91 47

Method handles and closures

  • We are working on closures in Java

– More flexible, less bulky than anonymous inner classes

  • What’s in a closure?

– A small bit of code specified in an expression – Optionally, some data associated with it at creation – A target (SAM) type specifying how the closure will be used

Monday, October 4, 2010

slide-92
SLIDE 92 47

Method handles and closures

  • We are working on closures in Java

– More flexible, less bulky than anonymous inner classes

  • What’s in a closure?

– A small bit of code specified in an expression – Optionally, some data associated with it at creation – A target (SAM) type specifying how the closure will be used

  • What does the JVM see?

– A method handle constant specifying the raw behavior (Typically a synthetic private, but may be any method.) – Optionally, a “bind” operation on the method handle – A “SAM conversion” operation to convert to the target type

Monday, October 4, 2010

slide-93
SLIDE 93 48

Invokedynamic and closures?

Monday, October 4, 2010

slide-94
SLIDE 94 48

Invokedynamic and closures?

  • An instructive possibility...

Monday, October 4, 2010

slide-95
SLIDE 95 48

Invokedynamic and closures?

  • An instructive possibility...
  • 1. Compile the data type and target types as Bootstrap

Method parameters.

Monday, October 4, 2010

slide-96
SLIDE 96 48

Invokedynamic and closures?

  • An instructive possibility...
  • 1. Compile the data type and target types as Bootstrap

Method parameters.

  • 2. When the call is linked, a runtime library selects an

efficient representation.

Monday, October 4, 2010

slide-97
SLIDE 97 48

Invokedynamic and closures?

  • An instructive possibility...
  • 1. Compile the data type and target types as Bootstrap

Method parameters.

  • 2. When the call is linked, a runtime library selects an

efficient representation.

  • 3. The call is bound to a method handle which creates

the needed closure.

Monday, October 4, 2010

slide-98
SLIDE 98 48

Invokedynamic and closures?

  • An instructive possibility...
  • 1. Compile the data type and target types as Bootstrap

Method parameters.

  • 2. When the call is linked, a runtime library selects an

efficient representation.

  • 3. The call is bound to a method handle which creates

the needed closure.

  • 4. When the call is executed, data parameters (if any)

are passed on the stack.

Monday, October 4, 2010

slide-99
SLIDE 99 48

Invokedynamic and closures?

  • An instructive possibility...
  • 1. Compile the data type and target types as Bootstrap

Method parameters.

  • 2. When the call is linked, a runtime library selects an

efficient representation.

  • 3. The call is bound to a method handle which creates

the needed closure.

  • 4. When the call is executed, data parameters (if any)

are passed on the stack.

  • 5. The method handle folds it all together, optimally.

Monday, October 4, 2010

slide-100
SLIDE 100 49

JSR 292 design news

Monday, October 4, 2010

slide-101
SLIDE 101 49

JSR 292 design news

  • Method handle constants

– Allows bytecode to work with method handles, as directly as it works with methods themselves – Initially motivated by thinking about closure compilation

Monday, October 4, 2010

slide-102
SLIDE 102 49

JSR 292 design news

  • Method handle constants

– Allows bytecode to work with method handles, as directly as it works with methods themselves – Initially motivated by thinking about closure compilation

  • Bootstrap Methods localized to invokedynamic calls

– Allows dynamic call sites to be woven independently

Monday, October 4, 2010

slide-103
SLIDE 103 49

JSR 292 design news

  • Method handle constants

– Allows bytecode to work with method handles, as directly as it works with methods themselves – Initially motivated by thinking about closure compilation

  • Bootstrap Methods localized to invokedynamic calls

– Allows dynamic call sites to be woven independently

  • Class-specific values (for metaclass caching)

– ThreadLocal : Threads :: ClassValue : Class

Monday, October 4, 2010

slide-104
SLIDE 104 49

JSR 292 design news

  • Method handle constants

– Allows bytecode to work with method handles, as directly as it works with methods themselves – Initially motivated by thinking about closure compilation

  • Bootstrap Methods localized to invokedynamic calls

– Allows dynamic call sites to be woven independently

  • Class-specific values (for metaclass caching)

– ThreadLocal : Threads :: ClassValue : Class

  • “Live” constants

– Generalization of Class and Method Handle constants – Linked into the constant pool by a user-specified BSM

Monday, October 4, 2010

slide-105
SLIDE 105 50

What’s next? A standard

Monday, October 4, 2010

slide-106
SLIDE 106 50

What’s next? A standard

  • Reference Implementation driven as part of JDK 7

Monday, October 4, 2010

slide-107
SLIDE 107 50

What’s next? A standard

  • Reference Implementation driven as part of JDK 7
  • Experiments have been done with it:

– JRuby retargeting (Charlie Nutter) – Rhino (JavaScript) investigation – “PHP Reboot” project (Rémi Forax)

Monday, October 4, 2010

slide-108
SLIDE 108 50

What’s next? A standard

  • Reference Implementation driven as part of JDK 7
  • Experiments have been done with it:

– JRuby retargeting (Charlie Nutter) – Rhino (JavaScript) investigation – “PHP Reboot” project (Rémi Forax)

  • Expert Group has been actively discussing the spec.

Monday, October 4, 2010

slide-109
SLIDE 109 50

What’s next? A standard

  • Reference Implementation driven as part of JDK 7
  • Experiments have been done with it:

– JRuby retargeting (Charlie Nutter) – Rhino (JavaScript) investigation – “PHP Reboot” project (Rémi Forax)

  • Expert Group has been actively discussing the spec.
  • Nearing a second draft specification (this year)

Monday, October 4, 2010

slide-110
SLIDE 110 51

What’s next? Da Vinci projects

Monday, October 4, 2010

slide-111
SLIDE 111 51

What’s next? Da Vinci projects

  • The Da Vinci Machine Project continues

Monday, October 4, 2010

slide-112
SLIDE 112 51

What’s next? Da Vinci projects

  • The Da Vinci Machine Project continues
  • Community contributions:

– Continuations – Coroutines – Hotswap – Tailcalls – Interface injection

Monday, October 4, 2010

slide-113
SLIDE 113 51

What’s next? Da Vinci projects

  • The Da Vinci Machine Project continues
  • Community contributions:

– Continuations – Coroutines – Hotswap – Tailcalls – Interface injection

  • Gleams in our eyes:

– Object “species” (for splitting classes more finely) – Tuples and value types (for using registers more efficiently) – Advanced array types (for using memory more efficiently)

Monday, October 4, 2010

slide-114
SLIDE 114 52

What’s next? All of the above, fast and light

Monday, October 4, 2010

slide-115
SLIDE 115 52

What’s next? All of the above, fast and light

  • Architecture ≠ optimization

Monday, October 4, 2010

slide-116
SLIDE 116 52

What’s next? All of the above, fast and light

  • Architecture ≠ optimization
  • Architecture → enables optimization

Monday, October 4, 2010

slide-117
SLIDE 117 52

What’s next? All of the above, fast and light

  • Architecture ≠ optimization
  • Architecture → enables optimization
  • Efficient method handle creation

Monday, October 4, 2010

slide-118
SLIDE 118 52

What’s next? All of the above, fast and light

  • Architecture ≠ optimization
  • Architecture → enables optimization
  • Efficient method handle creation
  • Compact representations (fused MH/SAM nodes)

Monday, October 4, 2010

slide-119
SLIDE 119 52

What’s next? All of the above, fast and light

  • Architecture ≠ optimization
  • Architecture → enables optimization
  • Efficient method handle creation
  • Compact representations (fused MH/SAM nodes)
  • Memory-less representations

Monday, October 4, 2010

slide-120
SLIDE 120 53

“Fixnums” – tagged immediate pseudo-pointers

Monday, October 4, 2010

slide-121
SLIDE 121 53

“Fixnums” – tagged immediate pseudo-pointers

  • In Java, primitives can be “autoboxed”

– This convenience was added in JDK 5

Monday, October 4, 2010

slide-122
SLIDE 122 53

“Fixnums” – tagged immediate pseudo-pointers

  • In Java, primitives can be “autoboxed”

– This convenience was added in JDK 5

  • Boxing is expensive and tricky to optimize

– In general it requires building a whole “wrapper” object

Monday, October 4, 2010

slide-123
SLIDE 123 53

“Fixnums” – tagged immediate pseudo-pointers

  • In Java, primitives can be “autoboxed”

– This convenience was added in JDK 5

  • Boxing is expensive and tricky to optimize

– In general it requires building a whole “wrapper” object

  • Some older systems (Lisp, Smalltalk) are smarter

– They use the object pointer itself to store the primitive value – The pointer is “tagged” to distinguish it from a real address

Monday, October 4, 2010

slide-124
SLIDE 124 54

A list of integer values (before fixnums)

Monday, October 4, 2010

slide-125
SLIDE 125 55

A list of integer values (after fixnums)

Monday, October 4, 2010

slide-126
SLIDE 126 56

Fixnums in the Java VM

Monday, October 4, 2010

slide-127
SLIDE 127 56

Fixnums in the Java VM

  • The JVM can also do the “fixnum” trick

Monday, October 4, 2010

slide-128
SLIDE 128 56

Fixnums in the Java VM

  • The JVM can also do the “fixnum” trick
  • This will make Integer / int conversions very cheap

Monday, October 4, 2010

slide-129
SLIDE 129 56

Fixnums in the Java VM

  • The JVM can also do the “fixnum” trick
  • This will make Integer / int conversions very cheap
  • No need for special “int” container types

– Filter, Predicate vs. intFilter, intPredicate, etc.

Monday, October 4, 2010

slide-130
SLIDE 130 56

Fixnums in the Java VM

  • The JVM can also do the “fixnum” trick
  • This will make Integer / int conversions very cheap
  • No need for special “int” container types

– Filter, Predicate vs. intFilter, intPredicate, etc.

  • One catch: Doesn’t work well for “double” values

Monday, October 4, 2010

slide-131
SLIDE 131 57

Implications for languages

Monday, October 4, 2010

slide-132
SLIDE 132 57

Implications for languages

  • Bootstrap Methods — new link-time hook

– helps with call site management (JRuby, JavaScript) – can help with one-time representation setup (closures)

Monday, October 4, 2010

slide-133
SLIDE 133 57

Implications for languages

  • Bootstrap Methods — new link-time hook

– helps with call site management (JRuby, JavaScript) – can help with one-time representation setup (closures)

  • Method Handles — lower-level access to methods

– faster and more direct than reflection – more compact than inner classes

Monday, October 4, 2010

slide-134
SLIDE 134 57

Implications for languages

  • Bootstrap Methods — new link-time hook

– helps with call site management (JRuby, JavaScript) – can help with one-time representation setup (closures)

  • Method Handles — lower-level access to methods

– faster and more direct than reflection – more compact than inner classes

  • SAM conversion — bridge to higher-level APIs

– no more spinning of 1000’s of tiny inner classes (Scala)

Monday, October 4, 2010

slide-135
SLIDE 135 57

Implications for languages

  • Bootstrap Methods — new link-time hook

– helps with call site management (JRuby, JavaScript) – can help with one-time representation setup (closures)

  • Method Handles — lower-level access to methods

– faster and more direct than reflection – more compact than inner classes

  • SAM conversion — bridge to higher-level APIs

– no more spinning of 1000’s of tiny inner classes (Scala)

  • Class values — direct annotation of arb. classes

– no more fumbling with class-keyed hash tables (Groovy)

Monday, October 4, 2010

slide-136
SLIDE 136 57

Implications for languages

  • Bootstrap Methods — new link-time hook

– helps with call site management (JRuby, JavaScript) – can help with one-time representation setup (closures)

  • Method Handles — lower-level access to methods

– faster and more direct than reflection – more compact than inner classes

  • SAM conversion — bridge to higher-level APIs

– no more spinning of 1000’s of tiny inner classes (Scala)

  • Class values — direct annotation of arb. classes

– no more fumbling with class-keyed hash tables (Groovy)

  • Fixnums — Less pain dealing with primitives

Monday, October 4, 2010

slide-137
SLIDE 137 58

To find out more...

Monday, October 4, 2010

slide-138
SLIDE 138 58

To find out more...

  • “Bytecodes meet Combinators: invokedynamic on the

JVM”, Rose VMIL 2009

http://blogs.sun.com/jrose/entry/vmil_paper_on_invokedynamic

Monday, October 4, 2010

slide-139
SLIDE 139 58

To find out more...

  • “Bytecodes meet Combinators: invokedynamic on the

JVM”, Rose VMIL 2009

http://blogs.sun.com/jrose/entry/vmil_paper_on_invokedynamic

  • “Optimizing Invokedynamic”, Thalinger PPPJ 2010

http://blogs.sun.com/jrose/entry/an_experiment_with_generic_arithmetic

Monday, October 4, 2010

slide-140
SLIDE 140 58

To find out more...

  • “Bytecodes meet Combinators: invokedynamic on the

JVM”, Rose VMIL 2009

http://blogs.sun.com/jrose/entry/vmil_paper_on_invokedynamic

  • “Optimizing Invokedynamic”, Thalinger PPPJ 2010

http://blogs.sun.com/jrose/entry/an_experiment_with_generic_arithmetic

  • JVM Language Summit 2010

http://wiki.jvmlangsummit.com

Monday, October 4, 2010

slide-141
SLIDE 141 58

To find out more...

  • “Bytecodes meet Combinators: invokedynamic on the

JVM”, Rose VMIL 2009

http://blogs.sun.com/jrose/entry/vmil_paper_on_invokedynamic

  • “Optimizing Invokedynamic”, Thalinger PPPJ 2010

http://blogs.sun.com/jrose/entry/an_experiment_with_generic_arithmetic

  • JVM Language Summit 2010

http://wiki.jvmlangsummit.com

  • Da Vinci Machine Project

http://openjdk.java.net/projects/mlvm/

Monday, October 4, 2010

slide-142
SLIDE 142 58

To find out more...

  • “Bytecodes meet Combinators: invokedynamic on the

JVM”, Rose VMIL 2009

http://blogs.sun.com/jrose/entry/vmil_paper_on_invokedynamic

  • “Optimizing Invokedynamic”, Thalinger PPPJ 2010

http://blogs.sun.com/jrose/entry/an_experiment_with_generic_arithmetic

  • JVM Language Summit 2010

http://wiki.jvmlangsummit.com

  • Da Vinci Machine Project

http://openjdk.java.net/projects/mlvm/

  • Friday 9/25 bonus: http://scalaliftoff.com/

(discount code = scalaone)

Monday, October 4, 2010

slide-143
SLIDE 143 59

Monday, October 4, 2010