Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 3
Classroom Training
Learning Subscription
Live Virtual Class
Training On Demand
Keep Learning with Oracle University
education.oracle.com
Cloud
Technology
Applications
Industries
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Session Surveys
Help us help you!!
• Oracle would like to invite you to take a moment to give us your session
feedback. Your feedback will help us to improve your conference.
• Please be sure to add your feedback for your attended sessions by using
the Mobile Survey or in Schedule Builder.
4
Invokedynamic
for Mere Mortals
David Buck
Principal Member of Technical Staff
Java SE
October 26, 2015
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement
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.
6
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Program Agenda
Introduction
java.lang.invoke
invokedynamic instruction
Other stuff
1
2
3
4
7
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Introduction
8
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Target Audience
• Not compiler writers
• Curious
9
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Motivation
• Understand javap output better
• Understand the value JVM has as a multi-language JVM
10
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Da Vinci Machine Project
• The JVM is a great platform for running all sorts of languages
– Great performance
– Portability
– Security (sandbox)
– Pre-existing libraries and frameworks
11
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
• JVM-specific
– Scala
– Clojure
– Groovy
– Ceylon
– Fortress
– Gosu
– Kotlin
• Ported to JVM
– JRuby
– Jython
– Smalltalk
– Ada
– Scheme
– REXX
– Prolog
– Pascal
– Common LISP
(a small subset of) JVM languages
12
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Java Code
Language Runtime
13
JVM
OS
Java
Class Library
JRuby Runtime
JVM
OS
Java
Class Library
Ruby Code
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
non-Java language wish list
• Continuations
• Dynamic invocation
• Tail recursion
• Interface injection
• Other stuff
14
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
non-Java language wish list
• Continuations
• Dynamic invocation
• Tail recursion
• Interface injection
• Other stuff
15
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
What is dynamic typing?
16
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
What is dynamic typing?
def addtwo(a, b)
a + b;
end
17
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
What is dynamic typing?
We do not know what the types are until runtime
18
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
statically-typed vs. dynamically-typed
When do we type check / link?
– Compilation time (javac)
– Runtime
19
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Compile-time checking / linking
• Catch errors early
• Limits the type of code we can write (false positives)
20
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Run time checking / linking
• Allow more freedom of programming (less false positives)
• Less guarantees about runtime behavior
21
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
dynamic typing != type inference
object InferenceTest1 extends App {
val x = 1 + 2 * 3 // the type of x is Int
val y = x.toString() // the type of y is String
def succ(x: Int) = x + 1 // succ returns Int values
}
(Shamelessly copied from http://docs.scala-lang.org/tutorials/tour/local-
type-inference.html)
22
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
dynamic typing != week typing
a = "40"
b = a + 2
23
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Dynamically-typed languages
• Allow more programs, but have to do more runtime checking.
• No perfect type information at compile time
24
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Polymorphism != Dynamic typing (?!)
public String bar(Object o) {
return "You passed me " + o.toString();
}
25
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
The original invocation lineup
• invokestatic
– Class method
• invokevirtual
– Instance method
• invokeinterface
– Interface method
• Invokespecial
• Everything else (private, super class, constructors)
26
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
The original invocation lineup
• invokestatic
– Class method
• invokevirtual
– Instance method
• invokeinterface
– Interface method
• Invokespecial
• Everything else (private, super class, constructors)
27
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
invokestatic
public class InvokeStaticExample {
public static void main(String[] args) {
InvokeStaticExample.foo();
}
public static void foo() {
System.out.println("I am foo!");
}
}
28
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
The original invocation lineup
• invokestatic
– Class method
• invokevirtual
– Instance method
• invokeinterface
– Interface method
• Invokespecial
• Everything else (private, super class, constructors)
29
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
invokevirtual
public class InvokeVirtualExample {
public static void main(String[] args) {
InvokeVirtualExample ive = new InvokeVirtualExample();
ive.foo();
}
public void foo() {
System.out.println("I am foo!");
}
}
30
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
The original invocation lineup
• invokestatic
– Class method
• invokevirtual
– Instance method
• invokeinterface
– Interface method
• Invokespecial
• Everything else (private, super class, constructors)
31
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
public class InvokeInterfaceExample
implements MyInterface {
public static void main(String[] args)
{
MyInterface iie = new
InvokeInterfaceExample();
iie.foo();
}
public void foo() {
System.out.println("I am foo!");
}
}
interface MyInterface {
public void foo();
}
32
invokeinterface
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
The original invocation lineup
• invokestatic
– Class method
• invokevirtual
– Instance method
• invokeinterface
– Interface method
• Invokespecial
• Everything else (private, super class, constructors)
33
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
invokespecial
public class InvokeSpecialExample {
public static void main(String[] args) {
InvokeSpecialExample ise = new InvokeSpecialExample();
ise.foo();
}
private void foo() {
System.out.println("I am foo!");
}
}
34
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Poor dynamic languages on JVM?
• invocation logic is not baked into the JVM like it is for Java
• we need to fall back on reflection
35
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Reflection is slow
• security check on each invocation
• all arguments are Objects (boxing)
36
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
What the JVM doesn’t know can hurt it
37
Caller Reflection Magic! Callee
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Reflection prevents inlining!
38
Caller Reflection Magic! Callee
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
No one writes code like this
if (false) {
// do some important stuff...
System.out.println("I'm important!");
}
39
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Or this…
boolean cond = true;
if (cond) {
// do some important stuff...
System.out.println("I'm important!");
}
40
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
public void methodB() {
// ...
methodA(false);
// ...
}
public void methodA(boolean
optionalStuff) {
// ...
if (optionalStuff) {
// do some optional, but
important stuff...
System.out.println("I'm important
sometimes!");
}
// ...
}
41
But we do write stuff like
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
JSR-292
• java.lang.invoke API
A “better reflection”
• invokedynamic bytecode
Allows us to dispatch to linkage logic defined by invoke API
42
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
invokedynamic
• We call it “indy”
• No clear way to express in Java language
• Important milestone for JVM
– First new instruction in decades
– First new JVM feature to only (mainly) target non-java languages
43
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
java.lang.invoke API
• MethodHandle
• CallSite
• Bootstrap Method (BSM)
44
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MethodHandle
45
int foo()Method Handle
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MethodHandle
• Points to a method
• Is a “function pointer” (am I allowed to say this?)
• Polymorphic signature
46
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MethodHandle Performance
47
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MethodHandle Performance
• Early performance was not ideal
48
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MethodHandle Performance
• Early performance was not ideal
• Performance improved tremendously with lambda forms
49
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MethodHandle Performance
• Early performance was not ideal
• Performance improved tremendously with lambda forms
• Is now often significantly faster than reflection
50
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MethodHandle Performance
• Early performance was not ideal
• Performance improved tremendously with lambda forms
• Is now often significantly faster than reflection
• Can be used independently of invokedynamic
51
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
CallSite
52
private void doStuff();
descriptor: ()V
flags: ACC_PRIVATE
Code:
stack=2, locals=2, args_size=1
0: new #7
3: dup
4: invokespecial #8
7: astore_1
8: aload_1
9: aload_0
10: invokedynamic #9, 0
15: invokevirtual #10
18: return
CS Method Handle int foo()
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
CallSite
53
private void doStuff();
descriptor: ()V
flags: ACC_PRIVATE
Code:
stack=2, locals=2, args_size=1
0: new #7
3: dup
4: invokespecial #8
7: astore_1
8: aload_1
9: aload_0
10: invokedynamic #9, 0
15: invokevirtual #10
18: return
CS
int bar()
int foo()
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
CallSite
• Reifies Indy invocation side
• Has a MethodHandle
54
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Bootstrapping Step 1
55
private void doStuff();
descriptor: ()V
flags: ACC_PRIVATE
Code:
stack=2, locals=2, args_size=1
0: new #7
3: dup
4: invokespecial #8
7: astore_1
8: aload_1
9: aload_0
10: invokedynamic #9, 0
15: invokevirtual #10
18: return
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Bootstrapping Step 2
56
private void doStuff();
descriptor: ()V
flags: ACC_PRIVATE
Code:
stack=2, locals=2, args_size=1
0: new #7
3: dup
4: invokespecial #8
7: astore_1
8: aload_1
9: aload_0
10: invokedynamic #9, 0
15: invokevirtual #10
18: return BootStrap Method
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Bootstrapping Step 3
57
private void doStuff();
descriptor: ()V
flags: ACC_PRIVATE
Code:
stack=2, locals=2, args_size=1
0: new #7
3: dup
4: invokespecial #8
7: astore_1
8: aload_1
9: aload_0
10: invokedynamic #9, 0
15: invokevirtual #10
18: return BootStrap Method
int foo()
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Bootstrapping Step 4
58
private void doStuff();
descriptor: ()V
flags: ACC_PRIVATE
Code:
stack=2, locals=2, args_size=1
0: new #7
3: dup
4: invokespecial #8
7: astore_1
8: aload_1
9: aload_0
10: invokedynamic #9, 0
15: invokevirtual #10
18: return BootStrap Method
int foo()
CS
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Bootstrapping Step 5
59
private void doStuff();
descriptor: ()V
flags: ACC_PRIVATE
Code:
stack=2, locals=2, args_size=1
0: new #7
3: dup
4: invokespecial #8
7: astore_1
8: aload_1
9: aload_0
10: invokedynamic #9, 0
15: invokevirtual #10
18: return
int foo()
CS
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Bootstrap Method
• Only called on the first invocation of each indy bytecode
• Returns a CallSite
60
"Dr Martens, black, old" by Tarquin
is licensed under CC BY-SA 3.0
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Indy lifecycle
Initial Invocation
1. A specific indy invocation is executed for the first time
2. Bootstrap method is called and if finds (generates?!) a method to run
3. Botstrap method returns a permanent CallSite object for this indy
invocation
4. We jump to the method pointed to by the CallSite
61
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Indy Lifecycle
All subsequent calls
We jump to the method pointed to by the CallSite
62
Picture from
National Archives and Records Administration
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
This performance tragedy becomes
63
Caller Reflection Magic! Callee
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 64
Caller Callee
MethodHandle
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Linkage != Invocation
65
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Linkage != Invocation
• Linkage (i.e. bootstrap)
– Usually only needs to be done once
– Is expensive
66
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Linkage != Invocation
• Linkage (i.e. bootstrap)
– Usually only needs to be done once
– Is expensive
• Invocation
– Done a lot
– Only needs a jmp/call (and possibly a guard)
67
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Linkage != Dispatch
• Avoid the cost of linkage on almost every call
68
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Takeaways
69
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Takeaways
• Invokedynamic lets us programmatically alter linkage
70
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Takeaways
• Invokedynamic lets us programmatically alter linkage
• Then it gets out of the way! (linkage != invocation)
71
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Takeaways
• Invokedynamic lets us programmatically alter linkage
• Then it gets out of the way! (linkage != invocation)
• The Invoke API can often be used without indy
72
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Takeaways
• Invokedynamic lets us programmatically alter linkage
• Then it gets out of the way! (linkage != invocation)
• The Invoke API can often be used without indy
• JVM is a great platform for just about any language!
73
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Resources
• JVM Language Summit
http://openjdk.java.net/projects/mlvm/jvmlangsummit/
• Linkers & Loaders book
http://linker.iecc.com/
• John Rose’s Blog
https://blogs.oracle.com/jrose/
74
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Thank You!
75
InvokeDynamic for Mere Mortals [JavaOne 2015 CON7682]
InvokeDynamic for Mere Mortals [JavaOne 2015 CON7682]

InvokeDynamic for Mere Mortals [JavaOne 2015 CON7682]

  • 3.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | 3 Classroom Training Learning Subscription Live Virtual Class Training On Demand Keep Learning with Oracle University education.oracle.com Cloud Technology Applications Industries
  • 4.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Session Surveys Help us help you!! • Oracle would like to invite you to take a moment to give us your session feedback. Your feedback will help us to improve your conference. • Please be sure to add your feedback for your attended sessions by using the Mobile Survey or in Schedule Builder. 4
  • 5.
    Invokedynamic for Mere Mortals DavidBuck Principal Member of Technical Staff Java SE October 26, 2015 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
  • 6.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Safe Harbor Statement 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. 6
  • 7.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Program Agenda Introduction java.lang.invoke invokedynamic instruction Other stuff 1 2 3 4 7
  • 8.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Introduction 8
  • 9.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Target Audience • Not compiler writers • Curious 9
  • 10.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Motivation • Understand javap output better • Understand the value JVM has as a multi-language JVM 10
  • 11.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Da Vinci Machine Project • The JVM is a great platform for running all sorts of languages – Great performance – Portability – Security (sandbox) – Pre-existing libraries and frameworks 11
  • 12.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | • JVM-specific – Scala – Clojure – Groovy – Ceylon – Fortress – Gosu – Kotlin • Ported to JVM – JRuby – Jython – Smalltalk – Ada – Scheme – REXX – Prolog – Pascal – Common LISP (a small subset of) JVM languages 12
  • 13.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Java Code Language Runtime 13 JVM OS Java Class Library JRuby Runtime JVM OS Java Class Library Ruby Code
  • 14.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | non-Java language wish list • Continuations • Dynamic invocation • Tail recursion • Interface injection • Other stuff 14
  • 15.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | non-Java language wish list • Continuations • Dynamic invocation • Tail recursion • Interface injection • Other stuff 15
  • 16.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | What is dynamic typing? 16
  • 17.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | What is dynamic typing? def addtwo(a, b) a + b; end 17
  • 18.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | What is dynamic typing? We do not know what the types are until runtime 18
  • 19.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | statically-typed vs. dynamically-typed When do we type check / link? – Compilation time (javac) – Runtime 19
  • 20.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Compile-time checking / linking • Catch errors early • Limits the type of code we can write (false positives) 20
  • 21.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Run time checking / linking • Allow more freedom of programming (less false positives) • Less guarantees about runtime behavior 21
  • 22.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | dynamic typing != type inference object InferenceTest1 extends App { val x = 1 + 2 * 3 // the type of x is Int val y = x.toString() // the type of y is String def succ(x: Int) = x + 1 // succ returns Int values } (Shamelessly copied from http://docs.scala-lang.org/tutorials/tour/local- type-inference.html) 22
  • 23.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | dynamic typing != week typing a = "40" b = a + 2 23
  • 24.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Dynamically-typed languages • Allow more programs, but have to do more runtime checking. • No perfect type information at compile time 24
  • 25.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Polymorphism != Dynamic typing (?!) public String bar(Object o) { return "You passed me " + o.toString(); } 25
  • 26.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | The original invocation lineup • invokestatic – Class method • invokevirtual – Instance method • invokeinterface – Interface method • Invokespecial • Everything else (private, super class, constructors) 26
  • 27.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | The original invocation lineup • invokestatic – Class method • invokevirtual – Instance method • invokeinterface – Interface method • Invokespecial • Everything else (private, super class, constructors) 27
  • 28.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | invokestatic public class InvokeStaticExample { public static void main(String[] args) { InvokeStaticExample.foo(); } public static void foo() { System.out.println("I am foo!"); } } 28
  • 29.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | The original invocation lineup • invokestatic – Class method • invokevirtual – Instance method • invokeinterface – Interface method • Invokespecial • Everything else (private, super class, constructors) 29
  • 30.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | invokevirtual public class InvokeVirtualExample { public static void main(String[] args) { InvokeVirtualExample ive = new InvokeVirtualExample(); ive.foo(); } public void foo() { System.out.println("I am foo!"); } } 30
  • 31.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | The original invocation lineup • invokestatic – Class method • invokevirtual – Instance method • invokeinterface – Interface method • Invokespecial • Everything else (private, super class, constructors) 31
  • 32.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | public class InvokeInterfaceExample implements MyInterface { public static void main(String[] args) { MyInterface iie = new InvokeInterfaceExample(); iie.foo(); } public void foo() { System.out.println("I am foo!"); } } interface MyInterface { public void foo(); } 32 invokeinterface
  • 33.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | The original invocation lineup • invokestatic – Class method • invokevirtual – Instance method • invokeinterface – Interface method • Invokespecial • Everything else (private, super class, constructors) 33
  • 34.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | invokespecial public class InvokeSpecialExample { public static void main(String[] args) { InvokeSpecialExample ise = new InvokeSpecialExample(); ise.foo(); } private void foo() { System.out.println("I am foo!"); } } 34
  • 35.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Poor dynamic languages on JVM? • invocation logic is not baked into the JVM like it is for Java • we need to fall back on reflection 35
  • 36.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Reflection is slow • security check on each invocation • all arguments are Objects (boxing) 36
  • 37.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | What the JVM doesn’t know can hurt it 37 Caller Reflection Magic! Callee
  • 38.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Reflection prevents inlining! 38 Caller Reflection Magic! Callee
  • 39.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | No one writes code like this if (false) { // do some important stuff... System.out.println("I'm important!"); } 39
  • 40.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Or this… boolean cond = true; if (cond) { // do some important stuff... System.out.println("I'm important!"); } 40
  • 41.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | public void methodB() { // ... methodA(false); // ... } public void methodA(boolean optionalStuff) { // ... if (optionalStuff) { // do some optional, but important stuff... System.out.println("I'm important sometimes!"); } // ... } 41 But we do write stuff like
  • 42.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JSR-292 • java.lang.invoke API A “better reflection” • invokedynamic bytecode Allows us to dispatch to linkage logic defined by invoke API 42
  • 43.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | invokedynamic • We call it “indy” • No clear way to express in Java language • Important milestone for JVM – First new instruction in decades – First new JVM feature to only (mainly) target non-java languages 43
  • 44.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | java.lang.invoke API • MethodHandle • CallSite • Bootstrap Method (BSM) 44
  • 45.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | MethodHandle 45 int foo()Method Handle
  • 46.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | MethodHandle • Points to a method • Is a “function pointer” (am I allowed to say this?) • Polymorphic signature 46
  • 47.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | MethodHandle Performance 47
  • 48.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | MethodHandle Performance • Early performance was not ideal 48
  • 49.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | MethodHandle Performance • Early performance was not ideal • Performance improved tremendously with lambda forms 49
  • 50.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | MethodHandle Performance • Early performance was not ideal • Performance improved tremendously with lambda forms • Is now often significantly faster than reflection 50
  • 51.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | MethodHandle Performance • Early performance was not ideal • Performance improved tremendously with lambda forms • Is now often significantly faster than reflection • Can be used independently of invokedynamic 51
  • 52.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | CallSite 52 private void doStuff(); descriptor: ()V flags: ACC_PRIVATE Code: stack=2, locals=2, args_size=1 0: new #7 3: dup 4: invokespecial #8 7: astore_1 8: aload_1 9: aload_0 10: invokedynamic #9, 0 15: invokevirtual #10 18: return CS Method Handle int foo()
  • 53.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | CallSite 53 private void doStuff(); descriptor: ()V flags: ACC_PRIVATE Code: stack=2, locals=2, args_size=1 0: new #7 3: dup 4: invokespecial #8 7: astore_1 8: aload_1 9: aload_0 10: invokedynamic #9, 0 15: invokevirtual #10 18: return CS int bar() int foo()
  • 54.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | CallSite • Reifies Indy invocation side • Has a MethodHandle 54
  • 55.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Bootstrapping Step 1 55 private void doStuff(); descriptor: ()V flags: ACC_PRIVATE Code: stack=2, locals=2, args_size=1 0: new #7 3: dup 4: invokespecial #8 7: astore_1 8: aload_1 9: aload_0 10: invokedynamic #9, 0 15: invokevirtual #10 18: return
  • 56.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Bootstrapping Step 2 56 private void doStuff(); descriptor: ()V flags: ACC_PRIVATE Code: stack=2, locals=2, args_size=1 0: new #7 3: dup 4: invokespecial #8 7: astore_1 8: aload_1 9: aload_0 10: invokedynamic #9, 0 15: invokevirtual #10 18: return BootStrap Method
  • 57.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Bootstrapping Step 3 57 private void doStuff(); descriptor: ()V flags: ACC_PRIVATE Code: stack=2, locals=2, args_size=1 0: new #7 3: dup 4: invokespecial #8 7: astore_1 8: aload_1 9: aload_0 10: invokedynamic #9, 0 15: invokevirtual #10 18: return BootStrap Method int foo()
  • 58.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Bootstrapping Step 4 58 private void doStuff(); descriptor: ()V flags: ACC_PRIVATE Code: stack=2, locals=2, args_size=1 0: new #7 3: dup 4: invokespecial #8 7: astore_1 8: aload_1 9: aload_0 10: invokedynamic #9, 0 15: invokevirtual #10 18: return BootStrap Method int foo() CS
  • 59.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Bootstrapping Step 5 59 private void doStuff(); descriptor: ()V flags: ACC_PRIVATE Code: stack=2, locals=2, args_size=1 0: new #7 3: dup 4: invokespecial #8 7: astore_1 8: aload_1 9: aload_0 10: invokedynamic #9, 0 15: invokevirtual #10 18: return int foo() CS
  • 60.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Bootstrap Method • Only called on the first invocation of each indy bytecode • Returns a CallSite 60 "Dr Martens, black, old" by Tarquin is licensed under CC BY-SA 3.0
  • 61.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Indy lifecycle Initial Invocation 1. A specific indy invocation is executed for the first time 2. Bootstrap method is called and if finds (generates?!) a method to run 3. Botstrap method returns a permanent CallSite object for this indy invocation 4. We jump to the method pointed to by the CallSite 61
  • 62.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Indy Lifecycle All subsequent calls We jump to the method pointed to by the CallSite 62 Picture from National Archives and Records Administration
  • 63.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | This performance tragedy becomes 63 Caller Reflection Magic! Callee
  • 64.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | 64 Caller Callee MethodHandle
  • 65.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Linkage != Invocation 65
  • 66.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Linkage != Invocation • Linkage (i.e. bootstrap) – Usually only needs to be done once – Is expensive 66
  • 67.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Linkage != Invocation • Linkage (i.e. bootstrap) – Usually only needs to be done once – Is expensive • Invocation – Done a lot – Only needs a jmp/call (and possibly a guard) 67
  • 68.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Linkage != Dispatch • Avoid the cost of linkage on almost every call 68
  • 69.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Takeaways 69
  • 70.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Takeaways • Invokedynamic lets us programmatically alter linkage 70
  • 71.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Takeaways • Invokedynamic lets us programmatically alter linkage • Then it gets out of the way! (linkage != invocation) 71
  • 72.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Takeaways • Invokedynamic lets us programmatically alter linkage • Then it gets out of the way! (linkage != invocation) • The Invoke API can often be used without indy 72
  • 73.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Takeaways • Invokedynamic lets us programmatically alter linkage • Then it gets out of the way! (linkage != invocation) • The Invoke API can often be used without indy • JVM is a great platform for just about any language! 73
  • 74.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Resources • JVM Language Summit http://openjdk.java.net/projects/mlvm/jvmlangsummit/ • Linkers & Loaders book http://linker.iecc.com/ • John Rose’s Blog https://blogs.oracle.com/jrose/ 74
  • 75.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Thank You! 75