invokedynamic under the hood - oraclecr.openjdk.java.net/~ntv/talks/eclipsesummit16/indyunder...26...

33
invokedynamic under the hood Nadeesh T V ORACLE India Pvt Ltd 26 Aug 2016 JSR 292 invokedynamic under the hood

Upload: others

Post on 16-Sep-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

invokedynamic under the hood

Nadeesh T V

ORACLE India Pvt Ltd

26 Aug 2016

JSR 292 invokedynamic under the hood

Page 2: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

Outline

1 JVM Languages

2 PreInvokedynamic

3 Invokedynamic

4 MethodHandle

5 Summary

JSR 292 invokedynamic under the hood

Page 3: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

JVM Languages

Languages which can run on Java Virtual Machine (JVM)

Should be a valid class file containing byte codes

More than 200+ JVM languages [1]

Java (primary language) [2]JRuby (ported version of Ruby)Jython (ported version of Python)JavaScriptGroovySmalltalkScala etc...

JSR 292 invokedynamic under the hood

Page 4: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

JVM Languages

Semantics of other JVM languages differ from Java in many ways

Types can be dynamic

d e f sum ( a , b ) :t o t a l = a + br e t u r n t o t a l ;

Method invocation can be different

Smalltalk send messages for invocation

Scope, access rules can be different

JSR 292 invokedynamic under the hood

Page 5: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

Non Java Language’s Overhead

Figure: NonJava languages on JVM

Wrapper layer adds performance overheads while running on JVM!!!

JSR 292 invokedynamic under the hood

Page 6: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

NonJava Language’s Pain Points

NonJava compiler implementors have to write lot of code

JVM have to load lot of byte code (performance overhead)

Some of the dynamic languages heavily use Reflection (performanceoverhead) [3]

Every invocation requires an access checkMethod arguments are objects - boxing/unboxing

JSR 292 invokedynamic under the hood

Page 7: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

NonJava Language’s Pain Points

Wrapper layer act as a black box which can affect JIT inlining!!!

v o i d method1 ( b o o l e a n ar g ) {i f ( a rg ) {

do . . . . .} e l s e {

do . . .}

}method1 ( f a l s e )

JSR 292 invokedynamic under the hood

Page 8: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

Types of Method InvocationsBefore Java 7

invokestatic - Used to call static methods

s t a t i c v o i d s t a t i c M e t h o d ( ) {}

A . s t a t i c M e t h o d ( ) ;

Byte code of above invocation will be

i n v o k e s t a t i c #7 //Method s ta t i cMethod : ( )V

JSR 292 invokedynamic under the hood

Page 9: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

Types of Method InvocationsBefore Java 7

invokevirtual - Used to call instance methods

v o i d i n s t a n c e M e t h o d ( ) {}

o b j . i n s t a n c e M e t h o d ( ) ;

Byte code of above invocation will be

i n v o k e v i r t u a l #4 // Method ins tanceMethod : ( )V

JSR 292 invokedynamic under the hood

Page 10: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

Types of Method InvocationsBefore Java 7

invokeinterface - Use interface reference to call methods

L i s t l = new A r r a y L i s t ( ) ;l . add ( ”1” ) ;

Byte code of above invocation will be

i n v o k e i n t e r f a c e #10, 1// I n t e r f a c eMethod j a v a / u t i l / L i s t . add : ( L j ava / l ang /Object )Z

JSR 292 invokedynamic under the hood

Page 11: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

Types of Method InvocationsBefore Java 7

invokespecial - Used to call private, constructors, super methods

p r i v a t e v o i d p r i v a t e M e t h o d ( ) {}

o b j . p r i v a t e M e t h o d ( ) ;

Byte code of above invocation will be

i n v o k e s p e c i a l #5 // Method pr i va teMethod : ( )V

JSR 292 invokedynamic under the hood

Page 12: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

Types of Method InvocationsBefore Java 7

Figure: Summary of JVM bytecode invocations [2]

Receiver - Object in oop

Dispatch - Method searching and linking

JSR 292 invokedynamic under the hood

Page 13: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

JSR 292: Supporting Dynamically Typed Languages on the Java Platform

Started in 2005

Feature Completed in 2011

Released in JDK 7

Key Features

New byte code -invokedynamic

New unit of behavior -methodhandle

JSR 292 invokedynamic under the hood

Page 14: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

invokedynamic

Key Features

No receiver

Custom dispatching underuser control

Dynamic call sites can berelinked dynamically

JSR 292 invokedynamic under the hood

Page 15: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

BootStrapping1st time invocation

Linking of a Dynamic Callsite (DC) to a method (One time activity)

Figure: Flow of BootStrapping [4]JSR 292 invokedynamic under the hood

Page 16: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

BootStrap Method

Accept 3 Compulsory arguments

Byte code generator’s responsibility to link indy to BSM

Linking under user control !!!

JSR 292 invokedynamic under the hood

Page 17: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

BootStrapping2nd time invocation

(No bootstrapping/linking)

Figure: Flow of BootStrapping [4]JSR 292 invokedynamic under the hood

Page 18: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

BootStrap Method - Example

Runnable r = ( ) −> System . out . p r i n t l n ( ”” ) ;

Figure: Lambda expression byte code detailsImage adapted from [4]

JSR 292 invokedynamic under the hood

Page 19: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

MethodHandle (MH)

”MethodHandle is a typed, directly executable reference toan underlying method, constructor, field”

MH can be considered as a function/field pointer

JSR 292 invokedynamic under the hood

Page 20: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

Lookup

Contain access permission of a class

Should share only with trusted parties

Used in creating MethodHandle

Two types - publicLookup and lookup

In case of indy, JVM will pass lookup object to BSM

JSR 292 invokedynamic under the hood

Page 21: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

MethodHandle - Usage

Create using

MethodHandles.Lookup (in Java)CONSTANT MethodHandle (in byte code)

MethodHandles . Lookup l oo k u p = MethodHandles . p u b l i c L o o k u p ( )

MethodHandle m = l o o k up . f i n d S t a t i c ( S t r i n g . c l a s s , ” v a l u e O f ” ,MethodType . methodType ( S t r i n g . c l a s s , c h a r . c l a s s ) ) ;

S t r i n g output = ( S t r i n g ) m. i n v o k e E x a c t ( ’ c ’ ) ;

JSR 292 invokedynamic under the hood

Page 22: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

MethodHandle : Invocation

invokeExactArgument type should be exact matchIf no match, throw WrongMethodTypeException

invoke (invokeGeneric)Argument should be exact match/type converted match(boxing/unboxing, casting)If no match, throw WrongMethodTypeException

JSR 292 invokedynamic under the hood

Page 23: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

MethodHandle - Lookup Failing Case

c l a s s B {p r i v a t e v o i d m( ) {}

}

p u b l i c c l a s s A {p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) throws Throwable {

MethodHandle m = MethodHandles . l o o k u p ( ) . f i n d S p e c i a l (B . c l a s s ,”m” ,MethodType . methodType ( v o i d . c l a s s ) , B . c l a s s ) ;

}}

Throws IllegalAccessException

JSR 292 invokedynamic under the hood

Page 24: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

MethodHandle - Advantages

Access check only during creation of MH

No more access check during usage

No more boxing/unboxing overhead due tosignature polymorphic invocation

JSR 292 invokedynamic under the hood

Page 25: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

Signature Polymorphism

”A signature polymorphic method is one which can operatewith any of a wide range of call signatures and return types.”

invoke/invokeExact are signature polymorphic

JSR 292 invokedynamic under the hood

Page 26: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

CallSite (CS)

MethodHandle(MH) wrapped into CallSite

CS relink/retarget to different MH dynamically (setTarget)

CS is transparent to JIT and can optimistically inline through it

JSR 292 invokedynamic under the hood

Page 27: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

Summary

Figure: Flow of BootStrapping [4]

JSR 292 invokedynamic under the hood

Page 28: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

Acknowledgement

I would like to thank Vladimir Ivanov, Jamsheed CM (Oracle, Hotspotteam) for helping me in understanding invokedynamic.

JSR 292 invokedynamic under the hood

Page 29: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

References

1. ”A Renaissance VM: One Platform, Many Languages”, JRose,JVMLS 2011

2. http://cr.openjdk.java.net/ jrose/pres/200910-VMIL.pdf

3.”Invokedynamic for Mere Mortals”, David Buck, JavaOne 2015

4.”http://cr.openjdk.java.net/ vlivanov/talks/2015-IndyDeepDive.pdf”,VIvanov

5.”http://blog.headius.com/2008/09/first-taste-of-invokedynamic.html”,CNutter

JSR 292 invokedynamic under the hood

Page 30: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

Thank you

JSR 292 invokedynamic under the hood

Page 31: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

Types

Static - types determined at compile time

i n t x ;x = 2 ;x = ” h i ” ; // E r r o r

Dynamic - types determined at run time

x = 2 ;x = ” h i ” ; // Works

JSR 292 invokedynamic under the hood

Page 32: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

Types

Weak - types can be mixed

$a =10;$b=”a” ;$c=$a . $b ;p r i n t $c ; #10a

$a =10;$b=”20 a” ;$c=$a+$b ;p r i n t $c ; #30

JSR 292 invokedynamic under the hood

Page 33: invokedynamic under the hood - Oraclecr.openjdk.java.net/~ntv/talks/eclipseSummit16/indyunder...26 Aug 2016 JSR 292 invokedynamic under the hood Outline 1 JVM Languages 2 PreInvokedynamic

Types

Strong - No implicit type conversion

x = 2 ;y = 2 + ” h i ” ; // E r r o r

References1. ”http://www.rubyfleebie.com/ruby-is-dynamically-and-strongly-typed/”2.”http://www.i-programmer.info/programming/theory/1469-type-systems-demystified-part2-weak-vs-strong.html”

JSR 292 invokedynamic under the hood