SlideShare a Scribd company logo
InvokeDynamic for Mere Mortals [JavaOne 2015 CON7682]
InvokeDynamic for Mere Mortals [JavaOne 2015 CON7682]
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]

More Related Content

PPTX
Java 101
PDF
CompletableFuture уже здесь
PDF
Does Java Need Inline Types? What Project Valhalla Can Bring to Java
PDF
JIT Versus AOT: Unity And Conflict of Dynamic and Static Compilers (JavaOne 2...
PDF
WebSocket in Enterprise Applications 2015
PDF
How to start learning java
PDF
How to Thrive on REST/WebSocket-Based Microservices
PPTX
Ahead-Of-Time Compilation of Java Applications
Java 101
CompletableFuture уже здесь
Does Java Need Inline Types? What Project Valhalla Can Bring to Java
JIT Versus AOT: Unity And Conflict of Dynamic and Static Compilers (JavaOne 2...
WebSocket in Enterprise Applications 2015
How to start learning java
How to Thrive on REST/WebSocket-Based Microservices
Ahead-Of-Time Compilation of Java Applications

What's hot (20)

PPT
Presentation on java
PPTX
Introduction to java
PPTX
Interactive Java Support to your tool -- The JShell API and Architecture
PPTX
PROGRAMMING IN JAVA- unit 4-part II
PPTX
JShell: An Interactive Shell for the Java Platform
PDF
Java 9, JShell, and Modularity
PDF
Exception handling
PPT
Scala presentationjune112011
PPSX
Introduction to Java
PPTX
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
PDF
Hotspot & AOT
PPTX
Java 101 Intro to Java Programming
PDF
Support formation vidéo : OCA Java SE 8 Programmer (1Z0-808) (2)
PDF
Java - OOPS and Java Basics
PPT
Java Basics
PPTX
PROGRAMMING IN JAVA
PPT
The Economies of Scaling Software
PDF
JavaCro'15 - HUJAKing – Expansion of Java Community - Branko Mihaljević, Alek...
PPTX
Machine Learning Exposed!
PPTX
Java Reflection @KonaTechAdda
Presentation on java
Introduction to java
Interactive Java Support to your tool -- The JShell API and Architecture
PROGRAMMING IN JAVA- unit 4-part II
JShell: An Interactive Shell for the Java Platform
Java 9, JShell, and Modularity
Exception handling
Scala presentationjune112011
Introduction to Java
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Hotspot & AOT
Java 101 Intro to Java Programming
Support formation vidéo : OCA Java SE 8 Programmer (1Z0-808) (2)
Java - OOPS and Java Basics
Java Basics
PROGRAMMING IN JAVA
The Economies of Scaling Software
JavaCro'15 - HUJAKing – Expansion of Java Community - Branko Mihaljević, Alek...
Machine Learning Exposed!
Java Reflection @KonaTechAdda
Ad

Similar to InvokeDynamic for Mere Mortals [JavaOne 2015 CON7682] (20)

PDF
HotSpot Synchronization, A Peek Under the Hood [JavaOne 2015 CON7570]
ODP
JDD2015: Towards the Fastest (J)VM on the Planet! - Jaroslav Tulach
PDF
Java Debuggers: A Peek Under the Hood [JavaOne 2016 CON1503]
PDF
Pushing JavaEE outside of the enterprise: Home Automation & IoT - David Delab...
PDF
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
PDF
Lambdas And Streams in JDK8
PDF
Bytecode Verification, the Hero That Java Needs [JavaOne 2016 CON1500]
PDF
2015 Java update and roadmap, JUG sevilla
PDF
The State of Managed Runtimes 2013, by Attila Szegedi
PDF
8th TUC Meeting - David Meibusch, Nathan Hawes (Oracle Labs Australia). Frapp...
PDF
Pushing Java EE outside of the Enterprise: Home Automation and IoT - David De...
PDF
JavaCro'15 - Everything a Java EE Developer needs to know about the JavaScrip...
PDF
Completable future
PDF
Server Side JavaScript on the Java Platform - David Delabassee
PPTX
Serverless Kotlin
PDF
The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren...
PDF
Pushing Java EE outside of the Enterprise - Home Automation
PDF
[meetup] Mastering Java enhancements like a Pro: practical design patterns an...
PDF
Coding from Application Container Cloud to Oracle JET
PDF
Oracle super cluster for oracle e business suite
HotSpot Synchronization, A Peek Under the Hood [JavaOne 2015 CON7570]
JDD2015: Towards the Fastest (J)VM on the Planet! - Jaroslav Tulach
Java Debuggers: A Peek Under the Hood [JavaOne 2016 CON1503]
Pushing JavaEE outside of the enterprise: Home Automation & IoT - David Delab...
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas And Streams in JDK8
Bytecode Verification, the Hero That Java Needs [JavaOne 2016 CON1500]
2015 Java update and roadmap, JUG sevilla
The State of Managed Runtimes 2013, by Attila Szegedi
8th TUC Meeting - David Meibusch, Nathan Hawes (Oracle Labs Australia). Frapp...
Pushing Java EE outside of the Enterprise: Home Automation and IoT - David De...
JavaCro'15 - Everything a Java EE Developer needs to know about the JavaScrip...
Completable future
Server Side JavaScript on the Java Platform - David Delabassee
Serverless Kotlin
The Java Virtual Machine is Over - The Polyglot VM is here - Marcus Lagergren...
Pushing Java EE outside of the Enterprise - Home Automation
[meetup] Mastering Java enhancements like a Pro: practical design patterns an...
Coding from Application Container Cloud to Oracle JET
Oracle super cluster for oracle e business suite
Ad

More from David Buck (20)

PDF
JDK 13 New Features [MeetUp with Java Experts! @Gaienmae/Dojima 2019]
PDF
JDK Mission Control: Where We Are, Where We Are Going [Groundbreakers APAC 20...
PDF
Java Bytecode Crash Course [Code One 2019]
PDF
CSI (Crash Scene Investigation) HotSpot: Common JVM Crash Causes and Solution...
PDF
invokedynamic for Mere Mortals [Code One 2019]
PDF
Hangs, Slowdowns, Starvation—Oh My! A Deep Dive into the Life of a Java Threa...
PDF
JDK Mission Control: Where We Are, Where We Are Going [Code One 2019]
PDF
Java Concurrency, A(nother) Peek Under the Hood [Code One 2019]
PDF
Z Garbage Collector
PDF
Valhalla Update JJUG CCC Spring 2019
PDF
Var handles jjug_ccc_spring_2018
PDF
JDK 10 へようこそ
PDF
Java SE 8におけるHotSpotの進化 [Java Day Tokyo 2014 C-2]
PDF
HotSpot のロック: A Peek Under the Hood [JJUG ナイトセミナ JVM 特集 2015年8月]
PDF
Java Concurrency, A(nother) Peek Under the Hood [JavaOne 2016 CON1497]
PDF
Lambda: A Peek Under The Hood [Java Day Tokyo 2015 6-3]
PDF
Java Concurrency, A(nother) Peek Under the Hood [Java Day Tokyo 2016 3-C]
PDF
Ahead-of-Time Compilation with JDK 9 [Java Day Tokyo 2017 D1-A1]
PDF
Let’s Write Our Own Chip-8 Interpreter! [JavaOne 2017 CON3584]
PDF
Everything You Wanted to Know About JIT Compilation but Were Afraid to Ask [J...
JDK 13 New Features [MeetUp with Java Experts! @Gaienmae/Dojima 2019]
JDK Mission Control: Where We Are, Where We Are Going [Groundbreakers APAC 20...
Java Bytecode Crash Course [Code One 2019]
CSI (Crash Scene Investigation) HotSpot: Common JVM Crash Causes and Solution...
invokedynamic for Mere Mortals [Code One 2019]
Hangs, Slowdowns, Starvation—Oh My! A Deep Dive into the Life of a Java Threa...
JDK Mission Control: Where We Are, Where We Are Going [Code One 2019]
Java Concurrency, A(nother) Peek Under the Hood [Code One 2019]
Z Garbage Collector
Valhalla Update JJUG CCC Spring 2019
Var handles jjug_ccc_spring_2018
JDK 10 へようこそ
Java SE 8におけるHotSpotの進化 [Java Day Tokyo 2014 C-2]
HotSpot のロック: A Peek Under the Hood [JJUG ナイトセミナ JVM 特集 2015年8月]
Java Concurrency, A(nother) Peek Under the Hood [JavaOne 2016 CON1497]
Lambda: A Peek Under The Hood [Java Day Tokyo 2015 6-3]
Java Concurrency, A(nother) Peek Under the Hood [Java Day Tokyo 2016 3-C]
Ahead-of-Time Compilation with JDK 9 [Java Day Tokyo 2017 D1-A1]
Let’s Write Our Own Chip-8 Interpreter! [JavaOne 2017 CON3584]
Everything You Wanted to Know About JIT Compilation but Were Afraid to Ask [J...

Recently uploaded (20)

PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Digital Strategies for Manufacturing Companies
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Introduction to Artificial Intelligence
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
L1 - Introduction to python Backend.pptx
PPTX
ai tools demonstartion for schools and inter college
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPT
Introduction Database Management System for Course Database
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Softaken Excel to vCard Converter Software.pdf
Odoo POS Development Services by CandidRoot Solutions
PTS Company Brochure 2025 (1).pdf.......
Digital Strategies for Manufacturing Companies
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
How Creative Agencies Leverage Project Management Software.pdf
Odoo Companies in India – Driving Business Transformation.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Introduction to Artificial Intelligence
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
L1 - Introduction to python Backend.pptx
ai tools demonstartion for schools and inter college
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Introduction Database Management System for Course Database
Which alternative to Crystal Reports is best for small or large businesses.pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)

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 David Buck 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