The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

122
The JVM is dead! Long live the Polyglot VM! Marcus Lagergren Oracle

Transcript of The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Page 1: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

The JVM is dead! Long live the Polyglot VM! Marcus Lagergren Oracle

Page 2: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 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."

Page 3: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 3

Who am I?

@lagergren

Page 4: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 4

Program Agenda

§  Introduction

§  History of VM languages and runtimes

§  Emerging languages and language design on top of the JVM

§  Invokedynamic

§  The Nashorn project

§  The Da Vinci Machine Project (MLVM)

Page 5: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 5

Introduction

Page 6: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 6

I am here to talk about…

The Java Runtime: The JVM

Page 7: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 7

I am here to talk about…

The Universal Meta-Execution Environment

Page 8: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 8

I am here to talk about…

The Universal Meta-Execution Environment

Page 9: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 9

I am here to talk about…

The JVM as a multi language runtime

Page 10: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 10

I am here to talk about…

The JVM as a multi language runtime

(especially in the context of dynamic languages)

Page 11: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 11

History (what is a runtime

anyway?)

Page 12: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 12

LISP

Page 13: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 13

LISP

1950s – First compiler in 1962

Page 14: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 14

LISP

1950s – First compiler in 1962 Just-in-time compilation

Page 15: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 15

LISP

1950s – First compiler in 1962 Just-in-time compilation

Page 16: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 16

LISP

1950s – First compiler in 1962 Just-in-time compilation GC – Reference Counting

Page 17: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 17

LISP

1950s – First compiler in 1962 Just-in-time compilation GC – Reference Counting

Page 18: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 18

Smalltalk

Page 19: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 19

Smalltalk

First class library

Page 20: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 20

Smalltalk

First class library First visual GUI driven IDE

Page 21: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 21

Smalltalk

First class library First visual GUI driven IDE BYTECODE!

Page 22: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 22

Smalltalk

First class library First visual GUI driven IDE BYTECODE!

Page 23: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 23

Emerging languages (especially on the

JVM)

Page 24: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 24

Dynamic Languages

ActionScript Adenine APL BASIC BeanShell Clojure ColdFusion Dart Dylan Groovy E Fancy JavaScript

Julia Lua MATLAB Objective-C Perl PHP Powershell Python Qore R

REBOL REXX Ruby Scheme Smalltalk Snit Tcl VBScript Yoix etc

etc etc etc…

Page 25: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 25

Dynamic Languages

ActionScript Adenine APL BASIC BeanShell Clojure ColdFusion Dart Dylan Groovy E Fancy JavaScript

Julia Lua MATLAB Objective-C Perl PHP Powershell Python Qore R

REBOL REXX Ruby Scheme Smalltalk Snit Tcl VBScript Yoix etc

etc etc etc…

Page 26: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 26

Dynamic Languages

•  Dynamic Languages are hot today because •  They are easy to use •  They have no explicit compile stage •  They have good code readability •  Allow short development time for small projects •  Performance is good enough

Page 27: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 27

Trendy Dynamic Languages

Page 28: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 28

Trendy Dynamic Languages

Page 29: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 29

Trendy Dynamic Languages

Page 30: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 30

Trendy Dynamic Languages

Page 31: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 31

Trendy Dynamic Languages

Already on top of the JVM

Page 32: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 32

Trendy Dynamic Languages

Already on top of the JVM

Page 33: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 33

… and of course

V8, Futhark, Carakan, SpiderMonkey, JägerMonkey, *Monkey, Nitro, Rhino, Nashorn, etc etc

Page 34: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 34

Page 35: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 35

Page 36: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 36

Page 37: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 37

Page 38: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 38

Dynamic Languages – Implementation

Page 39: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 39

Dynamic Languages – Implementation

•  Native Runtimes •  Ruby, Perl, Python, v8, *monkey etc

Page 40: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 40

Dynamic Languages – Implementation

•  Native Runtimes •  Ruby, Perl, Python, v8, *monkey etc

•  Metacircular •  PyPy, Steel Banks Common LISP, Rubinius (well

partly), Smalltalk

Page 41: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 41

Dynamic Languages – Implementation

•  Native Runtimes •  Ruby, Perl, Python, v8, *monkey etc

•  Metacircular •  PyPy, Steel Banks Common LISP, Rubinius (well

partly), Smalltalk •  On top of a (J)VM

•  Clojure, Jython, JRuby, Rhino, Nashorn •  DLR/CLR

Page 42: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 42

Dynamic Languages – Implementation

•  Native Runtimes •  Ruby, Perl, Python, v8, *monkey etc

•  Metacircular •  PyPy, Steel Banks Common LISP, Rubinius (well

partly), Smalltalk •  On top of a (J)VM

•  Clojure, Jython, JRuby, Rhino, Nashorn •  DLR/CLR

Page 43: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 43

Dynamic Languages – Characteristics

•  Most emerging JVM languages today are dynamic •  As opposed to “non-dynamic” I guess… •  What is a dynamic language? •  Is there a formal definition?

Page 44: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 44

Dynamic Languages – Characteristics

•  Loosely typed •  Dynamic binding

•  Resolve functions/members/calls at runtime rather than compile time

Page 45: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 45

Dynamic Languages – Characteristics

•  Liberal redefinition policy •  Redefine/modify a class •  Redefine/modify a function

Page 46: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 46

Dynamic Languages – Characteristics

•  Liberal redefinition policy •  Redefine/modify a class •  Redefine/modify a function •  Redefine a builtin even •  Oh, go to hell, JavaScript!

Math.sin = function(x){ return 17; }

Page 47: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 47

Dynamic Languages – Characteristics

•  Code equals data •  eval / REPL •  Automatic memory management

Page 48: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 48

Dynamic Languages – Characteristics

•  But you can extend Java at runtime too, can’t you? •  Maybe a “non dynamic” language is more like C? •  I don’t think the “dynamic” prefix matters much

•  Things change at runtime – handle it

Page 49: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 49

Putting your language on top of the JVM

Page 50: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 50

Why?

•  You get so much for free •  Automatic memory management •  State of the art JIT optimizations •  Native threading capability •  Hybridization (JSR-223) •  Man decades of high tech

Page 51: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 51

Why?

0

100000

200000

300000

400000

500000

Code bases

Lines

Page 52: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 52

Why?

0

100000

200000

300000

400000

500000

Code bases

Lines

If it were all about code complexity – TOTALLY WORTH IT!

Page 53: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 53

“All problems in computer science can be solved by another level of indirection”

- David Wheeler

Page 54: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 54

“All problems in computer science can be solved by another level of indirection”

- David Wheeler

•  Sounds good! Implement it! •  Just serve up some bytecode •  People have been doing it since 1996

Page 55: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 55

They have been doing it a lot, actually…

Fantom

Fortress

BeanShell

Jaskell

ANTLR

ABCL

Erjang

X10

jgo

Nice

Gosu

Jacl

Page 56: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 56

“All problems in computer science can be solved by another level of indirection”

- David Wheeler

Page 57: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 57

“All problems in computer science can be solved by another level of indirection”

- David Wheeler

“Except for the problem of too many layers of indirection” - Kevlin Henney

Page 58: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 58

So why is it hard?

Page 59: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 59

So why is it hard?

•  Different levels of “hard” •  Square peg, round hole or round peg, oval hole? •  Scala is a fairly good fit*

Page 60: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 60

So why is it hard?

•  Different levels of “hard” •  Square peg, round hole or round peg, oval hole? •  Scala is a fairly good fit*

•  yes I know about tail call optimization and interface injection – also I was at JVMLS 2013 which left me with mental scars.

Page 61: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 61

So why is it hard?

•  Different levels of “hard” •  Square peg, round hole or round peg, oval hole? •  Scala is a fairly good fit •  Ruby or JavaScript are (at least at first glance) pretty

lousy ones

Page 62: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 62

So why is it hard?

•  Java bytecode [sic] •  Notice the “Java”

•  There are “classes”, “methods”, size limitations, strong types

•  Languages can be much more dynamic than Java •  Different linkage •  Loose types

Page 63: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 63

Not every language has exactly 5 strong types

int, long, float, double, Object int sum(int a, int b) { return a + b; }

Page 64: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 64

Not every language has exactly 5 strong types

int, long, float, double, Object iload_1 iload_2 iadd ireturn

Page 65: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 65

Not every language has exactly 5 strong types

int, long, float, double, Object function sum(a, b) { return a + b; }

Page 66: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 66

Not every language has exactly 5 strong types

int, long, float, double, Object ???? ???? ???? ????

Page 67: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 67

Not every language has exactly 5 strong types

int, long, float, double, Object ???? ???? ???? ????

Page 68: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 68

Also: it is hard to swap out code with other code

Page 69: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 69

Also: it is hard to swap out code with other code

Page 70: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 70

Applicability and performance

•  The extra layer costs us performance •  How can we work around it?

Page 71: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 71

Applicability and performance

•  The extra layer costs us performance •  How can we work around it? •  Passive

•  Just wait •  JIT is getting better all the time, GC is getting better

all the time

Page 72: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 72

Applicability and performance

•  The extra layer costs us performance •  How can we work around it? •  Passive

•  Just wait •  JIT is getting better all the time, GC is getting better

all the time •  Active

•  Punch through the indirection layer •  There are tools these days

Page 73: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 73

Punch through the indirection layer

invokedynamic is my ice pick!

Page 74: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 74

Invokedynamic

•  The first new bytecode since 1996 •  More than new type of call •  Breaks the constraints of Java call/linkage •  Can implement calls that act like function pointers •  More general: can implement custom data access

Page 75: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 75

The whole point: The JVM can optimize this!

Page 76: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 76

invokedynamic bytecode

Page 77: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 77

invokedynamic bytecode

Bootstrap Method

Calls (once)

Page 78: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 78

invokedynamic bytecode

Bootstrap Method

java.lang.invoke.CallSite CallSite

Calls (once)

Returns

Page 79: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 79

invokedynamic bytecode

Bootstrap Method

java.lang.invoke.CallSite CallSite

Target java.lang.invoke.MethodHandle

Calls (once)

Returns

Contains

Page 80: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 80

Example – bootstrap

public static CallSite bootstrap( MethodHandles.Lookup lookup, String name,

MethodType type, Object... metainfo) { //optional

MethodHandle mh = lookup.findStaticMethod( getClass(),

name, type);

return new MyMutableCallsite(mh, metainfo); }

Page 81: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 81

Example – java.lang.invoke •  MutableCallSite (ConstantCallSite)

•  setTarget, getTarget

•  MethodHandles •  guardWithTest

•  filterArguments •  filterReturnValue •  dropArguments

•  etc •  SwitchPoint

Page 82: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 82

Example – dynamic invocation

•  Parameter types known only at run time function add(a, b) {

return a + b; } var res = add(f(), g());

Page 83: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 83

Example – dynamic invocation

•  Parameter types known only at run time function add(a, b) {

return a + b; } var res = add(f(), g());

int add_int(int a, int b) { return a + b; }

Page 84: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 84

Example – dynamic invocation

•  Parameter types known only at run time function add(a, b) {

return a + b; } var res = add(f(), g()); int add_int(int a, int b) { return a + b; }

Object add_obj(Object a, Object b){ return JavaScript.ADD(a, b); }

Page 85: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 85

Example – dynamic invocation

•  Parameter types known only at run time var res = add(f(), g());

Page 86: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 86

Example – dynamic invocation

•  Parameter types known only at run time var res = add(f(), g());

boolean intsGuard(Object a, Object b) { return a.getClass() == Integer.class && b.getClass() == Integer.class; }

Page 87: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 87

Example – dynamic invocation

•  Parameter types known only at run time var res = add(f(), g());

boolean intsGuard(Object a, Object b) { return a.getClass() == Integer.class && b.getClass() == Integer.class; }

MethodHandle ADD = MethodHandles.guardWithTest( intsGuard, add_int, add_obj);

Object res = ADD(f(), g());

Page 88: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 88

Example – function call reassignment function square(x) { return x * x; } function multiply(x) { return x * 2; } function compute(x) { return square(x) + multiply(x); }

Page 89: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 89

Example – function call reassignment function square(x) { return x * x; } function multiply(x) { return x * 2; } function compute(x) { return square(x) + multiply(x); } multiply = function(x) { return x * 3; }

Page 90: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 90

Example – function call reassignment function square(x) { return x * x; } function multiply(x) { return x * 2; } function compute(x) { return square(x) + multiply(x); } multiply = function(x) { return x * 3; }

UH OH!

Page 91: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 91

Example – lazy constant initialization

•  Call site with one value, available only at runtime •  The “static final” approach won’t work

•  Value calculated once and remains immutable

Page 92: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 92

Example – lazy constant initialization

•  Call site with one value, available only at runtime •  The “static final” approach won’t work

•  Value calculated once and remains immutable

return new ConstantCallSite( MethodHandles.constant(

Data.class, loadDataFromDataBase());

Page 93: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 93

Java 8 also uses invokedynamic

(Delegators with lambda)

Page 94: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 94

The Nashorn Project

Page 95: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 95

What is Nashorn?

•  Nashorn is a 100% pure Java runtime for JavaScript •  Nashorn generates bytecode

•  Invokedynamics are everywhere •  Nashorn currently performs somewhere on the order of

~2-10x better than Rhino •  Nashorn is in JDK 8 •  Nashorn is 100% ECMAScript compliant •  Nashorn has a well thought through security model

Page 96: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 96

Why Nashorn?

•  Started as an invokedynamic POC. •  Rhino is still alive today after ~18 years. Why?

•  JSR-223 •  Nashorn is now mature and replaces Rhino for Java 8

Page 97: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 97

Page 98: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 98

Page 99: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 99

Rationale – JavaScript?

•  Extremely dynamic •  All (well, most) setters, getters, calls, have to be

invokedynamics •  Rhino is slow and old •  JSR-223

•  Should make it easy to provide POC apps for Nashorn

Page 100: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 100

Rationale

Page 101: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 101

Full ECMAScript compliance

Page 102: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 102

Performance

rhino nashorn

0 1 2 3 4 5 6 7 8

rhino

nashorn

Page 103: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 103

Performance

rhino nashorn

0 1 2 3 4 5 6 7 8

rhino

nashorn

Page 104: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 104

Nashorn current performance status

•  As of late August: •  No longer understaffed for performance •  We should try for native-like performance by

•  Modifying the JVM’s invokedynamic implementation

•  Changing Nashorn’s currently conservative type model.

Page 105: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 105

•  Key to native like performance •  In Nashorn: replace conservative types with

optimistic ones – implement rollback mechanism •  In the VM: math intrinsics (done), lambdaform

performance, better inlining, boxing

Nashorn current performance status

Page 106: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 106

Nashorn current performance status

•  (Very) initial POC after 2.5 weeks of work: •  Broke out octane.crypto.am3 – the hotspot in

the Crypto benchmark in octane. •  Turned it into microbenchmark

Page 107: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 107

Nashorn current performance status

•  Runtime

•  Rhino (with –opt 9): 34.6 s •  Nashorn tip: 10.8 s •  V8 1.3 s

Page 108: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 108

Nashorn with optimistic types

•  Runtime

•  Rhino (with –opt 9): 34.6 s •  Nashorn tip: 5.8 s •  V8 1.3 s

Page 109: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 109

Add JVM math intrinsics…

•  Runtime

•  Rhino (with –opt 9): 34.6 s •  Nashorn tip: 4.4 s •  V8 1.3 s

Page 110: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 110

Patch JVM to keep more type info while inlining…

•  Runtime

•  Rhino (with –opt 9): 34.6 s •  Nashorn tip: 2.5 s •  V8 1.3 s

Page 111: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 111

More information

•  We now run larger apps •  My JVMLS 2013 talk “Nashorn War Stories”

•  http://tinyurl.com/nashorn-war-stories-slides

•  http://tinyurl.com/nashorn-war-stories

(unshortened URLs)

http://www.slideshare.net/lagergren/lagergren-jvmls2013final

http://medianetwork.oracle.com/video/player/2630340183001

Page 112: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 112

Nashorn on the Server: Avatar.js

•  Server side JavaScript on the JVM •  An implementation of the node programming model

•  For writing enterprise applications in Java and JavaScript

•  Automatically provides seamless integration with existing Java libraries

•  Parallelism / background Java threads •  Small enough for the embedded space •  http://avatar-js.java.net

Page 113: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 113

Further POC: Askari Debugger

•  Debugger for Nashorn written in Nashorn •  Replace / view code while writing it •  3 weeks of work to get it up and running

Page 114: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 114

Nashorn already in OpenJDK 8

Now: more Nashorn performance enhancements

Now: more JVM performance

enhancements

Page 115: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 115

Contribute! 1.  Ask the community to contribute

functionality, testing, performance [analysis], bug fixes, library optimizations, browser simulation frameworks, kick-ass hybrid Java solutions. JVM optimizations

2.  … 3.  PROFIT!

blogs.oracle.com/nashorn

[email protected]

Page 116: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 116

The Da Vinci Machine Project

Page 117: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 117

Can we do better than the ice pick?

Page 118: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 118

Can we do better than the ice pick?

•  Reshape the hole!

Page 119: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 119

Let’s continue building our “future VM”

•  An open source incubator for JVM futures •  Contains code fragments (patches) •  Migration to OpenJDK requires

•  A standard •  A feature release plan

[email protected]

Page 120: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 120

Da Vinci Machine Patches

Page 121: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 121

It’s not just invokedynamic!

The JVM is evolving to become the multi-language runtime

Page 122: The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren (Oracle)

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 122

Thank you!

Q&A? @lagergren