one vm, many languages - goto bloggotocon.com/.../slides/briangoetz_onevmmanylanguages.pdf ·...

143
<Insert Picture Here> One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation Monday, October 4, 2010

Upload: others

Post on 21-May-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

<Insert Picture Here>

One VM, Many LanguagesBrian Goetz Java Language Architect, Oracle Corporation

Monday, October 4, 2010

Page 2: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 3: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 4: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

4

“Java is slow because it runs on a VM”• Early

implementations of theJVM executed bytecode with an interpreter [slow]

Monday, October 4, 2010

Page 5: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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 optimize

– Optimization is real, but invisible

Monday, October 4, 2010

Page 6: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 7: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

7

HotSpot optimizations

compiler tactics delayed compilation Tiered compilation on-stack replacement delayed reoptimization program dependence graph representation static single assignment representationproof-based techniques exact type inference memory value inference memory value tracking constant folding reassociation operator strength reduction null check elimination type test strength reduction type test elimination algebraic simplification common subexpression elimination integer range typingflow-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 optimistic nullness assertions optimistic type assertions optimistic type strengthening optimistic array length strengthening untaken branch pruning optimistic N-morphic inlining branch frequency prediction call frequency predictionmemory 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 vectorizationglobal code shaping inlining (graph integration) global code motion heat-based code layout switch balancing throw inliningcontrol 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

Page 8: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 9: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 10: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 11: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 12: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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 1Inline getString()

Monday, October 4, 2010

Page 13: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 14: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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 2Dead code

Monday, October 4, 2010

Page 15: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 16: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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: ExampleStep 3

Type sharpen and inlining

Monday, October 4, 2010

Page 17: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 18: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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 4Escape analysis

Monday, October 4, 2010

Page 19: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 20: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 21: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 22: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 23: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 24: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

24

Trends in programming languages

Monday, October 4, 2010

Page 25: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

25

Different kinds of languages

Monday, October 4, 2010

Page 26: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 27: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 28: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

28

Reality is a simulation

Primitive types+opsObject model

Memory modelDynamic linkingAccess control

GCUnicode

Checked exceptionsGenericsEnums

OverloadingConstructor chaining

Program analysisPrimitive types+ops

Object modelMemory modelDynamic linkingAccess control

GCUnicode

Java language

fictions

Java VM

features

Open classesDynamic typing

'eval'ClosuresMixins

Regular expressionsPrimitive types+ops

Object modelMemory modelDynamic linkingAccess control

GCUnicode

Ruby language

fictions

Monday, October 4, 2010

Page 29: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 30: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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 of 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

Page 31: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

31

JVM extensions for other languages

Monday, October 4, 2010

Page 32: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

31

JVM extensions for other languages

• There’s no shortage of JVM feature suggestions

Monday, October 4, 2010

Page 33: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 34: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 35: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 36: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 37: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 38: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 39: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 40: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

32

Monday, October 4, 2010

Page 41: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

32

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

Monday, October 4, 2010

Page 42: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 43: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

33

More flexible method calls

Monday, October 4, 2010

Page 44: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

33

More flexible method calls

• The invokevirtual bytecode performs a method call

Monday, October 4, 2010

Page 45: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

33

More flexible method calls

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

Monday, October 4, 2010

Page 46: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 47: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 48: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 49: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

34

Caller Methodinvokevirtual

Virtual method call in Java

Monday, October 4, 2010

Page 50: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

35

Caller Method

Dynamic method call

invokedynamic

Monday, October 4, 2010

Page 51: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

35

Caller Method

Language logic

Dynamic method call

invokedynamic

Monday, October 4, 2010

Page 52: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

35

Caller Method

Language logic

invokevirtual

Dynamic method call

invokedynamic

Monday, October 4, 2010

Page 53: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 54: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

36

JRuby caller

Method

JRuby logicinvokedynamic

invokevirtual

Jython logic

Groovy logicJython caller

Groovy caller

Monday, October 4, 2010

Page 55: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

37

Language logic is only needed...

*†‡

Monday, October 4, 2010

Page 56: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

37

Language logic is only needed...

*†‡

Monday, October 4, 2010

Page 57: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

37

Language logic is only needed...

ONCE *†‡

Monday, October 4, 2010

Page 58: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

37

Language logic is only needed...

ONCE *†‡

Monday, October 4, 2010

Page 59: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 60: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

38

The deal with method calls (in one slide)

4Monday, October 4, 2010

Page 61: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

38

The deal with method calls (in one slide)

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

4Monday, October 4, 2010

Page 62: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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-timeDon’t re-do method selection for every single invocation!

4Monday, October 4, 2010

Page 63: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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-timeDon’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

4Monday, October 4, 2010

Page 64: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

39

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

5Monday, October 4, 2010

Page 65: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

39

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

• Naming — using a symbolic name

5Monday, October 4, 2010

Page 66: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

39

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

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

5Monday, October 4, 2010

Page 67: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

5Monday, October 4, 2010

Page 68: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

5Monday, October 4, 2010

Page 69: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

40

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

6Monday, October 4, 2010

Page 70: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

40

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

• Including naming, linking, selecting, adapting:

6Monday, October 4, 2010

Page 71: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

6Monday, October 4, 2010

Page 72: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

6Monday, October 4, 2010

Page 73: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

6Monday, October 4, 2010

Page 74: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

6Monday, October 4, 2010

Page 75: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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.

6Monday, October 4, 2010

Page 76: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 77: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 78: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 79: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 80: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 81: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

43

Invokedynamic removes some limits

Monday, October 4, 2010

Page 82: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

43

Invokedynamic removes some limits

• Method naming is not limited to Java APIs

Monday, October 4, 2010

Page 83: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 84: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 85: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 86: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 87: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

45

Phases versus tasks (before invokedynamic)

Monday, October 4, 2010

Page 88: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

46

Phases versus tasks (after invokedynamic)

Monday, October 4, 2010

Page 89: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

47

Method handles and closures

Monday, October 4, 2010

Page 90: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

47

Method handles and closures

• We are working on closures in Java– More flexible, less bulky than anonymous inner classes

Monday, October 4, 2010

Page 91: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 92: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 93: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

48

Invokedynamic and closures?

Monday, October 4, 2010

Page 94: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

48

Invokedynamic and closures?

• An instructive possibility...

Monday, October 4, 2010

Page 95: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

48

Invokedynamic and closures?

• An instructive possibility...

1. Compile the data type and target types as Bootstrap Method parameters.

Monday, October 4, 2010

Page 96: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 97: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 98: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 99: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 100: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

49

JSR 292 design news

Monday, October 4, 2010

Page 101: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 102: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 103: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 104: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 105: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

50

What’s next? A standard

Monday, October 4, 2010

Page 106: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

50

What’s next? A standard

• Reference Implementation driven as part of JDK 7

Monday, October 4, 2010

Page 107: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 108: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 109: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 110: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

51

What’s next? Da Vinci projects

Monday, October 4, 2010

Page 111: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

51

What’s next? Da Vinci projects

• The Da Vinci Machine Project continues

Monday, October 4, 2010

Page 112: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 113: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 114: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

52

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

Monday, October 4, 2010

Page 115: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

52

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

• Architecture ≠ optimization

Monday, October 4, 2010

Page 116: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

52

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

• Architecture ≠ optimization• Architecture → enables optimization

Monday, October 4, 2010

Page 117: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

52

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

• Architecture ≠ optimization• Architecture → enables optimization

• Efficient method handle creation

Monday, October 4, 2010

Page 118: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 119: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 120: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

53

“Fixnums” – tagged immediate pseudo-pointers

Monday, October 4, 2010

Page 121: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

53

“Fixnums” – tagged immediate pseudo-pointers

• In Java, primitives can be “autoboxed”– This convenience was added in JDK 5

Monday, October 4, 2010

Page 122: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 123: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 124: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

54

A list of integer values (before fixnums)

Monday, October 4, 2010

Page 125: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

55

A list of integer values (after fixnums)

Monday, October 4, 2010

Page 126: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

56

Fixnums in the Java VM

Monday, October 4, 2010

Page 127: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

56

Fixnums in the Java VM

• The JVM can also do the “fixnum” trick

Monday, October 4, 2010

Page 128: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 129: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 130: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 131: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

57

Implications for languages

Monday, October 4, 2010

Page 132: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 133: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 134: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 135: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 136: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

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

Page 137: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

58

To find out more...

Monday, October 4, 2010

Page 138: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

58

To find out more...

• “Bytecodes meet Combinators: invokedynamic on the JVM”, Rose VMIL 2009http://blogs.sun.com/jrose/entry/vmil_paper_on_invokedynamic

Monday, October 4, 2010

Page 139: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

58

To find out more...

• “Bytecodes meet Combinators: invokedynamic on the JVM”, Rose VMIL 2009http://blogs.sun.com/jrose/entry/vmil_paper_on_invokedynamic

• “Optimizing Invokedynamic”, Thalinger PPPJ 2010http://blogs.sun.com/jrose/entry/an_experiment_with_generic_arithmetic

Monday, October 4, 2010

Page 140: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

58

To find out more...

• “Bytecodes meet Combinators: invokedynamic on the JVM”, Rose VMIL 2009http://blogs.sun.com/jrose/entry/vmil_paper_on_invokedynamic

• “Optimizing Invokedynamic”, Thalinger PPPJ 2010http://blogs.sun.com/jrose/entry/an_experiment_with_generic_arithmetic

• JVM Language Summit 2010http://wiki.jvmlangsummit.com

Monday, October 4, 2010

Page 141: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

58

To find out more...

• “Bytecodes meet Combinators: invokedynamic on the JVM”, Rose VMIL 2009http://blogs.sun.com/jrose/entry/vmil_paper_on_invokedynamic

• “Optimizing Invokedynamic”, Thalinger PPPJ 2010http://blogs.sun.com/jrose/entry/an_experiment_with_generic_arithmetic

• JVM Language Summit 2010http://wiki.jvmlangsummit.com

• Da Vinci Machine Projecthttp://openjdk.java.net/projects/mlvm/

Monday, October 4, 2010

Page 142: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

58

To find out more...

• “Bytecodes meet Combinators: invokedynamic on the JVM”, Rose VMIL 2009http://blogs.sun.com/jrose/entry/vmil_paper_on_invokedynamic

• “Optimizing Invokedynamic”, Thalinger PPPJ 2010http://blogs.sun.com/jrose/entry/an_experiment_with_generic_arithmetic

• JVM Language Summit 2010http://wiki.jvmlangsummit.com

• Da Vinci Machine Projecthttp://openjdk.java.net/projects/mlvm/

• Friday 9/25 bonus: http://scalaliftoff.com/(discount code = scalaone)

Monday, October 4, 2010

Page 143: One VM, Many Languages - GOTO Bloggotocon.com/.../slides/BrianGoetz_OneVMManyLanguages.pdf · 2010-10-06 · One VM, Many Languages Brian Goetz Java Language Architect, Oracle Corporation

59

Monday, October 4, 2010